Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ActiveRecord migrations without Rails

I am trying to update a gem called textacular to be compatible with latest rails version.

In the development Rake-tasks we need to run a few ActiveRecord migrations - without Rails.

Today it looks like:

namespace :migrate do
    desc 'Run the test database migrations'
    task :up => :'db:connect' do
      ActiveRecord::Migrator.up 'db/migrate'
    end

    desc 'Reverse the test database migrations'
    task :down => :'db:connect' do
      ActiveRecord::Migrator.down 'db/migrate'
    end
end

However when using ActiveRecord >= 5, it fails with:

NoMethodError: undefined method 'up' for ActiveRecord::Migrator:Class.

I have tried to look through the source code for ActiveRecord, tried a bunch of different methods but have not managed to run the migrations.

Does anyone have a hint of what to do?

Edit

Using ActiveRecord::Migration.up does nothing, probably just returns based on the method.

Using ActiveRecord::Migration.migrate(:up) gives output:

==  ActiveRecord::Migration: migrating ========================================
==  ActiveRecord::Migration: migrated (0.0000s) ===============================

All migrations are in the folder db/migrate.

like image 975
davidwessman Avatar asked Feb 21 '18 19:02

davidwessman


People also ask

Can you use ActiveRecord without rails?

One of the primary aspects of ActiveRecord is that there is very little to no configuration needed. It follow convention over configuration. ActiveRecord is commonly used with the Ruby-on-Rails framework but you can use it with Sinatra or without any web framework if desired.

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.

Which command is true to 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.

What does Rails DB Reset do?

rails db:reset:primary Drops and recreates the primary database from its schema for the current environment and loads the seeds. rails db:reset:secondary Drops and recreates the secondary database from its schema for the current environment and loads the seeds.


1 Answers

You can also use this command:

ActiveRecord::MigrationContext.new(Rails.root.join('db', 'migrate'), ActiveRecord::SchemaMigration).migrate
like image 110
Ruan Carlos Avatar answered Sep 20 '22 00:09

Ruan Carlos