Skip to content
manual

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 ​

  1. Add a default: Add .default() to the new column in your schema file.
  2. Commit the change: Run baseline db:commit to generate the migration.
  3. Refine (Optional): If you require complex logic, manually edit the generated .sql file.
  4. Remove the default: Remove the .default() constraint from your schema file.
  5. Re-commit: Run baseline db:commit again 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.

  1. Generate an empty migration:
    bash
    baseline db:migration
  2. Write custom SQL: Manually write the necessary ALTER TABLE or UPDATE statements within the newly created SQL file to handle the data transformation or constraint application.