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

Lock the table triggers are created for #111

Open
yourivdlans opened this issue Mar 20, 2023 · 0 comments
Open

Lock the table triggers are created for #111

yourivdlans opened this issue Mar 20, 2023 · 0 comments

Comments

@yourivdlans
Copy link

yourivdlans commented Mar 20, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant