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.
@@ -72,33 +72,80 @@ class PostgresqlSequenceTest < Minitest::Test
72
72
  assert_equal 3, PostgresqlDB.nextval(:position)
73
73
  end
74
74
 
75
- test "returns current/last sequence value, which doesn't increase by itself" do
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
- create_sequence :position, start: 2, increment: 2
79
+ create_sequence :position
79
80
  end
80
81
  end.up
81
82
 
83
+ # catch the 'start' value
84
+ assert_equal 1, PostgresqlDB.currval(:position)
85
+ # is the same value
86
+ assert_equal 1, PostgresqlDB.lastval(:position)
87
+
82
88
  PostgresqlDB.nextval(:position)
83
89
 
84
90
  assert_equal 2, PostgresqlDB.currval(:position)
85
91
  assert_equal 2, PostgresqlDB.lastval(:position)
92
+ end
93
+
94
+ test %( returns current/last sequence value, which doesn't increase by itself
95
+ for migration WITH 'start' and 'increment' values ) do
96
+ with_migration do
97
+ def up
98
+ create_sequence :position, start: 2, increment: 3
99
+ end
100
+ end.up
101
+
102
+ # catch the 'start' value
86
103
  assert_equal 2, PostgresqlDB.currval(:position)
104
+ # is the same value
87
105
  assert_equal 2, PostgresqlDB.lastval(:position)
106
+
107
+ PostgresqlDB.nextval(:position)
108
+
109
+ # support 'increment' value
110
+ assert_equal 5, PostgresqlDB.currval(:position)
111
+ assert_equal 5, PostgresqlDB.lastval(:position)
88
112
  end
89
113
 
90
- test 'sets sequence value' do
114
+ test 'sets a new sequence value greater than the current one' do
91
115
  with_migration do
92
116
  def up
93
117
  create_sequence :position
94
118
  end
95
119
  end.up
96
120
 
97
- PostgresqlDB.nextval(:position)
98
121
  assert_equal PostgresqlDB.currval(:position), 1
99
122
 
123
+ PostgresqlDB.nextval(:position)
124
+ assert_equal PostgresqlDB.currval(:position), 2
125
+
100
126
  PostgresqlDB.setval(:position, 101)
101
- assert_equal 101, PostgresqlDB.currval(:position)
127
+ assert_equal 101, PostgresqlDB.lastval(:position)
128
+
129
+ assert_equal 102, PostgresqlDB.nextval(:position)
130
+ end
131
+
132
+ test 'sets a new sequence value less than the current one (change the value as an EXCEPTION)' do
133
+ with_migration do
134
+ def up
135
+ create_sequence :position, start: 100
136
+ end
137
+ end.up
138
+
139
+ assert_equal PostgresqlDB.currval(:position), 100
140
+
141
+ PostgresqlDB.nextval(:position)
142
+ assert_equal PostgresqlDB.currval(:position), 101
143
+
144
+ PostgresqlDB.setval(:position, 1)
145
+ assert_equal 1, PostgresqlDB.lastval(:position)
146
+
147
+ PostgresqlDB.nextval(:position)
148
+ assert_equal 2, PostgresqlDB.lastval(:position)
102
149
  end
103
150
 
104
151
  test 'drops sequence and check_sequences' do
@@ -128,6 +175,12 @@ class PostgresqlSequenceTest < Minitest::Test
128
175
  end
129
176
 
130
177
  test 'orders sequences' do
178
+ with_migration do
179
+ def up
180
+ drop_table :things, if_exists: true
181
+ end
182
+ end.up
183
+
131
184
  list = PostgresqlDB.check_sequences.map { |s| s[:sequence_name] }
132
185
  assert !list.include?('a')
133
186
  assert !list.include?('b')
@@ -135,7 +188,6 @@ class PostgresqlSequenceTest < Minitest::Test
135
188
 
136
189
  with_migration do
137
190
  def up
138
- drop_table :things, if_exists: true
139
191
  create_sequence :c
140
192
  create_sequence :a
141
193
  create_sequence :b
@@ -186,4 +238,102 @@ class PostgresqlSequenceTest < Minitest::Test
186
238
 
187
239
  assert_equal pos2 - pos1, 1
188
240
  end
