Skip to content

Instantly share code, notes, and snippets.

@n0ts
Created March 17, 2011 08:16
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save n0ts/873997 to your computer and use it in GitHub Desktop.
Save n0ts/873997 to your computer and use it in GitHub Desktop.
insert db migration version to schema_migrations table
require 'optparse'
ENV['RAILS_ENV'] = ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
opts = {}
ARGV.options {|opt|
opt.on('-s', '--save', 'save') {|v| opts[:save] = v }
opt.parse!
}
Dir::entries("#{RAILS_ROOT}/db/migrate/").sort.each do |v|
next if v == "." or v == ".." or v == ".svn"
version, name = v.split('_', 2)
begin
if opts[:save]
ActiveRecord::Base.connection.execute("INSERT INTO schema_migrations values ('#{version}')")
end
rescue => e
p e
exit
end
end
@mohitmun
Copy link

👍

@jurielsz7
Copy link

ActiveRecord::SchemaMigration.create(version)

@Fornacula
Copy link

To specify @jurielsz7 answer, then the argument must be passed as a hash, otherwise ArgumentError (When assigning attributes, you must pass a hash as an argument, String passed.) error will be given.

So in following form it works as expected: ActiveRecord::SchemaMigration.create(version: '20211023125427')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment