56

In my Rails 4 app I would like to collapse my migration files into one large file (similar to schema.rb) as it's time to do some housekeeping but I'm not sure on how to access the table in the database that stores migration data so that when I run a migration I don't receive any errors/conflicts.

Question How can I access and delete the data in the table that stores migration data?

tommyd456
  • 10,443
  • 26
  • 89
  • 163
  • Have you tried copy/pasting the `schema.rb` file into a migration file? – davegson Sep 30 '13 at 17:51
  • I don't have a problem creating the single migration file - but if I just change the migration files without considering the db I will get errors during a migration. – tommyd456 Sep 30 '13 at 17:53
  • My current understanding is that you wish to have a file that stores all your migrations, so that you can clean em up? If so, I'd suggest looping through them and writing the data to a new file. Using methods like `File` `.glob` etc... – davegson Sep 30 '13 at 18:02
  • No - You are misunderstanding. Please see the question – tommyd456 Sep 30 '13 at 18:12
  • ok somehow I didn't get your question – davegson Sep 30 '13 at 18:19
  • a more popular similar question here - http://stackoverflow.com/q/3343534/753705 – abhishek77in Oct 02 '15 at 03:40

6 Answers6

82

for fun, you can also manipulate these in the console by making a model class for them...

class SchemaMigration < ActiveRecord::Base; self.primary_key = :version; end

then you can do SchemaMigration.all, SchemaMigration.last.delete, etc.

Really just a substitute for using SQL, and it is very rare that you would need to mess around at this low level… generally a bad idea but cool to see how to do it :)

David Lowenfels
  • 1,000
  • 6
  • 5
  • 1
    This works - just run that code in Rails console to create a model in that console-instance. Then you can select rows from that table with .where(:version => ...) and delete references to migrations as needed. Very helpful since PGAdmin won't let you delete rows from tables that don't have a primary key specified with the gui. – JosephK May 11 '17 at 06:08
  • 'generally'. But there are edge cases. Created a migration with a reference, adding `default: 1` where 1 did not exist. The migration halted... but the database was already altered. Said migration could not run. This saved the day and sanity cycles for me. Needless to say, this requires monkish patience and scrutiny before altering... – Jerome Aug 31 '20 at 11:31
  • @JosephK you can alter the schema_migrations table with psql commands. see answer below. – Jerome Aug 31 '20 at 11:34
61

Another solution could be to access it through:

ActiveRecord::SchemaMigration

The answer given by David didn't work in my context.

Bergrebell
  • 4,263
  • 4
  • 40
  • 53
19

The schema_migrations table holds the revision numbers; with the last record being the most recently executed migration. You can just manipulate these records manually.

steakchaser
  • 5,198
  • 1
  • 26
  • 34
10

to get the last version:

ActiveRecord::SchemaMigration.last.version

or all versions:

ActiveRecord::SchemaMigration.all.map(&:version)
localhostdotdev
  • 1,795
  • 16
  • 21
7

Not sure why you want to do this but here you go:

ActiveRecord::Migrator.get_all_versions

Agustin
  • 1,254
  • 13
  • 10
-1

I've had to do some cleanup of the sort: accumulation of seemingly trivial migrations create such pollution that things stop making sense.

As a last phase of development (not recommended once in production), you can clear out the schema_migrations table, consolidate your migrations (one-to-one with classes) and create a new table (beware: running migrate has different behaviours, depending on mysql vs postgresql)

@david-lowenfels answer is perfect for this context.

All this, naturally, assumes you haven't made errors in keys, indices, defaults. This is a serious task, but not an insensible one at the end of a development phase.

Jerome
  • 5,583
  • 3
  • 33
  • 76