Migrate external table to enum column #3196
-
Hello everyone, i have a two question with migrations. For example, i have these tables
I would like to simplify and remove MeasureUnits table and put directly the enum colum into Products, like this
Also, i would like delete a custom constrain, for example
the result should be
How could i execute this migration? thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I assume final innerProducts = products.createAlias('i');
// For each product we're about to select from in the TableMigration step, resolve the measurement name based on the current measurement id:
final unitNameFromId = subqueryExpression(selectOnly(measureUnits)
..addColumns([measureUnits.name])
..join([
innerJoin(innerProducts,
innerProducts.measureUnitId.equalsExp(measureUnits.id))
])
..where(innerProducts.id.equalsExp(products.id)));
await m.alterTable(TableMigration(
products,
newColumns: {products.unitName},
columnTransformer: {
products.unitName: unitNameFromId
}
)); After the migration has completed, you can delete the products table. With these complex migrations, I strongly recommend exporting the schemas so that you can write reliable unit tests to make sure no data is lost. For the second migration that alters table constraints without altering table semantics, you can run a simple alterTable call. |
Beta Was this translation helpful? Give feedback.
I assume
MeasureUnits
currently has anid
column as well, right? Assuming thatProducts
also has anid
column, a migration could look like this: