Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip_transaction support to single migrations #1021

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/avram/migrator/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ abstract class Avram::Migrator::Migration::V1
end
end

def initialize(@skip_transaction = false)
end

abstract def migrate
abstract def version : Int64

Expand Down Expand Up @@ -74,6 +77,10 @@ abstract class Avram::Migrator::Migration::V1
end
end

def skip_transaction(value : Bool)
@skip_transaction = value
end

private def track_migration(db : Avram::Database.class)
db.exec "INSERT INTO migrations(version) VALUES ($1)", version
end
Expand All @@ -99,9 +106,14 @@ abstract class Avram::Migrator::Migration::V1
# ```
private def execute_in_transaction(statements : Array(String), &)
database = Avram.settings.database_to_migrate
database.transaction do
if @skip_transaction
statements.each { |s| database.exec s }
yield database
else
database.transaction do
statements.each { |s| database.exec s }
yield database
end
end
rescue e : PQ::PQError
raise FailedMigration.new(migration: self.class.name, statements: statements, cause: e)
Expand Down