Monthly Archives: March 2022

MySql Auto-Increment

While playing around with MySql recently, I discovered a strange little quirk. An automatically incremented field cannot be reset – that is, it cannot be reset to a lower value than it currently is. For example, say that you insert a lot of records into a table, or that you manually add a key that’s very high:

MySql.Data.MySqlClient.MySqlException: ‘Duplicate entry ‘2147483647’ for key ‘test_table.PRIMARY”

In my case, I was simply playing around with some settings – if you have a table with more records than that then you may have other issues than this. However, my initial idea was to reset the count:

ALTER TABLE myschema.test_table AUTO_INCREMENT = 1

It turns out that, whilst you can do this, you can only do it for numbers higher than it currently is. Presumably this is to prevent conflicts or whatever. Anyway, the way around it for me was to simply insert a new record, but to override the setting of the auto-increment:

INSERT INTO `myschema`.`test_table`

You would then need to remove this record. Not ideal, but the only way I could get this to work.


A few weeks ago, I was looking into making a change to a project in work that uses DbUp. For some reason, I took away from that the overwhelming urge to write my own data generator. It’s far from finished, but I came up with datagen. This currently only comes in a MySql flavour, but my plan is to add a few more database engines.

The idea behind this is that you can generate pseudo data in your database. It’s not a tool in its own right, because I wanted to allow it to be customisable. To install , simply reference the package:

<PackageReference Include="datagen.MySql" Version="1.0.0" />

You can then populate the data in an entire schema. Just create a console app (this works with any app type that can physically access the database):

using datagen.Core;
using datagen.MySql;
using datagen.MySql.MySql;

var valueGenerator = new ValueGenerator(

string connectionString = "Server=;Port=3306;Database=datagentest;Uid=root;Pwd=password;AllowUserVariables=True";

var mySqlDefaults = new MySqlDefaults(connectionString);

var generate = new Generate(
    mySqlDefaults .UniqueKeyGenerator);
await generate.FillSchema(20, "datagentest");

The code above allows you to create a ValueGenerator – there is a default one in the package, but you can easily write your own. FillSchema then adds 20 rows to every table in the schema.


There are currently a few limitations – the main two being that this will only work with MySql currently, and that it does not deal with foreign keys (it will just omit that data).

Feel free to contribute, offer suggestions, or contibute.

Terraform adding control of an object not created through Terraform

In recent investigations into Terraform, I came across a situation where I’d created a resource not known to Terraform, but which I now wished to manage through it. Initially, I simply tried to create the resource. Interestingly, this goes through the plan stage, but errors at the apply stage with something similar to the following error:

│ Error: A resource with the ID “/subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter” already exists – to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for “azurerm_logic_app_workflow” for more information.

│ with azurerm_logic_app_workflow.logicapp,
│ on line 22, in resource “azurerm_logic_app_workflow” “logicapp”:
│ 22: resource “azurerm_logic_app_workflow” “logicapp” {

This is a really useful error – if you do, in fact, look at the docs for that resource then you’ll see at the end it gives the following way of importing:

terraform import azurerm_logic_app_workflow.workflow1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Logic/workflows/workflow1

So, simply replace the values that you’ve been given in the error message and you end up with:

terraform import azurerm_logic_app_workflow.logicapp /subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter

The result of running this is:

PS C:\Users\pcmic\source\repos\tf-logicapp-test> terraform import azurerm_logic_app_workflow.logicapp /subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter
azurerm_logic_app_workflow.logicapp: Importing from ID “/subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter”…
azurerm_logic_app_workflow.logicapp: Import prepared!
Prepared azurerm_logic_app_workflow for import
azurerm_logic_app_workflow.logicapp: Refreshing state… [id=/subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

This now adds that resource into your state file (.tfstate):

      "mode": "managed",
      "type": "azurerm_logic_app_workflow",
      "name": "logicapp",
. . .