Skip to content

Migrator: Allow to run migration files outside of transaction for Sqlite PRAGMAs #4074

@malteneuss

Description

@malteneuss

I have found these related issues/pull requests

Relates to #2085

Description

When using Sqlite3 you need to setup some (feature) settings for your database initially like

-- 00000_initial_setup.sql
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

However, the migrator runs the whole file in a single transaction, which is correct for the remaining migration files, but leads to an error with Sqlite3:

ExecuteMigration(Database(SqliteError { code: 1, message: "Safety level may not be changed inside a transaction" }), 0)

Prefered solution

Add some (comment) marker to the migration file format that instructs the migrator to run this particular file outside of a transaction. E.g. the popular dbmate migration tool supports a feature flag "transaction" among others:

-- migrate:up transaction:false
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

Alternatively, document the workaround of #2085 (comment):

-- terminate the original migrator TRANSACTION
COMMIT TRANSACTION;

-- 00000_initial_setup.sql
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

-- restart new migrator TRANSACTION
BEGIN TRANSACTION;

Is this a breaking change? Why or why not?

No breaking change, as this would be an optional, additional feature flag.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions