How to migrate column type from Text to DateTime with a custom cast expression #1421
-
Hello, I'm trying to migrate a column of type Text to DateTime. I tried performing the migration like this: migrator.alterTable(TableMigration(
affectedTable,
columnTransformer: {
affectedTable.dateColumn:
affectedTable.dateColumn.cast<DateTime>(),
},
)); Doing it like this converts the Text "2021-05-27T14:19:58-04:00" to an integer 2021 which seems to be only the year. I know that seconds since epoch is being stored in moor. DateTime date = DateTime.parse('2021-05-27T14:19:58-04:00').toLocal();
print('${date.millisecondsSinceEpoch/1000}'); //1622139598 Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I already posted this in Gitter, but this has a better visibility :) In sqlite3, you can convert a date time (stored as text) into UNIX seconds with |
Beta Was this translation helpful? Give feedback.
I already posted this in Gitter, but this has a better visibility :)
In sqlite3, you can convert a date time (stored as text) into UNIX seconds with
strftime('%s', datetime(your date time))
. The corresponding action in moor isFunctionCallExpression<DateTime>('strftime', [Constant('%s', FunctionCallExpression('datetime', table.yourDateColumn))])
, which you can use in a column transformer.