-
Thanks for providing such a great library. If there is a column of type How should I write the stepByStep logic in onUpgrade? @override
MigrationStrategy get migration {
return MigrationStrategy(
onCreate: (m) async {
await m.createAll();
},
beforeOpen: (details) async {
// Make sure that foreign keys are enabled
await customStatement('PRAGMA foreign_keys = ON');
},
onUpgrade: stepByStep(
from1To2: (m, schema) async {
// Write your migrations here
},
from2To3: (Migrator m, Schema3 schema) async {},
from3To4: (Migrator m, Schema4 schema) async {
await m.alterTable(TableMigration(
schema.tableUser,
columnTransformer: {
schema.tableUser.age:
Variable(schema.tableUser.age == 'sixteen' ? 16 : 0),
},
));
}
),
);
} Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think there are two reasonable options here:
var oldTextColumn = schema.tableUser.age.dartCast<String>();
columnTransformer: {
schema.tableUser.age: oldTextColumn.caseMatch<int>({
Constant('zero'): Constant(0),
Constant('one'): Constant(1),
// ...
})
} 2.If you have a generic parser that goes from the string representation to the number, you could register that as a user-defined function to invoke in the migration. For instance, if you open your database like this: NativeDatabase.createInBackground(
File(yourDatabasePath),
setup: (database) {
database.createFunction(
functionName: 'text_to_number',
function: (args) {
final text = args[0] as String;
// todo: Parse number string
return 0;
},
);
},
) Then you could write columnTransformer: {
schema.tableUser.age: CustomExpression('text_to_number(age)')
} |
Beta Was this translation helpful? Give feedback.
I think there are two reasonable options here:
age
will only take a few possible options (e.g. you know that it's between0
and120
), you could use a very long caseMatch expression to map all of the values:2.If you have a generic parser that goes from the string representation to the number, you could register that as a user-defined function to invoke in the migration. For instance, if you open your database like this: