-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensions: Set Local & Migration Transaction Options (#26)
Add extensions: set_local, migration_transaction_options.
- Loading branch information
Showing
8 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module MigrationDSLExtension | ||
def transaction_options(opts) | ||
migration.transaction_opts = opts | ||
end | ||
end | ||
|
||
module SimpleMigrationExtension | ||
attr_accessor :transaction_opts | ||
end | ||
|
||
module MigratorExtension | ||
def checked_transaction(migration, &block) | ||
if _use_transaction?(migration) | ||
_transaction(migration, &block) | ||
else | ||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
def _use_transaction?(migration) | ||
# NOTE: original code | ||
if @use_transactions.nil? | ||
if migration.use_transactions.nil? | ||
@db.supports_transactional_ddl? | ||
else | ||
migration.use_transactions | ||
end | ||
else | ||
@use_transactions | ||
end | ||
end | ||
|
||
def _transaction(migration, &block) | ||
if migration.transaction_opts.nil? | ||
db.transaction(&block) | ||
else | ||
db.transaction(migration.transaction_opts, &block) | ||
end | ||
end | ||
end | ||
|
||
Sequel::MigrationDSL.include(MigrationDSLExtension) | ||
Sequel::SimpleMigration.include(SimpleMigrationExtension) | ||
Sequel::Migrator.prepend(MigratorExtension) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sequel | ||
module SetLocal | ||
private | ||
|
||
def begin_new_transaction(conn, opts) | ||
super | ||
check_set_local(conn, opts[:set_local]) | ||
end | ||
|
||
def check_set_local(conn, locals) | ||
return if locals.nil? | ||
|
||
locals.each do |key, value| | ||
log_connection_execute(conn, "SET LOCAL #{key} = \"#{value}\"") | ||
end | ||
end | ||
end | ||
|
||
Database.register_extension(:set_local, SetLocal) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "migration_transaction_options" do | ||
def migrator | ||
Sequel::TimestampMigrator.new(DB, "spec/files/migrations") | ||
end | ||
|
||
before { migrator.run } | ||
|
||
it "does not migrate since rollback always option is set" do | ||
expect(DB.tables).to include(:first_table, :second_table) | ||
expect(DB.tables).not_to include(:third_table) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "set_local" do | ||
def run! | ||
DB.transaction(set_local: { statement_timeout: "1s" }) do | ||
DB.execute("SELECT pg_sleep(2)") | ||
end | ||
end | ||
|
||
specify do | ||
expect { run! }.to raise_error(/canceling statement due to statement timeout/) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
Sequel.migration do | ||
transaction_options(rollback: :always) | ||
|
||
up { create_table :third_table } | ||
down { drop_table :third_table } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters