Last active
February 26, 2020 02:46
-
-
Save ryochin/aa9b6636a40f29148a3f1bdd2c71f5cd to your computer and use it in GitHub Desktop.
[Rails] MySQL lock_tables
This file contains hidden or 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
def lock_tables(tables) | |
pair = tables.map { |table, type| '%s %s' % [table, type.to_s.upcase] } | |
ActiveRecord::Base.connection.execute 'LOCK TABLES %s' % pair.join(', ') | |
yield | |
ensure | |
ActiveRecord::Base.connection.execute 'UNLOCK TABLES' | |
end | |
lock_tables users: :read, blogs: :write do | |
# do something | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment