drift
drift copied to clipboard
`pragma legacy_alter_table = 1` not supported in Turso
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;
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.
Thanks for chasing this :)
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?
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.
Looks like this is not happening in Turso. I didn't want to touch the alterTable implementation too much to prevent accidentally breaking users with weird dependencies between tables and views. But we can just catch the exception triggered by pragma legacy_alter_table = 1 and apply a workaround only in that case.