Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Keep a table out of structure.sql during migrations

It is straightforward to ignore tables when your schema format is :ruby, but is there a way to do it when your schema format is :sql?

Ideally something like this in environment.rb:

ActiveRecord::SQLDumper.ignore_tables = ['table_name']

After a quick perusal through the AR source code it looks unpromising.

like image 477
steel Avatar asked Apr 20 '16 16:04

steel


People also ask

What is migration in Ruby on rails?

Ruby on Rails - Migrations. Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code. Teams of developers − If one person makes a schema change, the other developers just need to update, and run "rake migrate".

Can I use SQL inside a rails migration?

When you add a new model to a Rails application, it generates a migration to create the corresponding table for you. If that’s not what you need, you can write your own. If you need functionality that’s not supported by Active Record, you can execute SQL inside a migration.

How do I rollback a SQL statement in rails?

Rails doesn’t know how to rollback our SQL statement. You’re only supposed to place directives in a change method that Rails knows how to revert. Let’s write a migration that can be rolled back. Generate another migration named “AddAnotherUser” and open the file.

What happens if you can’t use a specific table in rails?

In the context of a high-load database, not being able to use a specific table for more than a few seconds can be catastrophic: all requests will start queuing and it may put the whole platform down. Let’s look at a few examples of Rails migrations, the kind of locking they require, and how to mitigate their impact.


1 Answers

There is currently no way to do this, when the schema format is set to :sql, Rails doesn't go through the regular SchemaDumper but instead uses the tasks in ActiveRecord::Tasks::PostgreSQLDatabaseTasks to do the dump, check it out here.

The code is quite straightforward. I came up with a simple patch for ActiveRecord that should work as expected. It relies on setting the tables to ignore in your database.yml file. It basically adds the following code:

ignore_tables = configuration['ignore_tables']
unless ignore_tables.blank?
  args += ignore_tables.split(',').map do |table|
    "-T #{table}"
  end
end

I just submitted a pull request to rails with those changes. In case you'd want to test it.

like image 122
Marc Lainez Avatar answered Feb 07 '23 06:02

Marc Lainez