14
14
context 'generator' do
15
15
let ( :migration_name ) { 'test_migration' }
16
16
17
- let ( :file_name ) { 'spec/db/data-migrations/20161031000000_test_migration.rb' }
17
+ context 'regular migration' do
18
+ let ( :file_name ) { 'spec/db/data-migrations/20161031000000_test_migration.rb' }
18
19
19
- before ( :each ) do
20
- allow ( Time ) . to receive ( :now ) . and_return ( Time . utc ( 2016 , 10 , 31 ) )
21
- Rails ::Generators . invoke ( 'data_migration' , [ migration_name ] )
22
- end
20
+ before ( :each ) do
21
+ allow ( Time ) . to receive ( :now ) . and_return ( Time . utc ( 2016 , 10 , 31 ) )
22
+ Rails ::Generators . invoke ( 'data_migration' , [ migration_name ] )
23
+ end
24
+
25
+ it 'creates non-empty migration file' do
26
+ expect ( File . exist? ( file_name ) ) . to be_truthy
27
+ expect ( File . size ( file_name ) ) . to be > 0
28
+ end
23
29
24
- it 'creates non-empty migration file' do
25
- expect ( File . exist? ( file_name ) ) . to be_truthy
26
- expect ( File . size ( file_name ) ) . to be > 0
30
+ it 'creates valid migration class' do
31
+ # rubocop:disable Security/Eval
32
+ eval ( File . open ( file_name ) . read )
33
+ # rubocop:enable Security/Eval
34
+ klass = migration_name . classify . constantize
35
+ expect ( klass . superclass ) . to eq ( ActiveRecord ::DataMigration )
36
+ expect ( klass . instance_methods ( false ) ) . to eq ( [ :up ] )
37
+ end
27
38
end
28
39
29
- it 'creates valid migration class' do
30
- # rubocop:disable Security/Eval
31
- eval ( File . open ( file_name ) . read )
32
- # rubocop:enable Security/Eval
33
- klass = migration_name . classify . constantize
34
- expect ( klass . superclass ) . to eq ( ActiveRecord ::DataMigration )
35
- expect ( klass . instance_methods ( false ) ) . to eq ( [ :up ] )
40
+ context 'pre migration' do
41
+ let ( :file_name ) { 'spec/db/data-migrations/20190131000000_pre_test_migration.rb' }
42
+
43
+ before ( :each ) do
44
+ allow ( Time ) . to receive ( :now ) . and_return ( Time . utc ( 2019 , 1 , 31 ) )
45
+ Rails ::Generators . invoke ( 'pre_data_migration' , [ migration_name ] )
46
+ end
47
+
48
+ it 'creates non-empty pre migration file' do
49
+ expect ( File . exist? ( file_name ) ) . to be_truthy
50
+ expect ( File . size ( file_name ) ) . to be > 0
51
+ end
52
+
53
+ it 'creates valid pre migration class' do
54
+ # rubocop:disable Security/Eval
55
+ eval ( File . open ( file_name ) . read )
56
+ # rubocop:enable Security/Eval
57
+ klass = "pre_#{ migration_name } " . classify . constantize
58
+ expect ( klass . superclass ) . to eq ( ActiveRecord ::DataMigration )
59
+ expect ( klass . instance_methods ( false ) ) . to eq ( [ :up ] )
60
+ end
36
61
end
37
62
end
38
63
39
64
context 'migrator' do
40
65
before ( :each ) do
41
- allow ( Time ) . to receive ( :now ) . and_return ( Time . utc ( 2016 , 11 , 1 , 2 , 3 , 4 ) )
66
+ allow ( Time ) . to receive ( :now ) . and_return (
67
+ Time . utc ( 2016 , 11 , 1 , 2 , 3 , 4 ) ,
68
+ Time . utc ( 2019 , 1 , 1 , 2 , 3 , 14 )
69
+ )
42
70
43
71
Rails ::Generators . invoke ( 'data_migration' , [ 'test' ] )
72
+ Rails ::Generators . invoke ( 'pre_data_migration' , [ 'test' ] )
44
73
end
45
74
46
75
def load_rake_rasks
@@ -54,7 +83,7 @@ def load_rake_rasks
54
83
end
55
84
56
85
it 'list migration file' do
57
- expect ( RailsDataMigrations ::Migrator . list_migrations . size ) . to eq ( 1 )
86
+ expect ( RailsDataMigrations ::Migrator . list_migrations . size ) . to eq ( 2 )
58
87
end
59
88
60
89
it 'applies pending migrations only once' do
@@ -69,6 +98,17 @@ def load_rake_rasks
69
98
end
70
99
end
71
100
101
+ it 'applies pending pre migrations' do
102
+ expect ( RailsDataMigrations ::LogEntry . count ) . to eq ( 0 )
103
+
104
+ load_rake_rasks
105
+
106
+ Rake ::Task [ 'data:migrate:pre' ] . execute
107
+
108
+ expect ( RailsDataMigrations ::Migrator . current_version ) . to eq ( 20190101020314 )
109
+ expect ( RailsDataMigrations ::LogEntry . count ) . to eq ( 1 )
110
+ end
111
+
72
112
it 'requires VERSION to run a single migration' do
73
113
ENV [ 'VERSION' ] = nil
74
114
0 commit comments