Description
Hi!
There is a case where I want to lock the table the triggers are triggered on because I want to make sure nothing gets inserted, updated or deleted while the triggers are dropped and created.
Initially I started with just a execute('LOCK TABLES table WRITE;')
at the start of the up
and down
methods.
But this changes the definition of the methods and after migrating the following warning is added to the schema.rb. And a generic execute
with CREATE TRIGGER
statement is used instead of the hair_trigger DSL.
WARNING: generating adapter-specific definition for table_after_insert_row_tr due to a mismatch. either there's a bug in hairtrigger or you've messed up your migrations and/or db :-/
To ensure we are still using the hair_trigger DSL and won't have this waning in our schema I created the following workaround:
module WrapUpAndDownWithLocks
def up
execute('LOCK TABLES table WRITE;')
super
ensure
execute('UNLOCK TABLES;')
end
def down
execute('LOCK TABLES table WRITE;')
super
ensure
execute('UNLOCK TABLES;')
end
end
class CreateTriggersTableInsertOrTableUpdateOrTableDelete < ActiveRecord::Migration[7.0]
prepend WrapUpAndDownWithLocks
def up
# migration generated by hair_trigger
end
def down
# migration generated by hair_trigger
end
end
Its a bit hacky but gets the job done. Not sure if this problem is something that should be solved by the gem itself. But I wanted to share my solution here for anyone else running into this.