Sybren A. Stüvel a4e5eef83e Manager: fix database migration 0004
Fix the database migration that adds `NOT NULL` clauses. It used
`INSERT INTO temp_x SELECT * from x;`, and the `*` returns the fields in
the order they are defined on the table. Since this might be different from
the order that the `INSERT INTO temp_x` expects, strange problems can
happen where columns get swapped (or constraints can fail on columns that
they should not fail for, because they got fed data from a different
column).
2024-03-04 13:06:09 +01:00

26 lines
835 B
Markdown

# SQL Migrations
The files here are run by [Goose][goose], the database migration tool.
[goose]: https://pkg.go.dev/github.com/pressly/goose/v3
These are embedded into the Flamenco Manager executable, and automatically run
on startup.
## Foreign Key Constraints
Foreign Key constraints (FKCs) are optional in SQLite, and always enabled by
Flamenco Manager. These are temporarily disabled during database migration
itself. This means you can replace a table like this, without `ON DELETE`
effects running.
```sql
INSERT INTO `temp_table` SELECT field1, field2, etc FROM `actual_table`;
DROP TABLE `actual_table`;
ALTER TABLE `temp_table` RENAME TO `actual_table`;
```
Note that the `SELECT` clause lists each field specifically. This is to ensure
that they are selected in the expected order. Without this, data can get
mangled.