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



32
33
34
35
# File 'lib/data_migrate/migration_context.rb', line 32

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

#down(target_version = nil) ⇒ Object



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

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

  DataMigrator.new(:down, selected_migrations, target_version).migrate
end

#migration_filesObject



37
38
39
40
# File 'lib/data_migrate/migration_context.rb', line 37

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

#migrations_statusObject



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

def migrations_status
  db_list = DataSchemaMigration.normalized_versions

  file_list = migration_files.map do |file|
    version, name, scope = parse_migration_filename(file)
    raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
    version = ActiveRecord::SchemaMigration.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



28
29
30
# File 'lib/data_migrate/migration_context.rb', line 28

def run(direction, target_version)
  DataMigrator.new(direction, migrations, target_version).run
end

#up(target_version = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# 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

  DataMigrator.new(:up, selected_migrations, target_version).migrate
end