File: migrator_test.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (262 lines) | stat: -rw-r--r-- 13,822 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require_relative "spec_helper"

Sequel.extension :migration
describe Sequel::Migrator do
  before do
    @db = DB
    @m = Sequel::Migrator
  end
  after do
    @db.drop_table?(:schema_info, :schema_migrations, :sm1111, :sm1122, :sm2222, :sm2233, :sm3333, :sm11111, :sm22222, :a, :b, :c, :d)
  end
  
  it "should be able to migrate up and down all the way successfully" do
    @dir = 'spec/files/integer_migrations'
    @m.apply(@db, @dir)
    [:schema_info, :sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_info].get(:version).must_equal 3
    @m.apply(@db, @dir, 0)
    [:sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_info].get(:version).must_equal 0
  end
  
  it "should be able to migrate up and down to specific versions successfully" do
    @dir = 'spec/files/integer_migrations'
    @m.apply(@db, @dir, 2)
    [:schema_info, :sm1111, :sm2222].each{|n| @db.table_exists?(n).must_equal true}
    @db.table_exists?(:sm3333).must_equal false
    @db[:schema_info].get(:version).must_equal 2
    @m.apply(@db, @dir, 1)
    [:sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db.table_exists?(:sm1111).must_equal true
    @db[:schema_info].get(:version).must_equal 1
  end

  it "should correctly set migration version to the last successful migration if the migration raises an error when migrating up" do
    @dir = 'spec/files/bad_up_migration'
    proc{@m.apply(@db, @dir)}.must_raise Sequel::DatabaseError
    [:schema_info, :sm11111].each{|n| @db.table_exists?(n).must_equal true}
    @db.table_exists?(:sm22222).must_equal false
    @db[:schema_info].get(:version).must_equal 1
    @m.apply(@db, @dir, 0)
    [:sm11111, :sm22222].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_info].get(:version).must_equal 0
  end

  it "should correctly set migration version to the last successful migration if the migration raises an error when migrating down" do
    @dir = 'spec/files/bad_down_migration'
    @m.apply(@db, @dir)
    [:schema_info, :sm11111, :sm22222].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_info].get(:version).must_equal 2
    proc{@m.apply(@db, @dir, 0)}.must_raise Sequel::DatabaseError
    [:sm22222].each{|n| @db.table_exists?(n).must_equal false}
    @db.table_exists?(:sm11111).must_equal true
    @db[:schema_info].get(:version).must_equal 1
  end

  it "should handle migrating up or down all the way with timestamped migrations" do
    @dir = 'spec/files/timestamped_migrations'
    @m.apply(@db, @dir)
    [:schema_migrations, :sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253851_create_nodes.rb 1273253853_3_create_users.rb'
    @m.apply(@db, @dir, 0)
    [:sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal []
  end

  it "should handle migrating up or down to specific timestamps with timestamped migrations" do
    @dir = 'spec/files/timestamped_migrations'
    @m.apply(@db, @dir, 1273253851)
    [:schema_migrations, :sm1111, :sm2222].each{|n| @db.table_exists?(n).must_equal true}
    @db.table_exists?(:sm3333).must_equal false
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253851_create_nodes.rb'
    @m.apply(@db, @dir, 1273253849)
    [:sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db.table_exists?(:sm1111).must_equal true
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb'
  end

  it "should apply all missing files when migrating up with timestamped migrations" do
    @dir = 'spec/files/timestamped_migrations'
    @m.apply(@db, @dir)
    @dir = 'spec/files/interleaved_timestamped_migrations'
    @m.apply(@db, @dir)
    [:schema_migrations, :sm1111, :sm1122, :sm2222, :sm2233, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253850_create_artists.rb 1273253851_create_nodes.rb 1273253852_create_albums.rb 1273253853_3_create_users.rb'
  end

  it "should not apply down action to migrations where up action hasn't been applied" do
    @dir = 'spec/files/timestamped_migrations'
    @m.apply(@db, @dir)
    @dir = 'spec/files/interleaved_timestamped_migrations'
    @m.apply(@db, @dir, 0)
    [:sm1111, :sm1122, :sm2222, :sm2233, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal []
  end

  it "should handle updating to a specific timestamp when interleaving migrations with timestamps" do
    @dir = 'spec/files/timestamped_migrations'
    @m.apply(@db, @dir)
    @dir = 'spec/files/interleaved_timestamped_migrations'
    @m.apply(@db, @dir, 1273253851)
    [:schema_migrations, :sm1111, :sm1122, :sm2222].each{|n| @db.table_exists?(n).must_equal true}
    [:sm2233, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253850_create_artists.rb 1273253851_create_nodes.rb'
  end

  it "should correctly update schema_migrations table when an error occurs when migrating up or down using timestamped migrations" do
    @dir = 'spec/files/bad_timestamped_migrations'
    proc{@m.apply(@db, @dir)}.must_raise Sequel::DatabaseError
    [:schema_migrations, :sm1111, :sm2222].each{|n| @db.table_exists?(n).must_equal true}
    @db.table_exists?(:sm3333).must_equal false
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253851_create_nodes.rb'
    proc{@m.apply(@db, @dir, 0)}.must_raise Sequel::DatabaseError
    [:sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db.table_exists?(:sm1111).must_equal true
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb'
  end

  it "should handle multiple migrations with the same timestamp correctly" do
    @dir = 'spec/files/duplicate_timestamped_migrations'
    @m.apply(@db, @dir)
    [:schema_migrations, :sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253853_create_nodes.rb 1273253853_create_users.rb'
    @m.apply(@db, @dir, 1273253853)
    [:sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb 1273253853_create_nodes.rb 1273253853_create_users.rb'
    @m.apply(@db, @dir, 1273253849)
    [:sm1111].each{|n| @db.table_exists?(n).must_equal true}
    [:sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'1273253849_create_sessions.rb'
    @m.apply(@db, @dir, 1273253848)
    [:sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal []
  end

  it "should convert schema_info table to schema_migrations table" do
    @dir = 'spec/files/integer_migrations'
    @m.apply(@db, @dir)
    [:schema_info, :sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal false}

    @dir = 'spec/files/convert_to_timestamp_migrations'
    @m.apply(@db, @dir)
    [:schema_info, :sm1111, :sm2222, :sm3333, :schema_migrations, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'001_create_sessions.rb 002_create_nodes.rb 003_3_create_users.rb 1273253850_create_artists.rb 1273253852_create_albums.rb'

    @m.apply(@db, @dir, 4)
    [:schema_info, :schema_migrations, :sm1111, :sm2222, :sm3333].each{|n| @db.table_exists?(n).must_equal true}
    [:sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'001_create_sessions.rb 002_create_nodes.rb 003_3_create_users.rb'

    @m.apply(@db, @dir, 0)
    [:schema_info, :schema_migrations].each{|n| @db.table_exists?(n).must_equal true}
    [:sm1111, :sm2222, :sm3333, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal []
  end

  it "should handle unapplied migrations when migrating schema_info table to schema_migrations table" do
    @dir = 'spec/files/integer_migrations'
    @m.apply(@db, @dir, 2)
    [:schema_info, :sm1111, :sm2222].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :sm3333, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal false}

    @dir = 'spec/files/convert_to_timestamp_migrations'
    @m.apply(@db, @dir, 1273253850)
    [:schema_info, :sm1111, :sm2222, :sm3333, :schema_migrations, :sm1122].each{|n| @db.table_exists?(n).must_equal true}
    [:sm2233].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'001_create_sessions.rb 002_create_nodes.rb 003_3_create_users.rb 1273253850_create_artists.rb'
  end

  it "should handle unapplied migrations when migrating schema_info table to schema_migrations table and target is less than last integer migration version" do
    @dir = 'spec/files/integer_migrations'
    @m.apply(@db, @dir, 1)
    [:schema_info, :sm1111].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :sm2222, :sm3333, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal false}

    @dir = 'spec/files/convert_to_timestamp_migrations'
    @m.apply(@db, @dir, 2)
    [:schema_info, :sm1111, :sm2222, :schema_migrations].each{|n| @db.table_exists?(n).must_equal true}
    [:sm3333, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal false}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'001_create_sessions.rb 002_create_nodes.rb'

    @m.apply(@db, @dir)
    [:schema_info, :sm1111, :sm2222, :schema_migrations, :sm3333, :sm1122, :sm2233].each{|n| @db.table_exists?(n).must_equal true}
    @db[:schema_migrations].select_order_map(:filename).must_equal %w'001_create_sessions.rb 002_create_nodes.rb 003_3_create_users.rb 1273253850_create_artists.rb 1273253852_create_albums.rb'
  end

  it "should handle reversible migrations" do
    @dir = 'spec/files/reversible_migrations'
    @db.drop_table?(:a, :b)
    @m.apply(@db, @dir, 1)
    [:schema_info, :a].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :b].each{|n| @db.table_exists?(n).must_equal false}
    @db[:a].columns.must_equal [:a]

    @m.apply(@db, @dir, 2)
    [:schema_info, :a].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :b].each{|n| @db.table_exists?(n).must_equal false}
    @db[:a].columns.must_equal [:a, :b]

    @m.apply(@db, @dir, 3)
    [:schema_info, :a].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :b].each{|n| @db.table_exists?(n).must_equal false}
    @db[:a].columns.must_equal [:a, :c]

    @m.apply(@db, @dir, 4)
    [:schema_info, :b].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :a].each{|n| @db.table_exists?(n).must_equal false}
    @db[:b].columns.must_equal [:a, :c]

    @m.apply(@db, @dir, 5)
    [:schema_info, :b].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :a].each{|n| @db.table_exists?(n).must_equal false}
    @db[:b].columns.must_equal [:a, :c, :e]

    if @db.supports_foreign_key_parsing?
      @m.apply(@db, @dir, 6)
      [:schema_info, :b, :c].each{|n| @db.table_exists?(n).must_equal true}
      [:schema_migrations, :a].each{|n| @db.table_exists?(n).must_equal false}
      @db[:b].columns.must_equal [:a, :c, :e, :f]

      @m.apply(@db, @dir, 7)
      [:schema_info, :b, :c, :d].each{|n| @db.table_exists?(n).must_equal true}
      [:schema_migrations, :a].each{|n| @db.table_exists?(n).must_equal false}
      @db[:b].columns.must_equal [:a, :c, :e, :f, :g]

      @m.apply(@db, @dir, 6)
      [:schema_info, :b, :c].each{|n| @db.table_exists?(n).must_equal true}
      [:schema_migrations, :a, :d].each{|n| @db.table_exists?(n).must_equal false}
      @db[:b].columns.must_equal [:a, :c, :e, :f]
    end

    @m.apply(@db, @dir, 5)
    [:schema_info, :b].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :a, :c].each{|n| @db.table_exists?(n).must_equal false}
    @db[:b].columns.must_equal [:a, :c, :e]

    @m.apply(@db, @dir, 4)
    [:schema_info, :b].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :a].each{|n| @db.table_exists?(n).must_equal false}
    @db[:b].columns.must_equal [:a, :c]

    @m.apply(@db, @dir, 3)
    [:schema_info, :a].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :b].each{|n| @db.table_exists?(n).must_equal false}
    @db[:a].columns.must_equal [:a, :c]

    @m.apply(@db, @dir, 2)
    [:schema_info, :a].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :b].each{|n| @db.table_exists?(n).must_equal false}
    @db[:a].columns.must_equal [:a, :b]

    @m.apply(@db, @dir, 1)
    [:schema_info, :a].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :b].each{|n| @db.table_exists?(n).must_equal false}
    @db[:a].columns.must_equal [:a]

    @m.apply(@db, @dir, 0)
    [:schema_info].each{|n| @db.table_exists?(n).must_equal true}
    [:schema_migrations, :a, :b].each{|n| @db.table_exists?(n).must_equal false}
  end
end