Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep a table out of schema.rb during migrations

As a follow-on to an earlier question about not reloading a huge, persistent table when I run my tests, I need to keep this table out of schema.rb when I run my migrations. This table gets loaded directly from a mysqldump, so I'm not worried about keeping track of it.

So, how can I keep a specific table out of schema.rb?

like image 684
Tarek Avatar asked Nov 17 '09 23:11

Tarek


People also ask

What is schema RB used for?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone.

How is schema RB generated?

It is a Ruby representation of your database; schema. rb is created by inspecting the database and expressing its structure using Ruby. It is database-agnostic (i.e. whether you use SQLite, PostgreSQL, MySQL or any other database that Rails supports, the syntax and structure will remain largely the same)

What does rake db schema load do?

Unlike rake db:migrate that runs migrations that have not run yet, rake db:schema:load loads the schema that is already generated in db/schema. rb into the database. Always use this command when: You run the application for the first time.


1 Answers

Turns out there's an option for just this situation!

I found it in activerecord-2.3.4/lib/active_record/schema_dumper.rb:

##
# :singleton-method:
# A list of tables which should not be dumped to the schema. 
# Acceptable values are strings as well as regexp.
# This setting is only used if ActiveRecord::Base.schema_format == :ruby
cattr_accessor :ignore_tables 
@@ignore_tables = []

So all I had to do was stick this at the end of environment.rb:

ActiveRecord::SchemaDumper.ignore_tables = ["table_name"]

If ActiveRecord.schema_format == :ruby, the array can also contain RegExp. For example, to ignore all tables starting with "MS":

ActiveRecord::SchemaDumper.ignore_tables = [/^MS/]
like image 75
Tarek Avatar answered Oct 09 '22 20:10

Tarek