241
+
242
+ test 'checks exception for delete_to_currval method' do
243
+ with_migration do
244
+ def up
245
+ create_sequence :position_id, if_exists: false, start: 1
246
+ end
247
+ end.up
248
+
249
+ assert_equal 1, PostgresqlDB.currval(:position_id)
250
+
251
+ assert_raises Sequel::MethodNotAllowed do
252
+ PostgresqlDB.delete_to_currval(:position_id)
253
+ end
254
+ end
255
+
256
+ test 'deletes sequences by "drop_sequence?"' do
257
+ with_migration do
258
+ def up
259
+ create_sequence :a
260
+ create_sequence :b
261
+ create_sequence :c
262
+ end
263
+ end.up
264
+
265
+ assert PostgresqlDB.custom_sequence?(:a)
266
+ assert PostgresqlDB.custom_sequence?(:b)
267
+ assert PostgresqlDB.custom_sequence?(:c)
268
+
269
+ PostgresqlDB.drop_sequence?(:a, :b, :c, :d)
270
+
271
+ assert !PostgresqlDB.custom_sequence?(:a)
272
+ assert !PostgresqlDB.custom_sequence?(:b)
273
+ assert !PostgresqlDB.custom_sequence?(:c)
274
+ end
275
+
276
+ test 'deletes if exists before creating! the sequence' do
277
+ with_migration do
278
+ def up
279
+ create_sequence! :position, { start: 1000, increment: 10 }
280
+ end
281
+ end.up
282
+
283
+ assert_equal 1000, PostgresqlDB.currval(:position)
284
+
285
+ 10.times { PostgresqlDB.nextval(:position) }
286
+ assert_equal 1100, PostgresqlDB.currval(:position)
287
+
288
+ PostgresqlDB.create_sequence!(:position, { start: 1, increment: 1 })
289
+
290
+ assert_equal 1, PostgresqlDB.currval(:position)
291
+
292
+ 10.times { PostgresqlDB.nextval(:position) }
293
+ assert_equal 11, PostgresqlDB.currval(:position)
294
+ end
295
+
296
+ test 'checks the BIGINT primery key for a sequence table' do
297
+ with_migration do
298
+ def up
299
+ create_sequence :position, { start: 9_223_372_036_854_775_800 }
300
+ end
301
+ end.up
302
+
303
+ assert_equal 9_223_372_036_854_775_800, PostgresqlDB.currval(:position)
304
+ assert_equal 9_223_372_036_854_775_801, PostgresqlDB.nextval(:position)
305
+ end
306
+
307
+ test 'checks the BIGINT primery key with negative value for a sequence table' do
308
+ with_migration do
309
+ def up
310
+ create_sequence :position, { minvalue: -9_223_372_036_854_775_808 }
311
+ end
312
+ end.up
313
+
314
+ assert_equal(-9_223_372_036_854_775_808, PostgresqlDB.currval(:position))
315
+ assert_equal(-9_223_372_036_854_775_807, PostgresqlDB.nextval(:position))
316
+ end
317
+
318
+ # https://www.postgresql.org/docs/current/sql-createsequence.html
319
+ test 'adds sequence with ext. params' do
320
+ with_migration do
321
+ def up
322
+ create_sequence :position, {
323
+ data_type: 'smallint',
324
+ minvalue: -250,
325
+ maxvalue: 250,
326
+ start: -1,
327
+ cache: 3,
328
+ cycle: 'CYCLE',
329
+ owned_by: 'NONE',
330
+ increment: 2,
331
+ if_exists: false
332
+ }
333
+ end
334
+ end.up
335
+
336
+ assert_equal(-1, PostgresqlDB.nextval('position'))
337
+ assert_equal 1, PostgresqlDB.nextval('position')
338
+ end
189
339
  end
@@ -76,20 +76,41 @@ class SqliteSequenceTest < Minitest::Test
76
76
  assert_operator (2 + @step), :>, SQLiteDB.nextval(:position)
77
77
  end
78
78
 
79
- test "returns current/last sequence value, which doesn't increase by itself" do
79
+ test %( returns current/last sequence value, which doesn't increase by itself
80
+ for migration WITHOUT 'start' or 'increment' values ) do
80
81
  with_migration do
81
82
  def up
82
83
  create_sequence :position
83
84
  end
84
85
  end.up
85
86
 
87
+ # catch the 'start' value 'by default
88
+ assert_equal 1, SQLiteDB.currval(:position)
89
+ assert_equal 1, SQLiteDB.lastval(:position)
90
+
86
91
  SQLiteDB.nextval(:position)
