you are in: codestackercodes [RSS] → tag: migrations [RSS]

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

[rails] create_table methods Delicious Email

show/hide lines
   1    create_table :table do |t|
   2      t.column # adds an ordinary column. Ex: t.column(:name, :string)
   3      t.index # adds a new index.
   4      t.timestamps
   5      t.change # changes the column definition. Ex: t.change(:name, :string, :limit => 80)
   6      t.change_default # changes the column default value.
   7      t.rename # changes the name of the column.
   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
created by leozera — 14 December 2008 — get a short url — tags: migrations rails embed

migrations without timestamp Delicious Email

set in your environment.rb

show/hide lines
   1  config.active_record.timestamped_migrations = false
created by leozera — 26 November 2008 — get a short url — tags: migrations rails ruby embed