class DataMigrate::DataMigrator
Public Class Methods
assure_data_schema_table()
click to toggle source
# File lib/data_migrate/data_migrator.rb, line 57 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 29 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 53 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 33 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 71 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 63 def migrations(_migrations_paths) #DataMigrate::MigrationContext.new(migrations_paths).migrations DataMigrate::MigrationContext.new(_migrations_paths).migrations end
migrations_path()
click to toggle source
# File lib/data_migrate/data_migrator.rb, line 46 def migrations_path DataMigrate.config.data_migrations_path end
migrations_paths()
click to toggle source
# File lib/data_migrate/data_migrator_five.rb, line 8 def self.migrations_paths [DataMigrate.config.data_migrations_path] end
migrations_status()
click to toggle source
# File lib/data_migrate/data_migrator.rb, line 25 def migrations_status migrations_status_orig([DataMigrate.config.data_migrations_path]) end
Also aliased as: migrations_status_orig
needs_migration?()
click to toggle source
# File lib/data_migrate/data_migrator_five.rb, line 48 def needs_migration? DataMigrate::DatabaseTasks.pending_migrations.count.positive? end
new(direction, migrations, target_version = nil)
click to toggle source
# File lib/data_migrate/data_migrator_five.rb, line 17 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 73 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 69 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 41 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 77 def create_table(sm_table) ActiveRecord::Base.connection.create_table(sm_table, id: false) do |schema_migrations_table| schema_migrations_table.string :version, primary_key: true end end
db_config()
click to toggle source
# File lib/data_migrate/data_migrator.rb, line 95 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 83 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