87
- # changed value (=2) after default one (=1)
88
92
 
89
93
  assert_equal 2, SQLiteDB.currval(:position)
90
94
  assert_equal 2, SQLiteDB.lastval(:position)
95
+ end
96
+
97
+ test %( returns current/last sequence value, which doesn't increase by itself
98
+ for migration WITH 'start' and 'increment' values ) do
99
+ with_migration do
100
+ def up
101
+ create_sequence :position, start: 2, increment: 3
102
+ end
103
+ end.up
104
+
105
+ # catch the 'start' value
91
106
  assert_equal 2, SQLiteDB.currval(:position)
92
107
  assert_equal 2, SQLiteDB.lastval(:position)
108
+
109
+ SQLiteDB.nextval(:position)
110
+
111
+ # Doesn't support 'increment' value
112
+ assert_equal 3, SQLiteDB.currval(:position)
113
+ assert_equal 3, SQLiteDB.lastval(:position)
93
114
  end
94
115
 
95
116
  test 'sets a new sequence value greater than the current one' do
@@ -111,7 +132,7 @@ class SqliteSequenceTest < Minitest::Test
111
132
  assert_equal 102, SQLiteDB.lastval(:position)
112
133
  end
113
134
 
114
- test 'sets a new sequence value less than the current one' do
135
+ test 'sets a new sequence value less than the current one (does not change the value)' do
115
136
  with_migration do
116
137
  def up
117
138
  create_sequence :position, start: 100
@@ -196,6 +217,12 @@ class SqliteSequenceTest < Minitest::Test
196
217
  end
197
218
 
198
219
  test 'orders sequences' do
220
+ with_migration do
221
+ def up
222
+ drop_table :objects, if_exists: true
223
+ end
224
+ end.up
225
+
199
226
  list = SQLiteDB.check_sequences.map { |s| s[:name] }
200
227
  assert !list.include?('a')
201
228
  assert !list.include?('b')
@@ -203,7 +230,6 @@ class SqliteSequenceTest < Minitest::Test
203
230
 
204
231
  with_migration do
205
232
  def up
206
- drop_table :things, if_exists: true
207
233
  create_sequence :c, { start: 1 }
208
234
  create_sequence :a, { start: 3 }
209
235
  create_sequence :b
@@ -322,10 +348,111 @@ class SqliteSequenceTest < Minitest::Test
322
348
  assert_equal pos4 - pos2, 2
323
349
  end
324
350
 
351
+ test 'checks custom sequence generated from code' do
352
+ assert_equal SQLiteDB.custom_sequence?(:c), false
353
+
354
+ with_migration do
355
+ def up
356
+ create_sequence :c
357
+ end
358
+ end.up
359
+
360
+ assert_equal SQLiteDB.custom_sequence?(:c), true
361
+ end
362
+
325
363
  test 'creates a Sequence by calling DB.setval(position, 1) if it still does not exist' do
326
364
  assert !sequence_table_exists?('position')
327
365
 
328
366
  SQLiteDB.setval(:position, 100)
329
367
  assert_equal 100, SQLiteDB.currval(:position)
330
368
  end
