Class: RailsDataMigrations::Migrator

Inherits:
ActiveRecord::Migrator
  • Object
show all
Defined in:
lib/rails_data_migrations/migrator.rb

Constant Summary collapse

MIGRATOR_SALT =
2053462855

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_versionObject



31
32
33
# File 'lib/rails_data_migrations/migrator.rb', line 31

def current_version
  get_all_versions.max || 0
end

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



23
24
25
26
27
28
29
# File 'lib/rails_data_migrations/migrator.rb', line 23

def get_all_versions(connection = ActiveRecord::Base.connection)
  if migrations_table_exists?(connection)
    LogEntry.all.map { |x| x.version.to_i }.sort
  else
    []
  end
end

.list_migrationsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_data_migrations/migrator.rb', line 55

def list_migrations
  if rails_7_1?
    ::ActiveRecord::MigrationContext.new(
      migrations_path, ::ActiveRecord::Base.connection.schema_migration
    ).migrations
  elsif rails_6_0?
    ::ActiveRecord::MigrationContext.new(migrations_path, ::ActiveRecord::SchemaMigration).migrations
  elsif rails_5_2?
    ::ActiveRecord::MigrationContext.new(migrations_path).migrations
  else
    migrations(migrations_path)
  end
end

.list_pending_migrationsObject



69
70
71
72
73
74
75
76
# File 'lib/rails_data_migrations/migrator.rb', line 69

def list_pending_migrations
  if rails_5_2?
    already_migrated = get_all_versions
    list_migrations.reject { |m| already_migrated.include?(m.version) }
  else
    open(migrations_path).pending_migrations # rubocop:disable Security/Open
  end
end

.migrations_pathObject



39
40
41
# File 'lib/rails_data_migrations/migrator.rb', line 39

def migrations_path
  'db/data_migrations'
end

.migrations_table_exists?(connection = ActiveRecord::Base.connection) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/rails_data_migrations/migrator.rb', line 18

def migrations_table_exists?(connection = ActiveRecord::Base.connection)
  table_check_method = connection.respond_to?(:data_source_exists?) ? :data_source_exists? : :table_exists?
  connection.send(table_check_method, schema_migrations_table_name)
end

.rails_5_2?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rails_data_migrations/migrator.rb', line 47

def rails_5_2?
  Rails::VERSION::MAJOR > 5 || (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 2)
end

.rails_6_0?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails_data_migrations/migrator.rb', line 43

def rails_6_0?
  Rails::VERSION::MAJOR >= 6
end

.rails_7_1?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rails_data_migrations/migrator.rb', line 51

def rails_7_1?
  Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1
end

.run_migration(direction, migrations_path, version) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_data_migrations/migrator.rb', line 78

def run_migration(direction, migrations_path, version)
  if rails_7_1?
    new(
      direction,
      list_migrations,
      ::ActiveRecord::Base.connection.schema_migration,
      ::ActiveRecord::InternalMetadata.new(ActiveRecord::Base.connection),
      version
    ).run
  elsif rails_6_0?
    new(direction, list_migrations, ::ActiveRecord::SchemaMigration, version).run
  elsif rails_5_2?
    new(direction, list_migrations, version).run
  else
    run(direction, migrations_path, version)
  end
end

.schema_migrations_table_nameObject



35
36
37
# File 'lib/rails_data_migrations/migrator.rb', line 35

def schema_migrations_table_name
  LogEntry.table_name
end

Instance Method Details

#record_version_state_after_migrating(version) ⇒ Object



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

def record_version_state_after_migrating(version)
  if down?
    migrated.delete(version)
    LogEntry.where(version: version.to_s).delete_all
  else
    migrated << version
    LogEntry.create!(version: version.to_s)
  end
end