Migrations
Handling Existing Data
Sometimes, adding or changing an existing column can come with some challenges, for example adding a new non-nullable column eventhough there are existing records.
Here are two example approaches:
Approach 1: The Default Value Transition
- Add a default: Add
.default()to the new column in your schema file. - Commit the change: Run
baseline db:committo generate the migration. - Refine (Optional): If you require complex logic, manually edit the generated
.sqlfile. - Remove the default: Remove the
.default()constraint from your schema file. - Re-commit: Run
baseline db:commitagain to finalize the schema without the default constraint.
Approach 2: Manual SQL Migration
If your data transformation is complex (e.g., splitting a name column into first_name and last_name), use a manual migration.
- Generate an empty migration:bash
baseline db:migration - Write custom SQL: Manually write the necessary
ALTER TABLEorUPDATEstatements within the newly created SQL file to handle the data transformation or constraint application.