Skip to content

How to migrate column type to update value based on old column value? #3352

Answered by simolus3
iamiota asked this question in Q&A
Discussion options

You must be logged in to vote

I think there are two reasonable options here:

  1. If you know that age will only take a few possible options (e.g. you know that it's between 0 and 120), you could use a very long caseMatch expression to map all of the values:
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.createInBackg…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@iamiota
Comment options

Answer selected by iamiota
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants