Posts Tagged ‘playground’

rails data migrations, part 1

class DataMigration < ActiveRecord::Migration

  class << self

    ERROR = 'Snusnu::DataMigration ERROR'
    WARNING = 'Snusnu::DataMigration WARNING'
    INFO = 'Snusnu::DataMigration INFO'

    def load_fixtures(file_name, fixtures_folder = 'data')
      unless file_name.is_a?(String) || file_name.is_a?(Symbol)
        raise ArgumentError, "#{ERROR} - file_name must be String or Symbol"
      end
      require 'active_record/fixtures'
      data_migration_path = "#{RAILS_ROOT}/db/migrate/#{fixtures_folder}"
      Fixtures.create_fixtures(data_migration_path, file_name.to_s)
      say "#{INFO}"
      say "Successfully loaded #{data_migration_path}/#{file_name.to_s}.yml", true
    rescue Errno::ENOENT => e
      say "#{WARNING}"
      say e.message, true
    end

  end

end

class CreateMyMigration < Snusnu::DataMigration
  def self.up
    create_table :people, :force => true do |t|
      #...
    end
    load_fixtures :people #, :folder => "fixtures"
  end
end

Newer Entries »