Class: DataMigrate::DatabaseTasks

Inherits:
Object
  • Object
show all
Extended by:
ActiveRecord::Tasks::DatabaseTasks
Defined in:
lib/data_migrate/database_tasks.rb

Overview

This class extends DatabaseTasks to add a schema_file method.

Class Method Summary collapse

Class Method Details

.check_schema_file(filename) ⇒ Object



26
27
28
29
30
31
# File 'lib/data_migrate/database_tasks.rb', line 26

def check_schema_file(filename)
  unless File.exist?(filename)
    message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
    Kernel.abort message
  end
end

.data_migrations_pathObject



48
49
50
# File 'lib/data_migrate/database_tasks.rb', line 48

def data_migrations_path
  ::DataMigrate.config.data_migrations_path
end

.dump_filename(spec_name, format = ActiveRecord::Base.schema_format) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/data_migrate/database_tasks.rb', line 16

def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
  filename = if spec_name == "primary"
    schema_file_type(format)
  else
    "#{spec_name}_#{schema_file_type(format)}"
  end

  ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
end

.forward(step = 1) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/data_migrate/database_tasks.rb', line 67

def self.forward(step = 1)
  DataMigrate::DataMigrator.assure_data_schema_table
  migrations = pending_migrations.reverse.pop(step).reverse
  migrations.each do | pending_migration |
    if pending_migration[:kind] == :data
      ActiveRecord::Migration.write("== %s %s" % ["Data", "=" * 71])
      DataMigrate::DataMigrator.run(:up, data_migrations_path, pending_migration[:version])
    elsif pending_migration[:kind] == :schema
      ActiveRecord::Migration.write("== %s %s" % ["Schema", "=" * 69])
      DataMigrate::SchemaMigration.run(:up, DataMigrate::SchemaMigration.migrations_paths, pending_migration[:version])
    end
  end
end

.past_migrations(sort = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/data_migrate/database_tasks.rb', line 91

def self.past_migrations(sort = nil)
  data_versions = DataMigrate::DataSchemaMigration.table_exists? ? DataMigrate::DataSchemaMigration.normalized_versions : []
  schema_versions = ActiveRecord::SchemaMigration.normalized_versions
  migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }

  sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
end

.pending_data_migrationsObject



81
82
83
84
85
# File 'lib/data_migrate/database_tasks.rb', line 81

def self.pending_data_migrations
  data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
  sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
    pending_migrations.map {|m| { version: m.version, name: m.name, kind: :data }})
end

.pending_migrationsObject



33
34
35
36
37
38
# File 'lib/data_migrate/database_tasks.rb', line 33

def pending_migrations
  sort_migrations(
    pending_schema_migrations,
    pending_data_migrations
  )
end

.pending_schema_migrationsObject



87
88
89
# File 'lib/data_migrate/database_tasks.rb', line 87

def self.pending_schema_migrations
  ::DataMigrate::SchemaMigration.pending_schema_migrations
end

.run_migration(migration, direction) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/data_migrate/database_tasks.rb', line 52

def run_migration(migration, direction)
  if migration[:kind] == :data
    ::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
    ::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
  else
    ::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
    ::DataMigrate::SchemaMigration.run(
      direction,
      ::DataMigrate::SchemaMigration.migrations_paths,
      migration[:version]
    )
  end
end

.schema_file_type(_format = nil) ⇒ Object



12
13
14
# File 'lib/data_migrate/database_tasks.rb', line 12

def schema_file_type(_format = nil)
  "data_schema.rb"
end

.sort_migrations(*migrations) ⇒ Object



40
41
42
# File 'lib/data_migrate/database_tasks.rb', line 40

def sort_migrations(*migrations)
  migrations.flatten.sort { |a, b|  sort_string(a) <=> sort_string(b) }
end

.sort_string(migration) ⇒ Object



44
45
46
# File 'lib/data_migrate/database_tasks.rb', line 44

def sort_string migration
  "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
end