2
2
3
3
module RailsDataMigrations
4
4
class Migrator < ::ActiveRecord ::Migrator
5
+ self . migrations_paths = [ ENV . fetch ( 'DATA_MIGRATIONS_PATH' , 'db/data_migrations' ) ]
6
+
5
7
MIGRATOR_SALT = 2053462855
6
8
7
9
def record_version_state_after_migrating ( version )
@@ -15,13 +17,8 @@ def record_version_state_after_migrating(version)
15
17
end
16
18
17
19
class << self
18
- def migrations_table_exists? ( connection = ActiveRecord ::Base . connection )
19
- table_check_method = connection . respond_to? ( :data_source_exists? ) ? :data_source_exists? : :table_exists?
20
- connection . send ( table_check_method , schema_migrations_table_name )
21
- end
22
-
23
- def get_all_versions ( connection = ActiveRecord ::Base . connection )
24
- if migrations_table_exists? ( connection )
20
+ def get_all_versions # rubocop:disable Naming/AccessorMethodName
21
+ if LogEntry . table_exists?
25
22
LogEntry . all . map { |x | x . version . to_i } . sort
26
23
else
27
24
[ ]
@@ -36,61 +33,25 @@ def schema_migrations_table_name
36
33
LogEntry . table_name
37
34
end
38
35
39
- def migrations_path
40
- ENV . fetch ( 'DATA_MIGRATIONS_PATH' , 'db/data_migrations' )
41
- end
42
-
43
- def rails_6_0?
44
- Rails ::VERSION ::MAJOR >= 6
45
- end
46
-
47
- def rails_5_2?
48
- Rails ::VERSION ::MAJOR > 5 || ( Rails ::VERSION ::MAJOR == 5 && Rails ::VERSION ::MINOR >= 2 )
49
- end
50
-
51
- def rails_7_1?
52
- Rails ::VERSION ::MAJOR == 7 && Rails ::VERSION ::MINOR >= 1
53
- end
54
-
55
36
def list_migrations
56
- if rails_7_1?
57
- ::ActiveRecord ::MigrationContext . new (
58
- migrations_path , ::ActiveRecord ::Base . connection . schema_migration
59
- ) . migrations
60
- elsif rails_6_0?
61
- ::ActiveRecord ::MigrationContext . new ( migrations_path , ::ActiveRecord ::SchemaMigration ) . migrations
62
- elsif rails_5_2?
63
- ::ActiveRecord ::MigrationContext . new ( migrations_path ) . migrations
64
- else
65
- migrations ( migrations_path )
66
- end
37
+ ::ActiveRecord ::MigrationContext . new ( migrations_path ) . migrations
67
38
end
68
39
69
40
def list_pending_migrations
70
- if rails_5_2?
71
- already_migrated = get_all_versions
72
- list_migrations . reject { |m | already_migrated . include? ( m . version ) }
73
- else
74
- open ( migrations_path ) . pending_migrations # rubocop:disable Security/Open
75
- end
76
- end
77
-
78
- def run_migration ( direction , migrations_path , version )
79
- if rails_7_1?
80
- new (
81
- direction ,
82
- list_migrations ,
83
- ::ActiveRecord ::Base . connection . schema_migration ,
84
- ::ActiveRecord ::InternalMetadata . new ( ActiveRecord ::Base . connection ) ,
85
- version
86
- ) . run
87
- elsif rails_6_0?
88
- new ( direction , list_migrations , ::ActiveRecord ::SchemaMigration , version ) . run
89
- elsif rails_5_2?
90
- new ( direction , list_migrations , version ) . run
91
- else
92
- run ( direction , migrations_path , version )
93
- end
41
+ already_migrated = get_all_versions
42
+ list_migrations . reject { |m | already_migrated . include? ( m . version ) }
43
+ end
44
+
45
+ def run_migration ( direction , version )
46
+ connection_pool = ActiveRecord ::Tasks ::DatabaseTasks . migration_connection_pool
47
+ internal_metadata = ActiveRecord ::InternalMetadata . new ( connection_pool )
48
+ new (
49
+ direction ,
50
+ list_migrations ,
51
+ LogEntry . schema_migration_instance ,
52
+ internal_metadata ,
53
+ version
54
+ ) . run
94
55
end
95
56
end
96
57
end
0 commit comments