Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I pass to schema_migration in ActiveRecord::MigrationContext#new

Since the newest Rails version, ActiveRecord::MigrationContext#new seems to take a new argument called schema_migration. But I have no idea what to pass there and where to get it.

I cannot find any information on it. I googled for an hour, all examples for MigrationContext I found referred to older rails versions. The class MigrationContext doesn't seem to be documented at all. From the source code I couldn't figure out what to pass either.

Some context: I am trying to test some of my more dangerous migrations. I found quite a few tutorials and it seemed easy and I went along an did it. But the code that prepares the state of the test db so I can apply the migration is currently not working. Sadly all the tutorials used older Rails versions and this fails due to the wrong number of arguments:

ActiveRecord::MigrationContext.new(migrations_paths)
like image 611
Lykos Avatar asked Apr 01 '20 15:04

Lykos


People also ask

How do I run a specific migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How do I rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

How do I migrate a database in Ruby on Rails?

Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema. rb file to match the structure of your database.


2 Answers

I found out what I need to pass:

ActiveRecord::Base.connection.schema_migration

So the whole code would be:

  migrations_paths = ActiveRecord::Migrator.migrations_paths
  schema_migration = ActiveRecord::Base.connection.schema_migration
  migration_context = ActiveRecord::MigrationContext.new(migrations_paths, schema_migration)

I googled and tried for an hour and didn't figure it out, but just after posting the question, I finally stumbled upon this github issue https://github.com/pat/combustion/issues/98 which had the same problem and a solution.

like image 178
Lykos Avatar answered Oct 16 '22 08:10

Lykos


You might also just want to use ActiveRecord::Base.connection.migration_context instead of ActiveRecord::MigrationContext.new. No arguments needed.

like image 4
Jacquen Avatar answered Oct 16 '22 08:10

Jacquen