369
+
370
+ test 'deletes history from the sequence_table' do
371
+ with_migration do
372
+ def up
373
+ create_sequence :position_id, if_exists: false, start: 1
374
+ end
375
+ end.up
376
+
377
+ assert_equal 1, SQLiteDB.currval(:position_id)
378
+
379
+ sequence_table_size = SQLiteDB.fetch('SELECT count(*) as len FROM position_id;').first[:len]
380
+ assert_equal 1, sequence_table_size
381
+
382
+ 10.times { SQLiteDB.nextval(:position_id) }
383
+
384
+ assert_equal 11, SQLiteDB.currval(:position_id)
385
+
386
+ sequence_table_size = SQLiteDB.fetch('SELECT count(*) as len FROM position_id;').first[:len]
387
+ assert_equal 11, sequence_table_size
388
+
389
+ SQLiteDB.delete_to_currval(:position_id)
390
+
391
+ assert_equal 11, SQLiteDB.currval(:position_id)
392
+
393
+ sequence_table_size = SQLiteDB.fetch('SELECT count(*) as len FROM position_id;').first[:len]
394
+ assert_equal 1, sequence_table_size
395
+ end
396
+
397
+ test 'deletes sequences by "drop_sequence?"' do
398
+ with_migration do
399
+ def up
400
+ create_sequence :a
401
+ create_sequence :b
402
+ create_sequence :c
403
+ end
404
+ end.up
405
+
406
+ assert SQLiteDB.custom_sequence?(:a)
407
+ assert SQLiteDB.custom_sequence?(:b)
408
+ assert SQLiteDB.custom_sequence?(:c)
409
+
410
+ SQLiteDB.drop_sequence?(:a, :b, :c, :d)
411
+
412
+ assert !SQLiteDB.custom_sequence?(:a)
413
+ assert !SQLiteDB.custom_sequence?(:b)
414
+ assert !SQLiteDB.custom_sequence?(:c)
415
+ end
416
+
417
+ test 'deletes if exists before creating! the sequence' do
418
+ with_migration do
419
+ def up
420
+ create_sequence! :position, { start: 1000, increment: 10 }
421
+ end
422
+ end.up
423
+
424
+ assert_equal 1000, SQLiteDB.currval(:position)
425
+
426
+ 10.times { SQLiteDB.nextval(:position) }
427
+ assert_equal 1010, SQLiteDB.currval(:position)
428
+
429
+ SQLiteDB.create_sequence!(:position, { start: 1, increment: 1 })
430
+
431
+ assert_equal 1, SQLiteDB.currval(:position)
432
+
433
+ 10.times { SQLiteDB.nextval(:position) }
434
+ assert_equal 11, SQLiteDB.currval(:position)
435
+ end
436
+
437
+ test 'checks the BIGINT primery key for a sequence table' do
438
+ with_migration do
439
+ def up
440
+ create_sequence :position, { start: 9_223_372_036_854_775_800 }
441
+ end
442
+ end.up
443
+
444
+ assert_equal 9_223_372_036_854_775_800, SQLiteDB.currval(:position)
445
+ assert_equal 9_223_372_036_854_775_801, SQLiteDB.nextval(:position)
446
+ end
447
+
448
+ test 'checks the BIGINT primery key with negative value for a sequence table (does not support)' do
449
+ with_migration do
450
+ def up
451
+ create_sequence :position, { start: -9_223_372_036_854_775_807 }
452
+ end
453
+ end.up
454
+
455
+ assert_equal 0, SQLiteDB.currval(:position)
456
+ assert_equal 1, SQLiteDB.nextval(:position)
457
+ end
331
458
  end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'test_helper'
4
- require 'logger'
4
+ # require 'logger'
5
5
 
6
6
  SQLiteDB = Sequel.connect(
7
- "sqlite://#{ENV.fetch('TEST_SQLITE_DATABASE', nil) || 'db/test.sqlite3'}",
8
- loggers: [Logger.new($stdout)]
7
+ # loggers: [Logger.new($stdout)],
8
+ "sqlite://#{ENV.fetch('TEST_SQLITE_DATABASE', nil) || 'db/test.sqlite3'}"
9
9
  )
10
10
 
11
11
  module SqliteTestHelper
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-sequence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikolai Bocharov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-28 00:00:00.000000000 Z
11
+ date: 2023-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.28'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.73'
22
+ version: '6.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5.28'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.73'
32
+ version: '6.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -114,50 +114,8 @@ dependencies:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
116
  version: 0.22.0
117
- - !ruby/object:Gem::Dependency
118
- name: mysql2
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: 0.5.3
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: 0.5.3
131
- - !ruby/object:Gem::Dependency
132
- name: pg
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: 1.5.4
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: 1.5.4
145
- - !ruby/object:Gem::Dependency
146
- name: sqlite3
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: 1.6.0
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- requirements:
156
- - - "~>"
157
- - !ruby/object:Gem::Version
158
- version: 1.6.0
159
117
  description: |2
160
- This gem provides a single interface for SEQUENCE functionality
118
+ This gem provides a single user-friendly interface for SEQUENCE functionality
161
119
  in Postgresql and MariaDB DBMS within the Sequel ORM.
162
120
  It also models the Sequences to meet the needs of SQLite and MySQL users.
163
121
  email:
@@ -168,6 +126,7 @@ extra_rdoc_files:
168
126
  - README.md
169
127
  - LICENSE.md
170
128
  files:
129
+ - ".ci.gemfile"
171
130
  - ".github/ISSUE_TEMPLATE/bug_report.md"
172
131
  - ".github/ISSUE_TEMPLATE/feature_request.md"
173
132
  - ".github/PULL_REQUEST_TEMPLATE.md"