simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.56k stars 365 forks source link

`pragma legacy_alter_table = 1` not supported in Turso #3088

Open exaby73 opened 2 months ago

exaby73 commented 2 months ago

Describe the bug

Hello!. Thank you for the support for hrana and by extension, Turso. I am facing an error with an alter migration. Here are the steps to reproduce:

This was my previous row

DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();

Then changed to the following since I wanted to explicitly save the date in UTC

DateTimeColumn get createdAt => dateTime().clientDefault(() => DateTime.now().toUtc())();

I have created a migration like so in onUpgrade

// Other migrations
if (from < 4) {
  await m.alterTable(TableMigration(tableWithChangedDefault));
}

This causes the following error to be thrown

HranaException(SQL_PARSE_ERROR): SQL string could not be parsed: unsupported statement: pragma legacy_alter_table = 1;
simolus3 commented 2 months ago

I've filed https://github.com/tursodatabase/libsql/issues/1547 to ask about this - looking at some sqld code, they seem to be considering legacy_alter_table in some places. So I hope it's an oversight and that the pragma is supposed to work. Let's see what happens upstream.

exaby73 commented 2 months ago

Thanks for chasing this :)

exaby73 commented 2 months ago

Speaking of Turso, how is hrana saving migration information? I used to see a schema migrations table when I was using drift with postgres, but it seems like it's not there in Turso. I tried running .tables in the shell and SELECT * FROM sqlite_master WHERE type='table', both gives me one the tables I created, but no schema migrations table. Is this intended?

simolus3 commented 2 months ago

sqlite, and libsql by extension, have a user_version pragma saved in the header of the database file. Drift is using that as a simple version number to record which schema is currently active. You can view and update it manually with pragma user_version.