you are in: codestackercodes [RSS]import sql files into migrations

import sql files into migrations Delicious Email

it’s a small how to import a file in migrations.

from: http://code-ronin.com/articles/rails-migrations-directly-import-sql

show/hide lines
   1  # first, create the method import_sql:
   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  # create a migration. ex:
  14  
  15  ./script/generate migration sql_external_test
  16  
  17  # save your sql file with sql commands in db/. migration's sample:
  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  
created by leozera — 02 February 2009 — get a short url — tags: migrations rails ruby sql embed

Comments



We dont send spam :)

If checked, it's highlights your comment
simple_captcha.jpg
Are you a human? Type the code from the image