Class: DataMigrate::MigrationContext

Inherits:
ActiveRecord::MigrationContext
  • Object
show all
Defined in:
lib/data_migrate/migration_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(migrations_paths = "db/data") ⇒ MigrationContext

Returns a new instance of MigrationContext.



3
4
5
# File 'lib/data_migrate/migration_context.rb', line 3

def initialize(migrations_paths = "db/data")
  @migrations_paths = migrations_paths || "db/data"
end

Instance Method Details

#current_versionObject



35
36
37
38
# File 'lib/data_migrate/migration_context.rb', line 35

def current_version
  get_all_versions.max || 0
rescue ActiveRecord::NoDatabaseError
end

#down(target_version = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/data_migrate/migration_context.rb', line 18

def down(target_version = nil)
  selected_migrations =
    if block_given?
      migrations.select { |m| yield m }
    else
      migrations
    end

  data_migrator = DataMigrate::RailsHelper.data_migrator(:down, selected_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper., target_version)
  data_migrator.migrate
end

#migration_filesObject



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

def migration_files
  paths = Array(migrations_paths)
  Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
end

#migrations_statusObject



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

def migrations_status
  db_list = DataMigrate::RailsHelper.data_schema_migration.normalized_versions

  file_list = migration_files.map do |file|
    version, name, scope = parse_migration_filename(file)
    raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
    version = DataMigrate::RailsHelper.schema_migration.normalize_migration_number(version)
    status = db_list.delete(version) ? "up" : "down"
    [status, version, (name + scope).humanize]
  end.compact

  db_list.map! do |version|
    ["up", version, "********** NO FILE **********"]
  end

  (db_list + file_list).sort_by { |_, version, _| version }
end

#run(direction, target_version) ⇒ Object



30
31
32
33
# File 'lib/data_migrate/migration_context.rb', line 30

def run(direction, target_version)
  data_migrator = DataMigrate::RailsHelper.data_migrator(direction, migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper., target_version)
  data_migrator.run
end

#up(target_version = nil) ⇒ Object



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

def up(target_version = nil)
  selected_migrations = if block_given?
                          migrations.select { |m| yield m }
                        else
                          migrations
                        end

  data_migrator = DataMigrate::RailsHelper.data_migrator(:up, selected_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper., target_version)
  data_migrator.migrate
end