Using rake db: migrate straight, vanilla SQL

What are the problems using rake db:migrate to load vanilla SQL?

The business requirements I work with do not allow me to use the default Rails migrations. But I still need to track changes, easily change the DDL of the database and other things that Rails migration gives you.

So, the migration file will look like this:

 class AddDateToPost < ActiveRecord::Migration def self.up ActiveRecord::Base.connection.execute("ALTER TABLE `posts` ADD COLUMN date DATETIME NULL") end def self.down ActiveRecord::Base.connection.execute("ALTER TABLE `posts` DROP COLUMN date") end end 
+6
ruby-on-rails rake rails-migrations
source share
2 answers

This is perfectly acceptable, and there are no touches if you are sure that your up and down functions reflect each other. I would suggest doing the following for readability:

  class AddDateToPost <ActiveRecord :: Migration
   def self.up
     execute "ALTER TABLE` posts` ADD COLUMN date DATETIME NULL "
   end

   def self.down
     execute "ALTER TABLE` posts` DROP COLUMN date "
   end
 end
+17
source share

You can use the Rails migration methods in a project without rails using the standalone-migrations gem .

After installing the gem, you will add the following lines to the Rakefile to enable rake db:* tasks rake db:* :

 require 'standalone_migrations' StandaloneMigrations::Tasks.load_tasks 

After that, you simply configure your migrations, as usual:

 class AddDateToPost < ActiveRecord::Migration def self.up add_columm :posts, :date, :datetime, default: nil end def self.down remove_columm :posts, :date end end 
0
source share

All Articles