sequel-sequence 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ci.gemfile +25 -0
- data/.github/workflows/ci.yml +1 -2
- data/CHANGELOG.md +21 -0
- data/CONTRIBUTING.md +15 -5
- data/README.md +41 -4
- data/lib/sequel/sequence/database/postgresql.rb +43 -15
- data/lib/sequel/sequence/database/server/mariadb.rb +57 -17
- data/lib/sequel/sequence/database/server/mysql.rb +20 -12
- data/lib/sequel/sequence/database/sqlite.rb +21 -8
- data/lib/sequel/sequence/database.rb +17 -0
- data/lib/sequel/sequence/version.rb +1 -1
- data/lib/sequel/sequence.rb +46 -14
- data/sequel-sequence.gemspec +13 -13
- data/test/mariadb_test_helper.rb +4 -0
- data/test/mysql_test_helper.rb +2 -0
- data/test/postgresql_test_helper.rb +5 -1
- data/test/sequel/mariadb_sequence_test.rb +163 -16
- data/test/sequel/mock_sequence_test.rb +19 -1
- data/test/sequel/mysql_sequence_test.rb +133 -16
- data/test/sequel/postgresql_sequence_test.rb +156 -6
- data/test/sequel/sqlite_sequence_test.rb +131 -4
- data/test/sqlite_test_helper.rb +3 -3
- metadata +6 -47
data/lib/sequel/sequence.rb
CHANGED
@@ -1,29 +1,61 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'sequel/database'
|
4
|
-
require 'sequel/adapters/postgres'
|
5
|
-
require 'sequel/adapters/mysql2'
|
6
|
-
require 'sequel/adapters/sqlite'
|
7
4
|
require 'sequel/error'
|
8
5
|
require 'sequel/sequence/database_ext_connection'
|
9
6
|
|
10
7
|
module Sequel
|
11
8
|
module Sequence
|
12
9
|
require 'sequel/sequence/database'
|
13
|
-
|
14
|
-
module Database
|
15
|
-
require 'sequel/sequence/database/postgresql'
|
16
|
-
require 'sequel/sequence/database/sqlite'
|
17
|
-
end
|
18
10
|
end
|
19
11
|
end
|
20
12
|
|
21
13
|
Sequel::Database.include(
|
22
14
|
Sequel::Sequence::Database
|
23
15
|
)
|
24
|
-
|
25
|
-
|
26
|
-
)
|
27
|
-
|
28
|
-
|
29
|
-
|
16
|
+
|
17
|
+
begin
|
18
|
+
if Gem::Specification.find_by_name('pg')
|
19
|
+
require 'sequel/adapters/postgres'
|
20
|
+
|
21
|
+
module Sequel
|
22
|
+
module Sequence
|
23
|
+
module Database
|
24
|
+
require 'sequel/sequence/database/postgresql'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Sequel::Postgres::Database.include(
|
30
|
+
Sequel::Sequence::Database::PostgreSQL
|
31
|
+
)
|
32
|
+
end
|
33
|
+
rescue Gem::LoadError
|
34
|
+
# do nothing
|
35
|
+
end
|
36
|
+
|
37
|
+
begin
|
38
|
+
require 'sequel/adapters/mysql2' if Gem::Specification.find_by_name('mysql2')
|
39
|
+
rescue Gem::LoadError
|
40
|
+
# do nothing
|
41
|
+
end
|
42
|
+
|
43
|
+
begin
|
44
|
+
if Gem::Specification.find_by_name('sqlite3')
|
45
|
+
require 'sequel/adapters/sqlite'
|
46
|
+
|
47
|
+
module Sequel
|
48
|
+
module Sequence
|
49
|
+
module Database
|
50
|
+
require 'sequel/sequence/database/sqlite'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Sequel::SQLite::Database.include(
|
56
|
+
Sequel::Sequence::Database::SQLite
|
57
|
+
)
|
58
|
+
end
|
59
|
+
rescue Gem::LoadError
|
60
|
+
# do nothing
|
61
|
+
end
|
data/sequel-sequence.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.summary = \
|
10
10
|
'Adds SEQUENCE support to Sequel for migrations to PostgreSQL, MariaDB, MySQL and SQLite.'
|
11
11
|
spec.description = <<-DES
|
12
|
-
This gem provides a single interface for SEQUENCE functionality
|
12
|
+
This gem provides a single user-friendly interface for SEQUENCE functionality
|
13
13
|
in Postgresql and MariaDB DBMS within the Sequel ORM.
|
14
14
|
It also models the Sequences to meet the needs of SQLite and MySQL users.
|
15
15
|
DES
|
@@ -27,22 +27,22 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.platform = Gem::Platform::RUBY
|
28
28
|
spec.required_ruby_version = '>= 2.7.0'
|
29
29
|
|
30
|
-
spec.add_dependency 'sequel', '>= 5.28', '<
|
30
|
+
spec.add_dependency 'sequel', '>= 5.28', '<6.0'
|
31
31
|
spec.add_development_dependency 'bundler', '>= 2.2.4'
|
32
32
|
spec.add_development_dependency 'minitest-utils', '~> 0.4.6'
|
33
33
|
spec.add_development_dependency 'pry-byebug', '~> 3.10.1'
|
34
34
|
spec.add_development_dependency 'rake', '~> 13.0.2'
|
35
35
|
spec.add_development_dependency 'rubocop', '~> 1.56.3'
|
36
36
|
spec.add_development_dependency 'simplecov', '~> 0.22.0'
|
37
|
-
if RUBY_ENGINE == 'jruby'
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
else
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
37
|
+
# if RUBY_ENGINE == 'jruby'
|
38
|
+
# # JRuby Adapter Dependencies
|
39
|
+
# spec.add_development_dependency 'jdbc-mysql', '~> 8.0.17'
|
40
|
+
# spec.add_development_dependency 'jdbc-postgres', '~> 42.2.14'
|
41
|
+
# spec.add_development_dependency 'jdbc-sqlite3', '~> 3.42'
|
42
|
+
# else
|
43
|
+
# # MRI/Rubinius Adapter Dependencies
|
44
|
+
# spec.add_development_dependency 'mysql2', '~> 0.5.3'
|
45
|
+
# spec.add_development_dependency 'pg', '~> 1.5.4'
|
46
|
+
# spec.add_development_dependency 'sqlite3', '~> 1.6.0'
|
47
|
+
# end
|
48
48
|
end
|
data/test/mariadb_test_helper.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'test_helper'
|
4
|
+
# require 'logger'
|
4
5
|
|
5
6
|
MariaDB = Sequel.connect(
|
7
|
+
# loggers: [Logger.new($stdout)],
|
6
8
|
adapter: 'mysql2',
|
7
9
|
user: ENV['TEST_MARIA_USERNAME'] || 'root',
|
8
10
|
password: ENV['TEST_MARIA_PASSWORD'] || 'root',
|
@@ -13,7 +15,9 @@ MariaDB = Sequel.connect(
|
|
13
15
|
|
14
16
|
module MariadbTestHelper
|
15
17
|
def recreate_table
|
18
|
+
MariaDB.run 'DROP TABLE IF EXISTS builders'
|
16
19
|
MariaDB.run 'DROP SEQUENCE IF EXISTS position'
|
20
|
+
MariaDB.run 'DROP SEQUENCE IF EXISTS position_id'
|
17
21
|
MariaDB.run 'DROP TABLE IF EXISTS wares'
|
18
22
|
MariaDB.run 'DROP SEQUENCE IF EXISTS a'
|
19
23
|
MariaDB.run 'DROP SEQUENCE IF EXISTS b'
|
data/test/mysql_test_helper.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'test_helper'
|
4
|
+
# require 'logger'
|
4
5
|
|
5
6
|
MysqlDB = Sequel.connect(
|
7
|
+
# loggers: [Logger.new($stdout)],
|
6
8
|
adapter: 'mysql2',
|
7
9
|
user: ENV['TEST_MYSQL_USERNAME'] || 'root',
|
8
10
|
password: ENV['TEST_MYSQL_PASSWORD'] || 'rootroot',
|
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'test_helper'
|
4
|
+
# require 'logger'
|
4
5
|
|
5
6
|
PostgresqlDB = Sequel.connect(
|
7
|
+
# loggers: [Logger.new($stdout)],
|
6
8
|
adapter: 'postgres',
|
7
9
|
user: ENV['TEST_POSTGRES_USERNAME'] || 'postgres',
|
8
10
|
password: ENV['TEST_POSTGRES_PASSWORD'] || 'postgres',
|
@@ -13,8 +15,10 @@ PostgresqlDB = Sequel.connect(
|
|
13
15
|
|
14
16
|
module PostgresqlTestHelper
|
15
17
|
def recreate_table
|
16
|
-
PostgresqlDB.run 'DROP TABLE IF EXISTS
|
18
|
+
PostgresqlDB.run 'DROP TABLE IF EXISTS masters'
|
17
19
|
PostgresqlDB.run 'DROP SEQUENCE IF EXISTS position'
|
20
|
+
PostgresqlDB.run 'DROP SEQUENCE IF EXISTS position_id'
|
21
|
+
PostgresqlDB.run 'DROP TABLE IF EXISTS things'
|
18
22
|
PostgresqlDB.run 'DROP SEQUENCE IF EXISTS a'
|
19
23
|
PostgresqlDB.run 'DROP SEQUENCE IF EXISTS b'
|
20
24
|
PostgresqlDB.run 'DROP SEQUENCE IF EXISTS c'
|
@@ -72,40 +72,77 @@ class MariadbSequenceTest < Minitest::Test
|
|
72
72
|
assert_equal 3, MariaDB.nextval(:position)
|
73
73
|
end
|
74
74
|
|
75
|
-
test
|
75
|
+
test %( returns current/last sequence value, which doesn't increase by itself
|
76
|
+
for migration WITHOUT 'start' or 'increment' values ) do
|
76
77
|
with_migration do
|
77
78
|
def up
|
78
79
|
create_sequence :position
|
79
80
|
end
|
80
81
|
end.up
|
81
82
|
|
82
|
-
MariaDB.nextval(:position)
|
83
|
-
|
84
|
-
assert_equal 1, MariaDB.currval(:position)
|
85
|
-
assert_equal 1, MariaDB.lastval(:position)
|
86
83
|
assert_equal 1, MariaDB.currval(:position)
|
87
84
|
assert_equal 1, MariaDB.lastval(:position)
|
85
|
+
|
86
|
+
MariaDB.nextval(:position)
|
87
|
+
|
88
|
+
assert_equal 2, MariaDB.currval(:position)
|
89
|
+
assert_equal 2, MariaDB.lastval(:position)
|
88
90
|
end
|
89
91
|
|
90
|
-
test
|
92
|
+
test %( returns current/last sequence value, which doesn't increase by itself
|
93
|
+
for migration WITH 'start' and 'increment' values ) do
|
91
94
|
with_migration do
|
92
95
|
def up
|
93
|
-
create_sequence :position
|
96
|
+
create_sequence :position, start: 2, increment: 3
|
94
97
|
end
|
95
98
|
end.up
|
96
99
|
|
100
|
+
assert_equal 2, MariaDB.currval(:position)
|
101
|
+
assert_equal 2, MariaDB.lastval(:position)
|
102
|
+
|
97
103
|
MariaDB.nextval(:position)
|
104
|
+
|
105
|
+
assert_equal 5, MariaDB.currval(:position)
|
106
|
+
assert_equal 5, MariaDB.lastval(:position)
|
107
|
+
end
|
108
|
+
|
109
|
+
test 'sets a new sequence value greater than the current one' do
|
110
|
+
with_migration do
|
111
|
+
def up
|
112
|
+
create_sequence :position
|
113
|
+
end
|
114
|
+
end.up
|
115
|
+
|
98
116
|
assert_equal MariaDB.currval(:position), 1
|
99
117
|
|
100
118
|
MariaDB.setval(:position, 101)
|
101
|
-
#
|
102
|
-
|
119
|
+
# assert_equal 1, MariaDB.lastval(:position)
|
120
|
+
# we observe the modified behavior of the method
|
121
|
+
assert_equal 101, MariaDB.lastval(:position)
|
103
122
|
|
104
123
|
MariaDB.nextval(:position)
|
105
|
-
#
|
124
|
+
# the value is correct in any case
|
106
125
|
assert_equal 102, MariaDB.lastval(:position)
|
107
126
|
end
|
108
127
|
|
128
|
+
test 'sets a new sequence value less than the current one (does not change the value)' do
|
129
|
+
with_migration do
|
130
|
+
def up
|
131
|
+
create_sequence :position, start: 100
|
132
|
+
end
|
133
|
+
end.up
|
134
|
+
|
135
|
+
assert_equal MariaDB.currval(:position), 100
|
136
|
+
|
137
|
+
MariaDB.nextval(:position)
|
138
|
+
assert_equal MariaDB.currval(:position), 101
|
139
|
+
|
140
|
+
MariaDB.setval(:position, 1)
|
141
|
+
assert_equal 101, MariaDB.lastval(:position)
|
142
|
+
|
143
|
+
assert_equal 102, MariaDB.nextval(:position)
|
144
|
+
end
|
145
|
+
|
109
146
|
test 'drops sequence and check_sequences' do
|
110
147
|
with_migration do
|
111
148
|
def up
|
@@ -125,14 +162,18 @@ class MariadbSequenceTest < Minitest::Test
|
|
125
162
|
end
|
126
163
|
end.down
|
127
164
|
|
128
|
-
sequence = MariaDB.check_sequences
|
129
|
-
seq[:Tables_in_test] == 'position'
|
130
|
-
end
|
165
|
+
sequence = MariaDB.check_sequences
|
131
166
|
|
132
|
-
|
167
|
+
assert_equal 0, sequence.size
|
133
168
|
end
|
134
169
|
|
135
170
|
test 'orders sequences' do
|
171
|
+
with_migration do
|
172
|
+
def up
|
173
|
+
drop_table :wares, if_exists: true
|
174
|
+
end
|
175
|
+
end.up
|
176
|
+
|
136
177
|
list = MariaDB.check_sequences.map { |s| s[:Tables_in_test] }
|
137
178
|
assert !list.include?('a')
|
138
179
|
assert !list.include?('b')
|
@@ -140,7 +181,6 @@ class MariadbSequenceTest < Minitest::Test
|
|
140
181
|
|
141
182
|
with_migration do
|
142
183
|
def up
|
143
|
-
drop_table :things, if_exists: true
|
144
184
|
create_sequence :c
|
145
185
|
create_sequence :a
|
146
186
|
create_sequence :b
|
@@ -153,10 +193,21 @@ class MariadbSequenceTest < Minitest::Test
|
|
153
193
|
assert list.include?('c')
|
154
194
|
end
|
155
195
|
|
196
|
+
test 'checks custom sequence generated from code' do
|
197
|
+
assert_equal MariaDB.custom_sequence?(:c), false
|
198
|
+
|
199
|
+
with_migration do
|
200
|
+
def up
|
201
|
+
create_sequence :c
|
202
|
+
end
|
203
|
+
end.up
|
204
|
+
|
205
|
+
assert_equal MariaDB.custom_sequence?(:c), true
|
206
|
+
end
|
207
|
+
|
156
208
|
test 'creates table that references sequence' do
|
157
209
|
with_migration do
|
158
210
|
def up
|
159
|
-
drop_table :builders, if_exists: true
|
160
211
|
create_sequence :position_id, if_exists: false, start: 1
|
161
212
|
create_table :builders do
|
162
213
|
primary_key :id
|
@@ -179,4 +230,100 @@ class MariadbSequenceTest < Minitest::Test
|
|
179
230
|
|
180
231
|
assert_equal pos2 - pos1, 1
|
181
232
|
end
|
233
|
+
|
234
|
+
test 'checks exception for delete_to_currval method' do
|
235
|
+
with_migration do
|
236
|
+
def up
|
237
|
+
create_sequence :position_id, if_exists: false, start: 1
|
238
|
+
end
|
239
|
+
end.up
|
240
|
+
|
241
|
+
assert_equal 1, MariaDB.currval(:position_id)
|
242
|
+
|
243
|
+
assert_raises Sequel::MethodNotAllowed do
|
244
|
+
MariaDB.delete_to_currval(:position_id)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
test 'deletes sequences by "drop_sequence?"' do
|
249
|
+
with_migration do
|
250
|
+
def up
|
251
|
+
create_sequence :a
|
252
|
+
create_sequence :b
|
253
|
+
create_sequence :c
|
254
|
+
end
|
255
|
+
end.up
|
256
|
+
|
257
|
+
assert MariaDB.custom_sequence?(:a)
|
258
|
+
assert MariaDB.custom_sequence?(:b)
|
259
|
+
assert MariaDB.custom_sequence?(:c)
|
260
|
+
|
261
|
+
MariaDB.drop_sequence?(:a, :b, :c, :d)
|
262
|
+
|
263
|
+
assert !MariaDB.custom_sequence?(:a)
|
264
|
+
assert !MariaDB.custom_sequence?(:b)
|
265
|
+
assert !MariaDB.custom_sequence?(:c)
|
266
|
+
end
|
267
|
+
|
268
|
+
test 'deletes if exists before creating! the sequence' do
|
269
|
+
with_migration do
|
270
|
+
def up
|
271
|
+
create_sequence! :position, { start: 1000, increment: 10 }
|
272
|
+
end
|
273
|
+
end.up
|
274
|
+
|
275
|
+
assert_equal 1000, MariaDB.currval(:position)
|
276
|
+
|
277
|
+
10.times { MariaDB.nextval(:position) }
|
278
|
+
assert_equal 1100, MariaDB.currval(:position)
|
279
|
+
|
280
|
+
MariaDB.create_sequence!(:position, { start: 1, increment: 1 })
|
281
|
+
|
282
|
+
assert_equal 1, MariaDB.currval(:position)
|
283
|
+
|
284
|
+
10.times { MariaDB.nextval(:position) }
|
285
|
+
assert_equal 11, MariaDB.currval(:position)
|
286
|
+
end
|
287
|
+
|
288
|
+
test 'checks the BIGINT primery key for a sequence table' do
|
289
|
+
with_migration do
|
290
|
+
def up
|
291
|
+
create_sequence :position, { start: 9_223_372_036_854_775_800 }
|
292
|
+
end
|
293
|
+
end.up
|
294
|
+
|
295
|
+
assert_equal 9_223_372_036_854_775_800, MariaDB.currval(:position)
|
296
|
+
assert_equal 9_223_372_036_854_775_801, MariaDB.nextval(:position)
|
297
|
+
end
|
298
|
+
|
299
|
+
test 'checks the BIGINT primery key with negative value for a sequence table' do
|
300
|
+
with_migration do
|
301
|
+
def up
|
302
|
+
create_sequence :position, { minvalue: -9_223_372_036_854_775_807 }
|
303
|
+
end
|
304
|
+
end.up
|
305
|
+
|
306
|
+
assert_equal(-9_223_372_036_854_775_807, MariaDB.currval(:position))
|
307
|
+
assert_equal(-9_223_372_036_854_775_806, MariaDB.nextval(:position))
|
308
|
+
end
|
309
|
+
|
310
|
+
# https://mariadb.com/kb/en/create-sequence/#arguments-to-create
|
311
|
+
test 'adds sequence with ext. params' do
|
312
|
+
with_migration do
|
313
|
+
def up
|
314
|
+
create_sequence :position, {
|
315
|
+
minvalue: -250,
|
316
|
+
maxvalue: 250,
|
317
|
+
start: -1,
|
318
|
+
cache: 3,
|
319
|
+
cycle: 'CYCLE',
|
320
|
+
increment: 2,
|
321
|
+
if_exists: false
|
322
|
+
}
|
323
|
+
end
|
324
|
+
end.up
|
325
|
+
|
326
|
+
assert_equal(-1, MariaDB.nextval('position'))
|
327
|
+
assert_equal 1, MariaDB.nextval('position')
|
328
|
+
end
|
182
329
|
end
|
@@ -20,7 +20,7 @@ class MockSequenceTest < Minitest::Test
|
|
20
20
|
mocked_method.verify
|
21
21
|
end
|
22
22
|
|
23
|
-
test 'checks custom_sequence?' do
|
23
|
+
test 'checks "custom_sequence?"' do
|
24
24
|
assert_raises Sequel::MethodNotAllowed do
|
25
25
|
MockDB.custom_sequence?(:position)
|
26
26
|
end
|
@@ -97,4 +97,22 @@ class MockSequenceTest < Minitest::Test
|
|
97
97
|
test 'checks build_exists_condition for a non boolean condition' do
|
98
98
|
assert_nil MockDB.build_exists_condition('')
|
99
99
|
end
|
100
|
+
|
101
|
+
test 'checks delete_to_currval' do
|
102
|
+
assert_raises Sequel::MethodNotAllowed do
|
103
|
+
MockDB.delete_to_currval(:position)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
test 'checks "drop_sequence?"' do
|
108
|
+
assert_raises Sequel::MethodNotAllowed do
|
109
|
+
MockDB.drop_sequence?(:position)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
test 'checks "create_sequence!"' do
|
114
|
+
assert_raises Sequel::MethodNotAllowed do
|
115
|
+
MockDB.create_sequence!(:position)
|
116
|
+
end
|
117
|
+
end
|
100
118
|
end
|
@@ -91,20 +91,41 @@ class MysqlSequenceTest < Minitest::Test
|
|
91
91
|
assert_operator (2 + @step), :>, MysqlDB.nextval(:position)
|
92
92
|
end
|
93
93
|
|
94
|
-
test
|
94
|
+
test %( returns current/last sequence value, which doesn't increase by itself
|
95
|
+
for migration WITHOUT 'start' or 'increment' values ) do
|
95
96
|
with_migration do
|
96
97
|
def up
|
97
98
|
create_sequence :position
|
98
99
|
end
|
99
100
|
end.up
|
100
101
|
|
102
|
+
# catch the 'start' value 'by default
|
103
|
+
assert_equal 1, MysqlDB.currval(:position)
|
104
|
+
assert_equal 1, MysqlDB.lastval(:position)
|
105
|
+
|
101
106
|
MysqlDB.nextval(:position)
|
102
|
-
# changed value (=2) after default one (=1)
|
103
107
|
|
104
108
|
assert_equal 2, MysqlDB.currval(:position)
|
105
109
|
assert_equal 2, MysqlDB.lastval(:position)
|
110
|
+
end
|
111
|
+
|
112
|
+
test %( returns current/last sequence value, which doesn't increase by itself
|
113
|
+
for migration WITH 'start' and 'increment' values ) do
|
114
|
+
with_migration do
|
115
|
+
def up
|
116
|
+
create_sequence :position, start: 2, increment: 3
|
117
|
+
end
|
118
|
+
end.up
|
119
|
+
|
120
|
+
# catch the 'start' value
|
106
121
|
assert_equal 2, MysqlDB.currval(:position)
|
107
122
|
assert_equal 2, MysqlDB.lastval(:position)
|
123
|
+
|
124
|
+
MysqlDB.nextval(:position)
|
125
|
+
|
126
|
+
# Doesn't support 'increment' value
|
127
|
+
assert_equal 3, MysqlDB.currval(:position)
|
128
|
+
assert_equal 3, MysqlDB.lastval(:position)
|
108
129
|
end
|
109
130
|
|
110
131
|
test 'sets a new sequence value greater than the current one' do
|
@@ -125,7 +146,7 @@ class MysqlSequenceTest < Minitest::Test
|
|
125
146
|
assert_equal 102, MysqlDB.nextval(:position)
|
126
147
|
end
|
127
148
|
|
128
|
-
test 'sets a new sequence value less than the current one' do
|
149
|
+
test 'sets a new sequence value less than the current one (does not change the value)' do
|
129
150
|
with_migration do
|
130
151
|
def up
|
131
152
|
create_sequence :position, start: 100
|
@@ -215,6 +236,12 @@ class MysqlSequenceTest < Minitest::Test
|
|
215
236
|
end
|
216
237
|
|
217
238
|
test 'orders sequences' do
|
239
|
+
with_migration do
|
240
|
+
def up
|
241
|
+
drop_table :stuffs, if_exists: true
|
242
|
+
end
|
243
|
+
end.up
|
244
|
+
|
218
245
|
list = MysqlDB.check_sequences.map { |s| s[:name] }
|
219
246
|
assert !list.include?('a')
|
220
247
|
assert !list.include?('b')
|
@@ -222,7 +249,6 @@ class MysqlSequenceTest < Minitest::Test
|
|
222
249
|
|
223
250
|
with_migration do
|
224
251
|
def up
|
225
|
-
drop_table :things, if_exists: true
|
226
252
|
create_sequence :c, { start: 1 }
|
227
253
|
create_sequence :a, { start: 3 }
|
228
254
|
create_sequence :b
|
@@ -235,18 +261,6 @@ class MysqlSequenceTest < Minitest::Test
|
|
235
261
|
assert list.include?('c')
|
236
262
|
end
|
237
263
|
|
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
264
|
test 'recreates the same sequence with the same start value' do
|
251
265
|
with_migration do
|
252
266
|
def up
|
@@ -353,6 +367,18 @@ class MysqlSequenceTest < Minitest::Test
|
|
353
367
|
assert_equal pos4 - pos2, 2
|
354
368
|
end
|
355
369
|
|
370
|
+
test 'checks custom sequence generated from code' do
|
371
|
+
assert_equal MysqlDB.custom_sequence?(:c), false
|
372
|
+
|
373
|
+
with_migration do
|
374
|
+
def up
|
375
|
+
create_sequence :c
|
376
|
+
end
|
377
|
+
end.up
|
378
|
+
|
379
|
+
assert_equal MysqlDB.custom_sequence?(:c), true
|
380
|
+
end
|
381
|
+
|
356
382
|
test "catches a Mysql2::Error: «Table mysql_sequence doesn't exist»" do
|
357
383
|
assert !sequence_table_exists?('position')
|
358
384
|
|
@@ -368,4 +394,95 @@ class MysqlSequenceTest < Minitest::Test
|
|
368
394
|
MysqlDB.setval(:position, 100)
|
369
395
|
assert_equal 100, MysqlDB.currval(:position)
|
370
396
|
end
|
397
|
+
|
398
|
+
test 'deletes history from the sequence_table' do
|
399
|
+
with_migration do
|
400
|
+
def up
|
401
|
+
create_sequence :position_id, if_exists: false, start: 1
|
402
|
+
end
|
403
|
+
end.up
|
404
|
+
|
405
|
+
assert_equal 1, MysqlDB.currval(:position_id)
|
406
|
+
|
407
|
+
sequence_table_size = MysqlDB.fetch('SELECT count(*) as len FROM position_id;').first[:len]
|
408
|
+
assert_equal 1, sequence_table_size
|
409
|
+
|
410
|
+
10.times { MysqlDB.nextval(:position_id) }
|
411
|
+
|
412
|
+
assert_equal 11, MysqlDB.currval(:position_id)
|
413
|
+
|
414
|
+
sequence_table_size = MysqlDB.fetch('SELECT count(*) as len FROM position_id;').first[:len]
|
415
|
+
assert_equal 11, sequence_table_size
|
416
|
+
|
417
|
+
MysqlDB.delete_to_currval(:position_id)
|
418
|
+
|
419
|
+
assert_equal 11, MysqlDB.currval(:position_id)
|
420
|
+
|
421
|
+
sequence_table_size = MysqlDB.fetch('SELECT count(*) as len FROM position_id;').first[:len]
|
422
|
+
assert_equal 1, sequence_table_size
|
423
|
+
end
|
424
|
+
|
425
|
+
test 'deletes sequences by "drop_sequence?"' do
|
426
|
+
with_migration do
|
427
|
+
def up
|
428
|
+
create_sequence :a
|
429
|
+
create_sequence :b
|
430
|
+
create_sequence :c
|
431
|
+
end
|
432
|
+
end.up
|
433
|
+
|
434
|
+
assert MysqlDB.custom_sequence?(:a)
|
435
|
+
assert MysqlDB.custom_sequence?(:b)
|
436
|
+
assert MysqlDB.custom_sequence?(:c)
|
437
|
+
|
438
|
+
MysqlDB.drop_sequence?(:a, :b, :c, :d)
|
439
|
+
|
440
|
+
assert !MysqlDB.custom_sequence?(:a)
|
441
|
+
assert !MysqlDB.custom_sequence?(:b)
|
442
|
+
assert !MysqlDB.custom_sequence?(:c)
|
443
|
+
end
|
444
|
+
|
445
|
+
test 'deletes if exists before creating! the sequence' do
|
446
|
+
with_migration do
|
447
|
+
def up
|
448
|
+
create_sequence! :position, { start: 1000, increment: 10 }
|
449
|
+
end
|
450
|
+
end.up
|
451
|
+
|
452
|
+
assert_equal 1000, MysqlDB.currval(:position)
|
453
|
+
|
454
|
+
10.times { MysqlDB.nextval(:position) }
|
455
|
+
assert_equal 1010, MysqlDB.currval(:position)
|
456
|
+
|
457
|
+
MysqlDB.create_sequence!(:position, { start: 1, increment: 1 })
|
458
|
+
|
459
|
+
assert_equal 1, MysqlDB.currval(:position)
|
460
|
+
|
461
|
+
10.times { MysqlDB.nextval(:position) }
|
462
|
+
assert_equal 11, MysqlDB.currval(:position)
|
463
|
+
end
|
464
|
+
|
465
|
+
test 'checks the BIGINT primery key for a sequence table' do
|
466
|
+
with_migration do
|
467
|
+
def up
|
468
|
+
create_sequence :position, { start: 9_223_372_036_854_775_800 }
|
469
|
+
end
|
470
|
+
end.up
|
471
|
+
|
472
|
+
assert_equal 9_223_372_036_854_775_800, MysqlDB.currval(:position)
|
473
|
+
assert_equal 9_223_372_036_854_775_801, MysqlDB.nextval(:position)
|
474
|
+
end
|
475
|
+
|
476
|
+
test 'checks the BIGINT primery key with negative value for a sequence table (does not support)' do
|
477
|
+
with_migration do
|
478
|
+
def up
|
479
|
+
create_sequence :position, { start: -9_223_372_036_854_775_807 }
|
480
|
+
end
|
481
|
+
end.up
|
482
|
+
|
483
|
+
assert_equal(-9_223_372_036_854_775_807, MysqlDB.currval(:position))
|
484
|
+
# https://www.cockroachlabs.com/docs/stable/serial#generated-values-for-modes-rowid-and-virtual_sequence
|
485
|
+
# "...negative values are not returned."
|
486
|
+
assert_equal 1, MysqlDB.nextval(:position)
|
487
|
+
end
|
371
488
|
end
|