@@ -18,6 +18,19 @@ abstract class Avram::Migrator::Migration::V1
1818 end
1919 end
2020
21+ # This macro will tell the migration to run all prepared statements
22+ # outside of a transaction. This is considered "unsafe" as improper
23+ # SQL could pose risk to data integrity if not handled correctly
24+ macro unsafe_migration
25+ def run_in_transaction : Bool
26+ false
27+ end
28+ end
29+
30+ def run_in_transaction : Bool
31+ true
32+ end
33+
2134 abstract def migrate
2235 abstract def version : Int64
2336
@@ -34,7 +47,7 @@ abstract class Avram::Migrator::Migration::V1
3447 else
3548 reset_prepared_statements
3649 migrate
37- execute_in_transaction @prepared_statements do |txn |
50+ run_prepared_statements @prepared_statements do |txn |
3851 track_migration(txn)
3952 unless quiet
4053 puts " Migrated #{ self .class.name.colorize(:green )} "
@@ -52,7 +65,7 @@ abstract class Avram::Migrator::Migration::V1
5265 else
5366 reset_prepared_statements
5467 rollback
55- execute_in_transaction @prepared_statements do |txn |
68+ run_prepared_statements @prepared_statements do |txn |
5669 untrack_migration(txn)
5770 unless quiet
5871 puts " Rolled back #{ self .class.name.colorize(:green )} "
@@ -93,13 +106,18 @@ abstract class Avram::Migrator::Migration::V1
93106 # # Usage
94107 #
95108 # ```
96- # execute_in_transaction ["DROP TABLE comments;"] do |db|
109+ # run_prepared_statements ["DROP TABLE comments;"] do |db|
97110 # db.exec "DROP TABLE users;"
98111 # end
99112 # ```
100- private def execute_in_transaction (statements : Array (String ), & )
113+ private def run_prepared_statements (statements : Array (String ), & )
101114 database = Avram .settings.database_to_migrate
102- database.transaction do
115+ if run_in_transaction
116+ database.transaction do
117+ statements.each { |sql | database.exec sql }
118+ yield database
119+ end
120+ else
103121 statements.each { |sql | database.exec sql }
104122 yield database
105123 end
0 commit comments