class DataMigrate::DataMigrator

Public Class Methods

assure_data_schema_table() click to toggle source
# File lib/data_migrate/data_migrator.rb, line 51
def assure_data_schema_table
  ActiveRecord::Base.establish_connection(db_config)
  sm_table = DataMigrate::DataMigrator.schema_migrations_table_name

  unless table_exists?(ActiveRecord::Base.connection, sm_table)
    create_table(sm_table)
  end
end
current_version(connection = ActiveRecord::Base.connection) click to toggle source
# File lib/data_migrate/data_migrator.rb, line 23
def current_version(connection = ActiveRecord::Base.connection)
  get_all_versions(connection).max || 0
end
full_migrations_path() click to toggle source

Provides the full migrations_path filepath @return (String)

# File lib/data_migrate/data_migrator.rb, line 47
def full_migrations_path
  File.join(Rails.root, *migrations_path.split(File::SEPARATOR))
end
get_all_versions(connection = ActiveRecord::Base.connection) click to toggle source
# File lib/data_migrate/data_migrator.rb, line 27
def get_all_versions(connection = ActiveRecord::Base.connection)
  if table_exists?(connection, schema_migrations_table_name)
    DataMigrate::DataSchemaMigration.all.map { |x| x.version.to_i }.sort
  else
    []
  end
end
match(filename) click to toggle source

Compares the given filename with what we expect data migration filenames to be, eg the “20091231235959_some_name.rb” pattern @param (String) filename @return (MatchData)

# File lib/data_migrate/data_migrator.rb, line 65
def match(filename)
  /(\d{14})_(.+)\.rb/.match(filename)
end
migrations(_migrations_paths) click to toggle source

TODO: this was added to be backward compatible, need to re-evaluate

# File lib/data_migrate/data_migrator_five.rb, line 57
def migrations(_migrations_paths)
  DataMigrate::MigrationContext.new(migrations_paths).migrations
end
migrations_path() click to toggle source
# File lib/data_migrate/data_migrator.rb, line 40
def migrations_path
  "db/data"
end
migrations_status() click to toggle source
# File lib/data_migrate/data_migrator_five.rb, line 52
def migrations_status
  DataMigrate::MigrationContext.new(migrations_paths).migrations_status
end
new(direction, migrations, target_version = nil) click to toggle source
# File lib/data_migrate/data_migrator_five.rb, line 14
def initialize(direction, migrations, target_version = nil)
  @direction         = direction
  @target_version    = target_version
  @migrated_versions = nil
  @migrations        = migrations

  validate(@migrations)

  DataMigrate::DataSchemaMigration.create_table
  ActiveRecord::InternalMetadata.create_table
end
rollback(migrations_path, steps) click to toggle source
# File lib/data_migrate/data_migrator_five.rb, line 66
def rollback(migrations_path, steps)
  DataMigrate::MigrationContext.new(migrations_path).rollback(steps)
end
run(direction, migration_paths, version) click to toggle source

TODO: this was added to be backward compatible, need to re-evaluate

# File lib/data_migrate/data_migrator_five.rb, line 62
def run(direction, migration_paths, version)
  DataMigrate::MigrationContext.new(migration_paths).run(direction, version)
end
schema_migrations_table_name() click to toggle source
# File lib/data_migrate/data_migrator.rb, line 35
def schema_migrations_table_name
  ActiveRecord::Base.table_name_prefix + "data_migrations" +
    ActiveRecord::Base.table_name_suffix
end

Private Class Methods

create_table(sm_table) click to toggle source
# File lib/data_migrate/data_migrator.rb, line 71
def create_table(sm_table)
  ActiveRecord::Base.connection.create_table(sm_table, id: false) do |schema_migrations_table|
    schema_migrations_table.column :version, :string, null: false
  end

  suffix = ActiveRecord::Base.table_name_suffix
  prefix = ActiveRecord::Base.table_name_prefix
  index_name = "#{prefix}unique_data_migrations#{suffix}"

  options = {unique: true, name: index_name}
  options[:length] = 191 if ActiveRecord::Base.connection_config[:adapter] == "mysql2"

  ActiveRecord::Base.connection.add_index sm_table, :version, options
end
db_config() click to toggle source
# File lib/data_migrate/data_migrator.rb, line 98
def db_config
  ActiveRecord::Base.configurations[Rails.env || "development"] ||
    ENV["DATABASE_URL"]
end
table_exists?(connection, table_name) click to toggle source
# File lib/data_migrate/data_migrator.rb, line 86
def table_exists?(connection, table_name)
  # Avoid the warning that table_exists? prints in Rails 5.0 due a
  # change in behavior between Rails 5.0 and Rails 5.1 of this method
  # with respect to database views.
  if ActiveRecord.version >= Gem::Version.new("5.0") &&
     ActiveRecord.version < Gem::Version.new("5.1")
    connection.data_source_exists?(table_name)
  else
    connection.table_exists?(schema_migrations_table_name)
  end
end

Public Instance Methods

load_migrated(connection = ActiveRecord::Base.connection) click to toggle source
# File lib/data_migrate/data_migrator.rb, line 18
def load_migrated(connection = ActiveRecord::Base.connection)
  self.class.get_all_versions(connection)
end
record_version_state_after_migrating(version) click to toggle source
# File lib/data_migrate/data_migrator.rb, line 8
def record_version_state_after_migrating(version)
  if down?
    migrated.delete(version)
    DataMigrate::DataSchemaMigration.where(version: version.to_s).delete_all
  else
    migrated << version
    DataMigrate::DataSchemaMigration.create!(version: version.to_s)
  end
end