drift icon indicating copy to clipboard operation
drift copied to clipboard

`pragma legacy_alter_table = 1` not supported in Turso

Open exaby73 opened this issue 1 year ago • 4 comments

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;

exaby73 avatar Jul 11 '24 12:07 exaby73

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.

simolus3 avatar Jul 11 '24 13:07 simolus3

Thanks for chasing this :)

exaby73 avatar Jul 13 '24 08:07 exaby73

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?

exaby73 avatar Jul 13 '24 08:07 exaby73

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.

simolus3 avatar Jul 13 '24 09:07 simolus3

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.

simolus3 avatar Nov 30 '24 22:11 simolus3