sequel-sequence 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +44 -8
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +14 -0
- data/CONTRIBUTING.md +111 -13
- data/README.md +33 -10
- data/Rakefile +34 -1
- data/lib/sequel/error.rb +6 -6
- data/lib/sequel/sequence/database/postgresql.rb +3 -2
- data/lib/sequel/sequence/database/server/mariadb.rb +79 -0
- data/lib/sequel/sequence/database/server/mysql.rb +185 -0
- data/lib/sequel/sequence/database/sqlite.rb +18 -24
- data/lib/sequel/sequence/database.rb +12 -12
- data/lib/sequel/sequence/database_ext_connection.rb +29 -0
- data/lib/sequel/sequence/version.rb +1 -1
- data/lib/sequel/sequence.rb +1 -9
- data/sequel-sequence.gemspec +3 -2
- data/test/mariadb_test_helper.rb +32 -0
- data/test/mock_test_helper.rb +5 -0
- data/test/mysql_test_helper.rb +16 -9
- data/test/sequel/mariadb_sequence_test.rb +182 -0
- data/test/sequel/mock_sequence_test.rb +100 -0
- data/test/sequel/mysql_sequence_test.rb +230 -41
- data/test/sequel/sqlite_sequence_test.rb +86 -12
- data/test/sqlite_test_helper.rb +10 -2
- data/test/test_helper.rb +1 -0
- metadata +13 -6
- data/lib/sequel/sequence/database/mysql.rb +0 -10
- data/lib/sequel/sequence/database/mysql2.rb +0 -81
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mock_test_helper'
|
4
|
+
|
5
|
+
class MockSequenceTest < Minitest::Test
|
6
|
+
test 'checks check_options with params' do
|
7
|
+
mocked_method = Minitest::Mock.new
|
8
|
+
some_instance = MockDB
|
9
|
+
|
10
|
+
mocked_method.expect :call, true, [Sequel::Database::DANGER_OPT_INCREMENT]
|
11
|
+
some_instance.stub :log_info, mocked_method do
|
12
|
+
some_instance.check_options({ increment: 2 })
|
13
|
+
end
|
14
|
+
mocked_method.verify
|
15
|
+
|
16
|
+
mocked_method.expect :call, true, [Sequel::Database::DANGER_OPT_INCREMENT]
|
17
|
+
some_instance.stub :log_info, mocked_method do
|
18
|
+
some_instance.check_options({ step: 2 })
|
19
|
+
end
|
20
|
+
mocked_method.verify
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'checks custom_sequence?' do
|
24
|
+
assert_raises Sequel::MethodNotAllowed do
|
25
|
+
MockDB.custom_sequence?(:position)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'checks check_sequences' do
|
30
|
+
assert_raises Sequel::MethodNotAllowed do
|
31
|
+
MockDB.check_sequences
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
test 'checks create_sequence' do
|
36
|
+
assert_raises Sequel::MethodNotAllowed do
|
37
|
+
MockDB.create_sequence(:position)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'checks drop_sequence' do
|
42
|
+
assert_raises Sequel::MethodNotAllowed do
|
43
|
+
MockDB.drop_sequence(:position)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
test 'checks quote_name' do
|
48
|
+
assert_raises Sequel::MethodNotAllowed do
|
49
|
+
MockDB.quote_name(:position)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test 'checks quote' do
|
54
|
+
assert_raises Sequel::MethodNotAllowed do
|
55
|
+
MockDB.quote(:position)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
test 'checks nextval_with_label' do
|
60
|
+
assert_raises Sequel::MethodNotAllowed do
|
61
|
+
MockDB.nextval_with_label(:position, 100)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
test 'checks nextval' do
|
66
|
+
assert_raises Sequel::MethodNotAllowed do
|
67
|
+
MockDB.nextval(:position)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'checks currval' do
|
72
|
+
assert_raises Sequel::MethodNotAllowed do
|
73
|
+
MockDB.currval(:position)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
test 'checks lastval' do
|
78
|
+
assert_raises Sequel::MethodNotAllowed do
|
79
|
+
MockDB.lastval(:position)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
test 'checks setval' do
|
84
|
+
assert_raises Sequel::MethodNotAllowed do
|
85
|
+
MockDB.setval(:position, 100)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
test 'checks build_exists_condition for a true condition' do
|
90
|
+
assert_equal Sequel::Database::IF_EXISTS, MockDB.build_exists_condition(true)
|
91
|
+
end
|
92
|
+
|
93
|
+
test 'checks build_exists_condition for a false condition' do
|
94
|
+
assert_equal Sequel::Database::IF_NOT_EXISTS, MockDB.build_exists_condition(false)
|
95
|
+
end
|
96
|
+
|
97
|
+
test 'checks build_exists_condition for a non boolean condition' do
|
98
|
+
assert_nil MockDB.build_exists_condition('')
|
99
|
+
end
|
100
|
+
end
|
@@ -12,13 +12,28 @@ class MysqlSequenceTest < Minitest::Test
|
|
12
12
|
test 'adds sequence with default values' do
|
13
13
|
with_migration do
|
14
14
|
def up
|
15
|
-
# create_sequence :position, {start: 1, increment: 1} - default values
|
15
|
+
# create_sequence :position, {start: 1, increment: 1, numeric_label: 0} - default values
|
16
16
|
create_sequence :position
|
17
17
|
end
|
18
18
|
end.up
|
19
19
|
|
20
|
-
assert_equal 1, MysqlDB.nextval(:position)
|
21
20
|
assert_equal 2, MysqlDB.nextval(:position)
|
21
|
+
assert_equal 3, MysqlDB.nextval(:position)
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'adds sequence with numeric_label' do
|
25
|
+
with_migration do
|
26
|
+
def up
|
27
|
+
# create_sequence :position, {start: 1, increment: 1, numeric_label: 0} - default values
|
28
|
+
create_sequence :position, numeric_label: 10
|
29
|
+
end
|
30
|
+
end.up
|
31
|
+
|
32
|
+
assert_equal 1, MysqlDB.currval(:position)
|
33
|
+
|
34
|
+
fiction_list = MysqlDB.fetch('SELECT fiction FROM position;').all
|
35
|
+
assert_equal 1, fiction_list.size
|
36
|
+
assert_equal 10, fiction_list.first[:fiction]
|
22
37
|
end
|
23
38
|
|
24
39
|
test 'adds sequence reader within model and its inherited class' do
|
@@ -28,15 +43,15 @@ class MysqlSequenceTest < Minitest::Test
|
|
28
43
|
end
|
29
44
|
end.up
|
30
45
|
|
31
|
-
class
|
46
|
+
class Stuff < Sequel::Model; end
|
32
47
|
|
33
|
-
assert_equal
|
34
|
-
assert_equal
|
48
|
+
assert_equal 2, Stuff.db.nextval('position')
|
49
|
+
assert_equal 3, Stuff.db.nextval('position')
|
35
50
|
|
36
|
-
class
|
51
|
+
class InheritedStuff < Stuff; end
|
37
52
|
|
38
|
-
assert_equal
|
39
|
-
assert_equal
|
53
|
+
assert_equal 4, InheritedStuff.db.nextval(:position)
|
54
|
+
assert_equal 5, InheritedStuff.db.nextval(:position)
|
40
55
|
end
|
41
56
|
|
42
57
|
test 'adds sequence starting at 100' do
|
@@ -46,30 +61,34 @@ class MysqlSequenceTest < Minitest::Test
|
|
46
61
|
end
|
47
62
|
end.up
|
48
63
|
|
49
|
-
assert_equal 100, MysqlDB.nextval(:position)
|
50
64
|
assert_equal 101, MysqlDB.nextval(:position)
|
65
|
+
assert_equal 102, MysqlDB.nextval(:position)
|
51
66
|
end
|
52
67
|
|
53
|
-
test 'adds sequence
|
68
|
+
test 'adds a sequence that we are trying to increase by a value greater than 1' do
|
69
|
+
@step = 4
|
54
70
|
with_migration do
|
55
71
|
def up
|
56
|
-
create_sequence :position, increment:
|
72
|
+
create_sequence :position, increment: 4
|
57
73
|
end
|
58
74
|
end.up
|
59
75
|
|
60
|
-
assert_equal
|
76
|
+
assert_equal 2, MysqlDB.nextval(:position)
|
61
77
|
assert_equal 3, MysqlDB.nextval(:position)
|
78
|
+
assert_operator (2 + @step), :>, MysqlDB.nextval(:position)
|
62
79
|
end
|
63
80
|
|
64
|
-
test 'adds sequence
|
81
|
+
test 'adds a sequence that we are trying to increase by a value greater than 1 (using :step alias)' do
|
82
|
+
@step = 4
|
65
83
|
with_migration do
|
66
84
|
def up
|
67
|
-
create_sequence :position, step:
|
85
|
+
create_sequence :position, step: 4
|
68
86
|
end
|
69
87
|
end.up
|
70
88
|
|
71
|
-
assert_equal
|
89
|
+
assert_equal 2, MysqlDB.nextval(:position)
|
72
90
|
assert_equal 3, MysqlDB.nextval(:position)
|
91
|
+
assert_operator (2 + @step), :>, MysqlDB.nextval(:position)
|
73
92
|
end
|
74
93
|
|
75
94
|
test "returns current/last sequence value, which doesn't increase by itself" do
|
@@ -80,33 +99,70 @@ class MysqlSequenceTest < Minitest::Test
|
|
80
99
|
end.up
|
81
100
|
|
82
101
|
MysqlDB.nextval(:position)
|
102
|
+
# changed value (=2) after default one (=1)
|
83
103
|
|
84
|
-
assert_equal
|
85
|
-
assert_equal
|
86
|
-
assert_equal
|
87
|
-
assert_equal
|
104
|
+
assert_equal 2, MysqlDB.currval(:position)
|
105
|
+
assert_equal 2, MysqlDB.lastval(:position)
|
106
|
+
assert_equal 2, MysqlDB.currval(:position)
|
107
|
+
assert_equal 2, MysqlDB.lastval(:position)
|
88
108
|
end
|
89
109
|
|
90
|
-
test 'sets sequence value' do
|
110
|
+
test 'sets a new sequence value greater than the current one' do
|
91
111
|
with_migration do
|
92
112
|
def up
|
93
113
|
create_sequence :position
|
94
114
|
end
|
95
115
|
end.up
|
96
116
|
|
97
|
-
MysqlDB.nextval(:position)
|
98
117
|
assert_equal MysqlDB.currval(:position), 1
|
99
118
|
|
119
|
+
MysqlDB.nextval(:position)
|
120
|
+
assert_equal MysqlDB.currval(:position), 2
|
121
|
+
|
100
122
|
MysqlDB.setval(:position, 101)
|
101
|
-
|
102
|
-
|
123
|
+
assert_equal 101, MysqlDB.lastval(:position)
|
124
|
+
|
125
|
+
assert_equal 102, MysqlDB.nextval(:position)
|
126
|
+
end
|
127
|
+
|
128
|
+
test 'sets a new sequence value less than the current one' do
|
129
|
+
with_migration do
|
130
|
+
def up
|
131
|
+
create_sequence :position, start: 100
|
132
|
+
end
|
133
|
+
end.up
|
134
|
+
|
135
|
+
assert_equal MysqlDB.currval(:position), 100
|
136
|
+
|
137
|
+
MysqlDB.nextval(:position)
|
138
|
+
assert_equal MysqlDB.currval(:position), 101
|
139
|
+
|
140
|
+
MysqlDB.setval(:position, 1)
|
141
|
+
assert_equal 101, MysqlDB.lastval(:position)
|
103
142
|
|
104
143
|
MysqlDB.nextval(:position)
|
105
|
-
# now the value is correct
|
106
144
|
assert_equal 102, MysqlDB.lastval(:position)
|
107
145
|
end
|
108
146
|
|
109
|
-
test '
|
147
|
+
test 'sets a new sequence value with a label' do
|
148
|
+
with_migration do
|
149
|
+
def up
|
150
|
+
create_sequence :position
|
151
|
+
end
|
152
|
+
end.up
|
153
|
+
|
154
|
+
MysqlDB.nextval(:position)
|
155
|
+
MysqlDB.nextval_with_label(:position, 1)
|
156
|
+
MysqlDB.nextval_with_label(:position, 1)
|
157
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position where fiction = 1;').all.size
|
158
|
+
assert_equal 2, fiction_set_size
|
159
|
+
|
160
|
+
# create_sequence + nextval
|
161
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position where fiction = 0;').all.size
|
162
|
+
assert_equal 2, fiction_set_size
|
163
|
+
end
|
164
|
+
|
165
|
+
test 'drops the sequence and the check_sequences' do
|
110
166
|
with_migration do
|
111
167
|
def up
|
112
168
|
create_sequence :position
|
@@ -114,7 +170,7 @@ class MysqlSequenceTest < Minitest::Test
|
|
114
170
|
end.up
|
115
171
|
|
116
172
|
sequence = MysqlDB.check_sequences.find_all do |seq|
|
117
|
-
seq[:
|
173
|
+
seq[:name] == 'position'
|
118
174
|
end
|
119
175
|
|
120
176
|
assert_equal 1, sequence.size
|
@@ -126,14 +182,40 @@ class MysqlSequenceTest < Minitest::Test
|
|
126
182
|
end.down
|
127
183
|
|
128
184
|
sequence = MysqlDB.check_sequences.find do |seq|
|
129
|
-
seq[:
|
185
|
+
seq[:name] == 'position'
|
186
|
+
end
|
187
|
+
|
188
|
+
assert_nil sequence
|
189
|
+
end
|
190
|
+
|
191
|
+
test 'drops the sequence with the parameter if_exists' do
|
192
|
+
with_migration do
|
193
|
+
def up
|
194
|
+
create_sequence :position
|
195
|
+
end
|
196
|
+
end.up
|
197
|
+
|
198
|
+
sequence = MysqlDB.check_sequences.find_all do |seq|
|
199
|
+
seq[:name] == 'position'
|
200
|
+
end
|
201
|
+
|
202
|
+
assert_equal 1, sequence.size
|
203
|
+
|
204
|
+
with_migration do
|
205
|
+
def down
|
206
|
+
drop_sequence :position, if_exists: true
|
207
|
+
end
|
208
|
+
end.down
|
209
|
+
|
210
|
+
sequence = MysqlDB.check_sequences.find do |seq|
|
211
|
+
seq[:name] == 'position'
|
130
212
|
end
|
131
213
|
|
132
214
|
assert_nil sequence
|
133
215
|
end
|
134
216
|
|
135
217
|
test 'orders sequences' do
|
136
|
-
list = MysqlDB.check_sequences.map { |s| s[:
|
218
|
+
list = MysqlDB.check_sequences.map { |s| s[:name] }
|
137
219
|
assert !list.include?('a')
|
138
220
|
assert !list.include?('b')
|
139
221
|
assert !list.include?('c')
|
@@ -141,42 +223,149 @@ class MysqlSequenceTest < Minitest::Test
|
|
141
223
|
with_migration do
|
142
224
|
def up
|
143
225
|
drop_table :things, if_exists: true
|
144
|
-
create_sequence :c
|
145
|
-
create_sequence :a
|
226
|
+
create_sequence :c, { start: 1 }
|
227
|
+
create_sequence :a, { start: 3 }
|
146
228
|
create_sequence :b
|
147
229
|
end
|
148
230
|
end.up
|
149
231
|
|
150
|
-
list = MysqlDB.check_sequences.map { |s| s[:
|
232
|
+
list = MysqlDB.check_sequences.map { |s| s[:name] }
|
151
233
|
assert list.include?('a')
|
152
234
|
assert list.include?('b')
|
153
235
|
assert list.include?('c')
|
154
236
|
end
|
155
237
|
|
238
|
+
test 'checks custom sequence generated from code' do
|
239
|
+
assert_equal MysqlDB.custom_sequence?(:c), false
|
240
|
+
|
241
|
+
with_migration do
|
242
|
+
def up
|
243
|
+
create_sequence :c
|
244
|
+
end
|
245
|
+
end.up
|
246
|
+
|
247
|
+
assert_equal MysqlDB.custom_sequence?(:c), true
|
248
|
+
end
|
249
|
+
|
250
|
+
test 'recreates the same sequence with the same start value' do
|
251
|
+
with_migration do
|
252
|
+
def up
|
253
|
+
create_sequence :position_id, if_exists: false, start: 1
|
254
|
+
end
|
255
|
+
end.up
|
256
|
+
|
257
|
+
assert_equal 1, MysqlDB.currval(:position_id)
|
258
|
+
|
259
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
260
|
+
assert_equal 1, fiction_set_size
|
261
|
+
|
262
|
+
with_migration do
|
263
|
+
def up
|
264
|
+
create_sequence :position_id, if_exists: false, start: 1
|
265
|
+
end
|
266
|
+
end.up
|
267
|
+
|
268
|
+
assert_equal 1, MysqlDB.currval(:position_id)
|
269
|
+
|
270
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
271
|
+
assert_equal 1, fiction_set_size
|
272
|
+
end
|
273
|
+
|
274
|
+
test 'recreates the same sequence with a smaller start value' do
|
275
|
+
with_migration do
|
276
|
+
def up
|
277
|
+
create_sequence :position_id, if_exists: false, start: 100
|
278
|
+
end
|
279
|
+
end.up
|
280
|
+
|
281
|
+
assert_equal 100, MysqlDB.currval(:position_id)
|
282
|
+
|
283
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
284
|
+
assert_equal 1, fiction_set_size
|
285
|
+
|
286
|
+
with_migration do
|
287
|
+
def up
|
288
|
+
create_sequence :position_id, if_exists: false, start: 1
|
289
|
+
end
|
290
|
+
end.up
|
291
|
+
|
292
|
+
assert_equal 100, MysqlDB.currval(:position_id)
|
293
|
+
|
294
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
295
|
+
assert_equal 1, fiction_set_size
|
296
|
+
end
|
297
|
+
|
298
|
+
test 'recreates the same sequence with a greater start value' do
|
299
|
+
with_migration do
|
300
|
+
def up
|
301
|
+
create_sequence :position_id, if_exists: false, start: 1
|
302
|
+
end
|
303
|
+
end.up
|
304
|
+
|
305
|
+
assert_equal 1, MysqlDB.currval(:position_id)
|
306
|
+
|
307
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
308
|
+
assert_equal 1, fiction_set_size
|
309
|
+
|
310
|
+
with_migration do
|
311
|
+
def up
|
312
|
+
create_sequence :position_id, if_exists: false, start: 100
|
313
|
+
end
|
314
|
+
end.up
|
315
|
+
|
316
|
+
assert_equal 100, MysqlDB.currval(:position_id)
|
317
|
+
|
318
|
+
fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
319
|
+
assert_equal 2, fiction_set_size
|
320
|
+
end
|
321
|
+
|
156
322
|
test 'creates table that references sequence' do
|
157
323
|
with_migration do
|
158
324
|
def up
|
159
|
-
drop_table :builders, if_exists: true
|
160
325
|
create_sequence :position_id, if_exists: false, start: 1
|
161
|
-
create_table :
|
326
|
+
create_table :creators do
|
162
327
|
primary_key :id
|
163
328
|
String :name, text: true
|
164
|
-
Bignum :position
|
329
|
+
Bignum :position
|
165
330
|
end
|
166
|
-
set_column_default_nextval :
|
331
|
+
set_column_default_nextval :creators, :position, :position_id
|
167
332
|
end
|
168
333
|
end.up
|
169
334
|
|
170
|
-
class
|
335
|
+
class Creator < Sequel::Model; end
|
171
336
|
|
172
|
-
|
173
|
-
pos1 = MysqlDB.
|
174
|
-
assert_equal pos1,
|
337
|
+
creator1 = Creator.create(name: 'Creator 1')
|
338
|
+
pos1 = MysqlDB.lastval(:position_id)
|
339
|
+
assert_equal pos1, creator1.reload.position
|
175
340
|
|
176
|
-
|
341
|
+
creator2 = Creator.create(name: 'Creator 2')
|
177
342
|
pos2 = MysqlDB.currval(:position_id)
|
178
|
-
assert_equal pos2,
|
343
|
+
assert_equal pos2, creator2.reload.position
|
179
344
|
|
180
345
|
assert_equal pos2 - pos1, 1
|
346
|
+
|
347
|
+
MysqlDB.nextval(:position_id)
|
348
|
+
|
349
|
+
creator4 = Creator.create(name: 'Creator 4')
|
350
|
+
pos4 = MysqlDB.currval(:position_id)
|
351
|
+
assert_equal pos4, creator4.reload.position
|
352
|
+
|
353
|
+
assert_equal pos4 - pos2, 2
|
354
|
+
end
|
355
|
+
|
356
|
+
test "catches a Mysql2::Error: «Table mysql_sequence doesn't exist»" do
|
357
|
+
assert !sequence_table_exists?('position')
|
358
|
+
|
359
|
+
MysqlDB.drop_table :mysql_sequence, if_exists: true
|
360
|
+
|
361
|
+
last_number = MysqlDB.lastval(:position)
|
362
|
+
assert_nil last_number
|
363
|
+
end
|
364
|
+
|
365
|
+
test 'creates a Sequence by calling DB.setval(position, 1) if it still does not exist' do
|
366
|
+
assert !sequence_table_exists?('position')
|
367
|
+
|
368
|
+
MysqlDB.setval(:position, 100)
|
369
|
+
assert_equal 100, MysqlDB.currval(:position)
|
181
370
|
end
|
182
371
|
end
|
@@ -12,7 +12,7 @@ class SqliteSequenceTest < Minitest::Test
|
|
12
12
|
test 'adds sequence with default values' do
|
13
13
|
with_migration do
|
14
14
|
def up
|
15
|
-
# create_sequence :position, {start: 1, increment: 1} - default values
|
15
|
+
# create_sequence :position, {start: 1, increment: 1, numeric_label: 0} - default values
|
16
16
|
create_sequence :position
|
17
17
|
end
|
18
18
|
end.up
|
@@ -126,8 +126,7 @@ class SqliteSequenceTest < Minitest::Test
|
|
126
126
|
SQLiteDB.setval(:position, 1)
|
127
127
|
assert_equal 101, SQLiteDB.lastval(:position)
|
128
128
|
|
129
|
-
SQLiteDB.nextval(:position)
|
130
|
-
assert_equal 102, SQLiteDB.lastval(:position)
|
129
|
+
assert_equal 102, SQLiteDB.nextval(:position)
|
131
130
|
end
|
132
131
|
|
133
132
|
test 'sets a new sequence value with a label' do
|
@@ -143,8 +142,9 @@ class SqliteSequenceTest < Minitest::Test
|
|
143
142
|
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position where fiction = 1;').all.size
|
144
143
|
assert_equal 2, fiction_set_size
|
145
144
|
|
145
|
+
# create_sequence + nextval
|
146
146
|
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position where fiction = 0;').all.size
|
147
|
-
assert_equal
|
147
|
+
assert_equal 2, fiction_set_size
|
148
148
|
end
|
149
149
|
|
150
150
|
test 'drops the sequence and the check_sequences' do
|
@@ -166,14 +166,12 @@ class SqliteSequenceTest < Minitest::Test
|
|
166
166
|
end
|
167
167
|
end.down
|
168
168
|
|
169
|
-
sequence = SQLiteDB.check_sequences.
|
170
|
-
seq[:name] == 'position'
|
171
|
-
end
|
169
|
+
sequence = (list = SQLiteDB.check_sequences).empty? ? nil : list
|
172
170
|
|
173
171
|
assert_nil sequence
|
174
172
|
end
|
175
173
|
|
176
|
-
test '
|
174
|
+
test 'drops the sequence with the parameter if_exists' do
|
177
175
|
with_migration do
|
178
176
|
def up
|
179
177
|
create_sequence :position
|
@@ -192,9 +190,7 @@ class SqliteSequenceTest < Minitest::Test
|
|
192
190
|
end
|
193
191
|
end.down
|
194
192
|
|
195
|
-
sequence = SQLiteDB.check_sequences.
|
196
|
-
seq[:name] == 'position'
|
197
|
-
end
|
193
|
+
sequence = (list = SQLiteDB.check_sequences).empty? ? nil : list
|
198
194
|
|
199
195
|
assert_nil sequence
|
200
196
|
end
|
@@ -220,10 +216,81 @@ class SqliteSequenceTest < Minitest::Test
|
|
220
216
|
assert list.include?('c')
|
221
217
|
end
|
222
218
|
|
219
|
+
test 'recreates the same sequence with the same start value' do
|
220
|
+
with_migration do
|
221
|
+
def up
|
222
|
+
create_sequence :position_id, if_exists: false, start: 1
|
223
|
+
end
|
224
|
+
end.up
|
225
|
+
|
226
|
+
assert_equal 1, SQLiteDB.currval(:position_id)
|
227
|
+
|
228
|
+
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
229
|
+
assert_equal 1, fiction_set_size
|
230
|
+
|
231
|
+
with_migration do
|
232
|
+
def up
|
233
|
+
create_sequence :position_id, if_exists: false, start: 1
|
234
|
+
end
|
235
|
+
end.up
|
236
|
+
|
237
|
+
assert_equal 1, SQLiteDB.currval(:position_id)
|
238
|
+
|
239
|
+
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
240
|
+
assert_equal 1, fiction_set_size
|
241
|
+
end
|
242
|
+
|
243
|
+
test 'recreates the same sequence with a smaller start value' do
|
244
|
+
with_migration do
|
245
|
+
def up
|
246
|
+
create_sequence :position_id, if_exists: false, start: 100
|
247
|
+
end
|
248
|
+
end.up
|
249
|
+
|
250
|
+
assert_equal 100, SQLiteDB.currval(:position_id)
|
251
|
+
|
252
|
+
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
253
|
+
assert_equal 1, fiction_set_size
|
254
|
+
|
255
|
+
with_migration do
|
256
|
+
def up
|
257
|
+
create_sequence :position_id, if_exists: false, start: 1
|
258
|
+
end
|
259
|
+
end.up
|
260
|
+
|
261
|
+
assert_equal 100, SQLiteDB.currval(:position_id)
|
262
|
+
|
263
|
+
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
264
|
+
assert_equal 1, fiction_set_size
|
265
|
+
end
|
266
|
+
|
267
|
+
test 'recreates the same sequence with a greater start value' do
|
268
|
+
with_migration do
|
269
|
+
def up
|
270
|
+
create_sequence :position_id, if_exists: false, start: 1
|
271
|
+
end
|
272
|
+
end.up
|
273
|
+
|
274
|
+
assert_equal 1, SQLiteDB.currval(:position_id)
|
275
|
+
|
276
|
+
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
277
|
+
assert_equal 1, fiction_set_size
|
278
|
+
|
279
|
+
with_migration do
|
280
|
+
def up
|
281
|
+
create_sequence :position_id, if_exists: false, start: 100
|
282
|
+
end
|
283
|
+
end.up
|
284
|
+
|
285
|
+
assert_equal 100, SQLiteDB.currval(:position_id)
|
286
|
+
|
287
|
+
fiction_set_size = SQLiteDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
|
288
|
+
assert_equal 2, fiction_set_size
|
289
|
+
end
|
290
|
+
|
223
291
|
test 'creates table that references sequence' do
|
224
292
|
with_migration do
|
225
293
|
def up
|
226
|
-
drop_table :apprentices, if_exists: true
|
227
294
|
create_sequence :position_id, if_exists: false, start: 1
|
228
295
|
create_table :apprentices do
|
229
296
|
primary_key :id
|
@@ -254,4 +321,11 @@ class SqliteSequenceTest < Minitest::Test
|
|
254
321
|
|
255
322
|
assert_equal pos4 - pos2, 2
|
256
323
|
end
|
324
|
+
|
325
|
+
test 'creates a Sequence by calling DB.setval(position, 1) if it still does not exist' do
|
326
|
+
assert !sequence_table_exists?('position')
|
327
|
+
|
328
|
+
SQLiteDB.setval(:position, 100)
|
329
|
+
assert_equal 100, SQLiteDB.currval(:position)
|
330
|
+
end
|
257
331
|
end
|
data/test/sqlite_test_helper.rb
CHANGED
@@ -10,8 +10,10 @@ SQLiteDB = Sequel.connect(
|
|
10
10
|
|
11
11
|
module SqliteTestHelper
|
12
12
|
def recreate_table
|
13
|
-
SQLiteDB.
|
14
|
-
SQLiteDB.
|
13
|
+
SQLiteDB.drop_table :apprentices, if_exists: true
|
14
|
+
SQLiteDB.drop_sequence :position
|
15
|
+
SQLiteDB.drop_sequence :position_id, if_exists: true
|
16
|
+
SQLiteDB.drop_table :objects, if_exists: true
|
15
17
|
SQLiteDB.drop_sequence 'a'
|
16
18
|
SQLiteDB.drop_sequence 'b'
|
17
19
|
SQLiteDB.drop_sequence 'c'
|
@@ -26,4 +28,10 @@ module SqliteTestHelper
|
|
26
28
|
|
27
29
|
Class.new(migration_class, &block).new(SQLiteDB)
|
28
30
|
end
|
31
|
+
|
32
|
+
def sequence_table_exists?(name)
|
33
|
+
sql = "SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%';"
|
34
|
+
table_list = SQLiteDB.fetch(sql).all.map { |_key, value| value }
|
35
|
+
table_list.include?(name)
|
36
|
+
end
|
29
37
|
end
|