Class: DataMigrate::DataMigrator

Inherits:
ActiveRecord::Migrator
  • Object
show all
Defined in:
lib/data_migrate/data_migrator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assure_data_schema_tableObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/data_migrate/data_migrator.rb', line 43

def assure_data_schema_table
  config = ActiveRecord::Base.configurations[Rails.env || 'development'] || ENV["DATABASE_URL"]
  ActiveRecord::Base.establish_connection(config)
  sm_table = DataMigrate::DataMigrator.schema_migrations_table_name

  unless table_exists?(ActiveRecord::Base.connection, 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}"

    ActiveRecord::Base.connection.add_index sm_table, :version,
      :unique => true,
      :name => index_name
  end
end

.get_all_versions(connection = ActiveRecord::Base.connection) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/data_migrate/data_migrator.rb', line 18

def get_all_versions(connection = ActiveRecord::Base.connection)
  if table_exists?(connection, schema_migrations_table_name)
    # Certain versions of the gem wrote data migration versions into
    # schema_migrations table. After the fix, it was corrected to write into
    # data_migrations. However, not to break anything we are going to
    # get versions from both tables.
    #
    # This may cause some problems:
    # Eg. rake data:versions will show version from the schema_migrations table
    # which may be a version of actual schema migration and not data migration
    DataMigrate::DataSchemaMigration.all.map { |x| x.version.to_i }.sort +
      ActiveRecord::SchemaMigration.all.map { |x| x.version.to_i }.sort
  else
    []
  end
end

.migrations_pathObject



39
40
41
# File 'lib/data_migrate/data_migrator.rb', line 39

def migrations_path
  'db/data'
end

.schema_migrations_table_nameObject



35
36
37
# 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

Instance Method Details

#record_version_state_after_migrating(version) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/data_migrate/data_migrator.rb', line 7

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