Class: ReeMigrator::ApplyMigrations

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_migrator/package/ree_migrator/functions/apply_migrations.rb

Constant Summary collapse

RUBY_EXT =
'*.rb'
DATA =
'data'
SCHEMA =
'schema'
InvalidMigrationYmlErr =
Class.new(StandardError)
MigrationNotFoundErr =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(connection, migrations_yml_path, schema_migrations_path, data_migrations_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ree_lib/packages/ree_migrator/package/ree_migrator/functions/apply_migrations.rb', line 26

def call(connection, migrations_yml_path, schema_migrations_path, data_migrations_path)
  logger.info("Parsing migrations.yml from #{migrations_yml_path}")

  migrations = YAML.load(File.read(migrations_yml_path))
  return [] if is_blank(migrations)

  applied_schema_migrations = indexed_migrations(connection, SCHEMA)
  applied_data_migrations = indexed_migrations(connection, DATA)
  schema_migrations = Dir.glob(File.join(schema_migrations_path, RUBY_EXT))
  data_migrations  = Dir.glob(File.join(data_migrations_path, RUBY_EXT))

  migrations = migrations.map do |migration|
    if !migration.is_a?(Hash) || !(migration.keys - [SCHEMA, DATA]).empty?
      raise InvalidMigrationYmlErr.new(
        "Invalid migrations.yml. Example of valid format:\n- schema: SCHEMA_MIGRATION_FILE_NAME.rb\n- data: DATA_MIGRATION_FILE_NAME.rb"
      )
    end

    migration_path = if migration.has_key?(SCHEMA)
      run_migration(
        connection,
        :schema,
        migration[SCHEMA],
        applied_schema_migrations,
        schema_migrations,
        schema_migrations_path
      )
    elsif migration.has_key?(DATA)
      run_migration(
        connection,
        :data,
        migration[DATA],
        applied_data_migrations,
        data_migrations,
        data_migrations_path
      )
    end

    migration_path
  end.compact

  migrations
end