Skip to content

Instantly share code, notes, and snippets.

@ryochin
Last active February 26, 2020 02:46
Show Gist options
  • Save ryochin/aa9b6636a40f29148a3f1bdd2c71f5cd to your computer and use it in GitHub Desktop.
Save ryochin/aa9b6636a40f29148a3f1bdd2c71f5cd to your computer and use it in GitHub Desktop.
[Rails] MySQL lock_tables
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