Send this to a friend
it’s a small how to import a file in migrations.
from: http://code-ronin.com/articles/rails-migrations-directly-import-sql
1
2
3 class ActiveRecord::ConnectionAdapters::MysqlAdapter
4 def import_sql(file)
5 conf = ActiveRecord::Base.configurations[RAILS_ENV]
6 sql_file = File.join(RAILS_ROOT, 'db', file + '.sql')
7 cmd_line = "mysql -h "+conf["host"]+" -D "+conf["database"]+ " --user="+conf["username"]+" --password="+conf["password"]+" < "+sql_file
8 raise Exception, "Error executing " + cmd_line unless system(cmd_line)
9 end
10 end
11
12
13
14
15 ./script/generate migration sql_external_test
16
17
18
19 class SqlExternalTest < ActiveRecord::Migration
20 def self.up
21 import_sql("test")
22 end
23
24 def self.down
25 end
26 end
27
class ActiveRecord::ConnectionAdapters::MysqlAdapter
def import_sql(file)
conf = ActiveRecord::Base.configurations[RAILS_ENV]
sql_file = File.join(RAILS_ROOT, 'db', file + '.sql')
cmd_line = "mysql -h "+conf["host"]+" -D "+conf["database"]+ " --user="+conf["username"]+" --password="+conf["password"]+" < "+sql_file
raise Exception, "Error executing " + cmd_line unless system(cmd_line)
end
end
./script/generate migration sql_external_test
class SqlExternalTest < ActiveRecord::Migration
def self.up
import_sql("test")
end
def self.down
end
end
Send this to a friend
1 create_table :table do |t|
2 t.column
3 t.index
4 t.timestamps
5 t.change
6 t.change_default
7 t.rename
8 t.references
9 t.belongs_to
10 t.string
11 t.text
12 t.integer
13 t.float
14 t.decimal
15 t.datetime
16 t.timestamp
17 t.time
18 t.date
19 t.binary
20 t.boolean
21 t.remove
22 t.remove_references
23 t.remove_belongs_to
24 t.remove_index
25 t.remove_timestamps
26 end
create_table :table do |t|
t.column
t.index
t.timestamps
t.change
t.change_default
t.rename
t.references
t.belongs_to
t.string
t.text
t.integer
t.float
t.decimal
t.datetime
t.timestamp
t.time
t.date
t.binary
t.boolean
t.remove
t.remove_references
t.remove_belongs_to
t.remove_index
t.remove_timestamps
end
Send this to a friend
set in your environment.rb
1 config.active_record.timestamped_migrations = false
config.active_record.timestamped_migrations = false