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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7012e03aa326861bef294edcf042850f662f9122664da409aa0dd764d9375d9
4
- data.tar.gz: b309b36a931be17ee7cb628c21083ef8543bb60139a249aa9fd1d42378321297
3
+ metadata.gz: 749ea113ce41f1f7f70f620d5aa70deac470a76491cdb36b4bd8e5c739487a07
4
+ data.tar.gz: 9628bc8f57ad5a1ead78d82eaa8348963876c93a6be40df0d8c9dabfab928f76
5
5
  SHA512:
6
- metadata.gz: 7c13a9a6e000990aaee2bdcca4d3d7519a7164c3217d4164f35e40e43deb76f652215169396647568cb03cf111d2a103f2f0f5d5f45e174b8107588e4be1808f
7
- data.tar.gz: e9483ab69ea26d780164a4e4683d75c2bd86d8c5d92983b506645c5544a3ebc277f25f12a463b1f959f675f0e780eaf3b22a82b9560e247f16f2a895518d0e53
6
+ metadata.gz: d0dbf107132dad0bcd23bcf5da23cb9fb121119dafb109865978a6bf26fe8465985875fd6d33b72422cd57e1d5b74b54fce065ba7a2fdf21b0d76f032b7c149e
7
+ data.tar.gz: bd1bbeaf56ddcd37b4bc86b6b83ad06e85212c7f09bb32db03004bfc9599dd240b141ea6c6130d64fe8b85471c3e6bb7cd8537714b7b6f34f744142b117ce606
@@ -34,16 +34,27 @@ jobs:
34
34
  --health-timeout 5s
35
35
  --health-retries 5
36
36
  mysql:
37
- image: mariadb:11.1
37
+ image: mysql:latest
38
38
  env:
39
- MARIADB_ROOT_PASSWORD: root
39
+ MYSQL_ROOT_PASSWORD: rootroot
40
40
  ports:
41
41
  - 3306:3306
42
42
  options: >-
43
- --health-cmd="healthcheck.sh --connect --innodb_initialized"
43
+ --health-cmd="mysqladmin ping"
44
44
  --health-interval 10s
45
45
  --health-timeout 5s
46
46
  --health-retries 3
47
+ # mariadb:
48
+ # image: mariadb:11.1
49
+ # env:
50
+ # MARIADB_ROOT_PASSWORD: root
51
+ # ports:
52
+ # - 3306:3306
53
+ # options: >-
54
+ # --health-cmd="healthcheck.sh --connect --innodb_initialized"
55
+ # --health-interval 10s
56
+ # --health-timeout 5s
57
+ # --health-retries 3
47
58
 
48
59
  env:
49
60
  SEQUEL: ${{ matrix.sequel }}
@@ -54,9 +65,10 @@ jobs:
54
65
  - name: Install db dependencies and check connections
55
66
  run: |
56
67
  DEBIAN_FRONTEND="noninteractive" sudo apt-get install -yqq mysql-client libmysqlclient-dev postgresql-client libpq-dev
57
- mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot -e "SHOW GRANTS FOR 'root'@'localhost'"
68
+ mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -prootroot -e "SHOW GRANTS FOR 'root'@'localhost'"
58
69
  env PGPASSWORD=postgres psql -h localhost -p ${{ job.services.postgres.ports[5432] }} -U postgres -l
59
70
  sqlite3 --version
71
+ # mysql --host 127.0.0.1 --port ${{ job.services.mariadb.ports[3306] }} -uroot -proot -e "SHOW GRANTS FOR 'root'@'localhost'"
60
72
 
61
73
  - name: Set up Ruby
62
74
  uses: ruby/setup-ruby@v1
@@ -66,7 +78,11 @@ jobs:
66
78
 
67
79
  - name: Create MySQL database
68
80
  run: |
69
- mysql -e 'create database test; use test; create table if not exists wares(id int auto_increment, primary key(id)); create table if not exists builders(id int auto_increment, primary key(id));' --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot
81
+ mysql -e 'create database test;' --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -prootroot
82
+
83
+ # - name: Create MariaDB database
84
+ # run: |
85
+ # mysql -e 'create database test; use test; create table if not exists wares(id int auto_increment, primary key(id)); create table if not exists builders(id int auto_increment, primary key(id));' --host 127.0.0.1 --port ${{ job.services.mariadb.ports[3306] }} -uroot -proot
70
86
 
71
87
  - name: Create PostgreSQL database
72
88
  env:
@@ -78,20 +94,40 @@ jobs:
78
94
  run: |
79
95
  mkdir ./db && touch ./db/test.sqlite3 && sqlite3 ./db/test.sqlite3
80
96
 
81
- - name: Run Tests
82
- run: bundle exec rake test
97
+ # - name: Run MariaDB Tests
98
+ # run: bundle exec rake TEST=test/sequel/mariadb_sequence_test.rb
99
+ # env:
100
+ # TEST_MARIA_DATABASE: test
101
+ # TEST_MARIA_HOST: 127.0.0.1
102
+ # TEST_MARIA_PORT: ${{ job.services.mariadb.ports[3306] }}
103
+ # TEST_MARIA_USERNAME: root
104
+ # TEST_MARIA_PASSWORD: root
105
+
106
+ - name: Run PostgreSQL Tests
107
+ run: bundle exec rake TEST=test/sequel/postgresql_sequence_test.rb
83
108
  env:
84
109
  TEST_POSTGRES_DATABASE: test
85
110
  TEST_POSTGRES_HOST: localhost
86
111
  TEST_POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
87
112
  TEST_POSTGRES_USERNAME: postgres
88
113
  TEST_POSTGRES_PASSWORD: postgres
114
+
115
+ - name: Run MySQL Tests
116
+ run: bundle exec rake TEST=test/sequel/mysql_sequence_test.rb
117
+ env:
89
118
  TEST_MYSQL_DATABASE: test
90
119
  TEST_MYSQL_HOST: 127.0.0.1
91
120
  TEST_MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}
92
121
  TEST_MYSQL_USERNAME: root
93
- TEST_MYSQL_PASSWORD: root
122
+ TEST_MYSQL_PASSWORD: rootroot
123
+
124
+ - name: Run SQLite Tests
125
+ run: bundle exec rake TEST=test/sequel/sqlite_sequence_test.rb
126
+ env:
94
127
  TEST_SQLITE_DATABASE: "db/test.sqlite3"
95
128
 
129
+ - name: Run Mock Tests
130
+ run: bundle exec rake TEST=test/sequel/mock_sequence_test.rb
131
+
96
132
  - name: Run Rubocop
97
133
  run: bundle exec rake rubocop
data/.rubocop.yml CHANGED
@@ -35,3 +35,6 @@ Style/Documentation:
35
35
  Lint/ConstantDefinitionInBlock:
36
36
  Exclude:
37
37
  - test/**/*
38
+
39
+ Metrics/ModuleLength:
40
+ Max: 150
data/CHANGELOG.md CHANGED
@@ -11,6 +11,20 @@ Prefix your message with one of the following:
11
11
  - [Security] in case of vulnerabilities.
12
12
  -->
13
13
 
14
+ ## v0.4.1 - 2023-09-28
15
+
16
+ - [Added] Important notice to README.md.
17
+ - [Added] MySQL tests cover 100%.
18
+ - [Added] SQLite tests cover 100%.
19
+ - [Added] Mock connection to check for additional ORM exceptions.
20
+
21
+ ## v0.4.0 - 2023-09-26
22
+
23
+ - [Fixed] Differences between MySQL and MariaDB.
24
+ - [Added] Gem API support for MySQL databases.
25
+ - [Changed] README.md, CONTRIBUTING.md and .gemspec description.
26
+ - [Fixed] Some API support for SQLite databases.
27
+
14
28
  ## v0.3.0 - 2023-09-21
15
29
 
16
30
  - [Added] A parametrized 'IF EXISTS' condition into the drop_sequence.
data/CONTRIBUTING.md CHANGED
@@ -67,14 +67,21 @@ your contribution is according to how this project works.
67
67
  - This project uses [Rubocop](https://rubocop.org) to enforce code style. Before
68
68
  submitting your changes, make sure your tests are passing and code conforms to
69
69
  the expected style by running `rake`.
70
+ ```bash
71
+ $ bundle exec rake rubocop
72
+ ```
70
73
  - Do not change the library version. This will be done by the maintainer
71
74
  whenever a new version is about to be released.
72
75
 
73
- ### Ruby tests
76
+ ## Ruby tests
77
+
78
+ Key points in preparing RDBMS and using tests.
79
+
80
+ ### Preparing a PostgreSQL database
74
81
 
75
82
  - Make sure you have a test PostgreSQL database:
76
83
  ```bash
77
- sudo psql -U USER_NAME -d test
84
+ $ sudo psql -U USER_NAME -d test
78
85
  test=# \dt
79
86
  List of relations
80
87
  Schema | Name | Type | Owner
@@ -84,28 +91,31 @@ test=# \dt
84
91
  ```
85
92
  and role `postgres`
86
93
  ```bash
87
- psql -d test -c 'SELECT rolname FROM pg_roles;'
94
+ $ psql -d test -c 'SELECT rolname FROM pg_roles;'
88
95
  rolname
89
96
  ---------------------------
90
97
  postgres
91
98
  ```
92
99
  - If none of them exist, create role
93
100
  ```bash
94
- psql -d postgres -c "create role postgres superuser createdb login password 'postgres';"
101
+ $ psql -d postgres -c "create role postgres superuser createdb login password 'postgres';"
95
102
  ```
96
103
  and database with a couple of tables:
97
104
 
98
105
  ```bash
99
- sudo psql -U postgres -d postgres
106
+ $ sudo psql -U postgres -d postgres
100
107
  postgres=# CREATE DATABASE test;
101
108
  postgres=# \c test
102
109
  test=# CREATE TABLE IF NOT EXISTS things ();
103
110
  test=# CREATE TABLE IF NOT EXISTS masters ();
104
111
  test=# \q
105
112
  ```
106
- - Make sure you have a test MySQL database:
113
+
114
+ ### Preparing a MariaDB database
115
+
116
+ - Make sure you have a test MariaDB database:
107
117
  ```bash
108
- mysql
118
+ $ mysql
109
119
  MariaDB [(none)]> show databases;
110
120
  MariaDB [(none)]> USE test;
111
121
  MariaDB [test]> SHOW TABLES;
@@ -123,19 +133,107 @@ MariaDB [(none)]> USE test;
123
133
  MariaDB [test]> CREATE TABLE IF NOT EXISTS wares(id int auto_increment, primary key(id));
124
134
  MariaDB [test]> CREATE TABLE IF NOT EXISTS builders(id int auto_increment, primary key(id));
125
135
  ```
136
+
137
+ ### Preparing a MySQL database
138
+
139
+ The optimal way to share Mysql and MariaDB on the same computer is to utilize docker containers.
140
+
141
+ - Check the local availability of a Mysql container:
142
+ ```bash
143
+ $ docker image ls
144
+ REPOSITORY TAG IMAGE ID CREATED SIZE
145
+ mysql latest 3503aa5f0751 2 days ago 599MB
146
+ ```
147
+ - If there is no distribution package download the docker container with Mysql to the local computer:
148
+ ```bash
149
+ $ docker run -p 3360:3306 --name test_mysql -e MYSQL_ROOT_PASSWORD=rootroot -d mysql:latest
150
+ ```
151
+ - Show running containers:
152
+ ```bash
153
+ $ docker container ls
154
+ CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
155
+ a0d3476699f4 mysql:latest "docker-entrypoint.s…" 17 minutes ago Up 11 seconds 33060/tcp, 0.0.0.0:3360->3306/tcp test_mysql
156
+ ```
157
+ - Launch the MySQL container if it is still not running:
158
+ ```bash
159
+ $ docker start test_mysql
160
+ ```
161
+
162
+ To create a new database for tests, you could run the MySQL client in the terminal as follows:
163
+
164
+ - Check the IP address of the running MySQL server:
165
+ ```bash
166
+ $ docker inspect test_mysql
167
+ ...
168
+ "IPAddress": "172.17.0.2",
169
+ ...
170
+ ```
171
+ - Run the MySQL client:
172
+ ```bash
173
+ $ docker run -e MYSQL_ROOT_PASSWORD=rootroot -it mysql /bin/bash
174
+ bash-4.4#
175
+ ```
176
+ - Launch the MySQL shell:
177
+ ```bash
178
+ bash-4.4# mysql -h 172.17.0.2 -u root -p
179
+ Enter password:
180
+ Welcome to the MySQL monitor. Commands end with ; or \g.
181
+ Your MySQL connection id is 8
182
+ Server version: 8.1.0 MySQL Community Server - GPL
183
+
184
+ Copyright (c) 2000, 2023, Oracle and/or its affiliates.
185
+
186
+ Oracle is a registered trademark of Oracle Corporation and/or its
187
+ affiliates. Other names may be trademarks of their respective
188
+ owners.
189
+
190
+ Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
191
+
192
+ mysql>
193
+ ```
194
+ - Make sure that the test database is available:
195
+ ```bash
196
+ mysql> show schemas;
197
+ +--------------------+
198
+ | Database |
199
+ +--------------------+
200
+ | information_schema |
201
+ | mysql |
202
+ | performance_schema |
203
+ | sys |
204
+ | test |
205
+ +--------------------+
206
+ ```
207
+ - If the test danabase doesn't exists, create it:
208
+ ```bash
209
+ mysql> CREATE DATABASE test;
210
+ ```
211
+ - Create a couple of tables:
212
+ ```bash
213
+ mysql> USE test;
214
+ mysql> CREATE TABLE IF NOT EXISTS stuffs(id int auto_increment, primary key(id));
215
+ mysql> CREATE TABLE IF NOT EXISTS creators(id int auto_increment, primary key(id));
216
+ ```
217
+
218
+ ### Preparing a SQLite database
219
+
126
220
  - Add a test SQLite database:
127
221
  ```bash
128
- mkdir db && touch db/test.sqlite3
222
+ $ mkdir db && touch db/test.sqlite3
129
223
  ```
130
224
  - Add a couple of tables in the SQLite database:
131
225
  ```bash
132
- sqlite3 db/test.sqlite3
226
+ $ sqlite3 db/test.sqlite3
133
227
  sqlite> create table objects(id integer primary key autoincrement);
134
228
  sqlite> create table apprentices(id integer primary key autoincrement);
135
229
  ```
136
- - Run the tests separately:
230
+
231
+ ### Running tests:
232
+
137
233
  ```bash
138
- bundle exec rake TEST=test/sequel/postgresql_sequence_test.rb
139
- bundle exec rake TEST=test/sequel/mysql_sequence_test.rb
140
- bundle exec rake TEST=test/sequel/sqlite_sequence_test.rb
234
+ $ bundle exec rake TEST=test/sequel/postgresql_sequence_test.rb
235
+ $ bundle exec rake TEST=test/sequel/mariadb_sequence_test.rb
236
+ $ bundle exec rake TEST=test/sequel/mysql_sequence_test.rb
237
+ $ bundle exec rake TEST=test/sequel/sqlite_sequence_test.rb
238
+ $ bundle exec rake TEST=test/sequel/mock_sequence_test.rb
141
239
  ```
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Downloads total](https://img.shields.io/gem/dt/sequel-sequence.svg)](https://rubygems.org/profiles/it_architect)
6
6
  [![Code Climate](https://codeclimate.com/github/Oreol-Group/sequel-sequence.svg)](https://codeclimate.com/github/Oreol-Group/sequel-sequence)
7
7
 
8
- Adds a useful interface and support for PostgreSQL and MySQL `SEQUENCE` on Sequel migrations. Gem includes functionality to cover the needs of SQLite users as well.
8
+ Adds a useful interface for PostgreSQL and MariaDB `SEQUENCE` on Sequel migrations. This Gem includes functionality to meet the needs of MySQL and SQLite users as well.
9
9
 
10
10
  ## Installation
11
11
 
@@ -21,7 +21,7 @@ gem "sequel-sequence"
21
21
 
22
22
  ## Usage with PostgreSQL and MariaDB
23
23
 
24
- To create a `SEQUENCE`, just use the method `create_sequence`.
24
+ To create and delete a `SEQUENCE`, simply use the `create_sequence` and `drop_sequence` methods.
25
25
 
26
26
  ```ruby
27
27
  Sequel.migration do
@@ -29,26 +29,42 @@ Sequel.migration do
29
29
  create_sequence :position, if_exists: false
30
30
  end
31
31
 
32
+ down do
33
+ drop_sequence :position, if_exists: true
34
+ end
35
+ end
36
+ ```
37
+
38
+ It would also be correct to write:
39
+ ```ruby
40
+ Sequel.migration do
41
+ up do
42
+ create_sequence :position
43
+ end
44
+
32
45
  down do
33
46
  drop_sequence :position
34
47
  end
35
48
  end
36
49
  ```
37
50
 
38
- You can also specify the initial value and increment:
51
+ You can also specify the following optional parameters: `if_exists` – a condition of acceptability; `start` – an initial value; `increment` or `step` – step size to the next auto incrementing value:
39
52
 
40
53
  ```ruby
41
54
  create_sequence :position, increment: 2
55
+ create_sequence :position, step: 2
42
56
  create_sequence :position, start: 100
43
57
  create_sequence :position, if_exists: false
44
58
  ```
45
59
 
60
+ The `increment` and `step` parameters have the same meaning. By default their values are 1. The default value of `start` is 1 as well.
61
+
46
62
  To define a column that has a sequence as its default value, use something like the following:
47
63
 
48
64
  ```ruby
49
65
  Sequel.migration do
50
66
  change do
51
- create_sequence :position_id, if_exists: false, start: 1
67
+ create_sequence :position_id, if_exists: false, start: 1000
52
68
 
53
69
  create_table(:things) do
54
70
  primary_key :id
@@ -83,23 +99,30 @@ DB.lastval("position")
83
99
  DB.setval("position", 1234)
84
100
  ```
85
101
 
86
- ## Usage with SQLite
102
+ ## Usage with SQLite and MySQL
87
103
 
88
- In SQLite, the sequence functionality is implemented by registering tables in the database with a primary key of `id` and an additional integer field `fiction`.
104
+ The sequence functionality for SQLite or MySQL databases is implemented by registering tables in the database with a primary key of `id` and an additional integer field `fiction`.
89
105
  ```sql
90
106
  CREATE TABLE `name_of_your_sequence_table`
91
107
  (id integer primary key autoincrement, fiction integer);
92
108
  ```
93
109
 
94
- You might utilize the last one as a numeric label to collect statistics on the operation of the end-to-end counter `"name_of_your_sequence_table".id` within the application.
95
-
110
+ You might utilize the last field `fiction` as a numeric label to collect statistics on the operation of the end-to-end counter `"name_of_your_sequence_table".id` within the application.
96
111
  ```ruby
97
- DB.nextval_with_label("position", 1)
112
+ create_sequence :position, if_exists: false, start: 1000, numeric_label: 1
113
+ ```
114
+ and
115
+ ```ruby
116
+ DB.nextval_with_label(:position, 1)
98
117
  ```
99
118
 
100
119
  By default, `fiction` has a zero value.
101
120
 
102
- Otherwise, the operation of this gem for SQLite is identical to the ways of using Sequence in more advanced databases.
121
+ Otherwise, the operation of this gem for SQLite and MySQL is similar to the ways of using Sequence in more advanced RDBMS. There is only one difference here, you won't be able to change the increment value from 1 to another using the `increment` or `step` parameter.
122
+
123
+ ## Known issues you may encounter
124
+
125
+ This solution does not allow you to simultaneously work with MySQL and MariaDB databases from one application. If such a need arises, move the data processing functionality to different microservices.
103
126
 
104
127
  ## Maintainer
105
128
 
data/Rakefile CHANGED
@@ -7,7 +7,40 @@ require 'rubocop/rake_task'
7
7
  Rake::TestTask.new(:test) do |t|
8
8
  t.libs << 'test'
9
9
  t.libs << 'lib'
10
- t.test_files = FileList['test/**/*_test.rb']
10
+ t.test_files = FileList['test/sequel/postgresql_sequence_test.rb',
11
+ 'test/sequel/mariadb_sequence_test.rb',
12
+ 'test/sequel/sqlite_sequence_test.rb',
13
+ 'test/sequel/mock_sequence_test.rb']
14
+ end
15
+
16
+ Rake::TestTask.new(:mysql) do |t|
17
+ t.libs << 'test'
18
+ t.libs << 'lib'
19
+ t.test_files = FileList['test/sequel/mysql_sequence_test.rb']
20
+ end
21
+
22
+ Rake::TestTask.new(:postgresql) do |t|
23
+ t.libs << 'test'
24
+ t.libs << 'lib'
25
+ t.test_files = FileList['test/sequel/postgresql_sequence_test.rb']
26
+ end
27
+
28
+ Rake::TestTask.new(:mariadb) do |t|
29
+ t.libs << 'test'
30
+ t.libs << 'lib'
31
+ t.test_files = FileList['test/sequel/mariadb_sequence_test.rb']
32
+ end
33
+
34
+ Rake::TestTask.new(:sqlite) do |t|
35
+ t.libs << 'test'
36
+ t.libs << 'lib'
37
+ t.test_files = FileList['test/sequel/sqlite_sequence_test.rb']
38
+ end
39
+
40
+ Rake::TestTask.new(:mock) do |t|
41
+ t.libs << 'test'
42
+ t.libs << 'lib'
43
+ t.test_files = FileList['test/sequel/mock_sequence_test.rb']
11
44
  end
12
45
 
13
46
  RuboCop::RakeTask.new
data/lib/sequel/error.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sequel
4
- class MethodNotAllowed < StandardError
5
- METHOD_NOT_ALLOWED = 'Method not allowed'
4
+ (
5
+ # Error raised when attempting to utilize an invalid adapter to SEQUENCE interface.
6
+ MethodNotAllowed = Class.new(Error)
7
+ ).name
6
8
 
7
- # Initialize a new Error object
8
- def initialize(message = '')
9
- super(message)
10
- end
9
+ class Database
10
+ METHOD_NOT_ALLOWED = 'Method not allowed'
11
11
  end
12
12
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  # https://sequel.jeremyevans.net/rdoc/files/doc/sql_rdoc.html
4
4
  # https://github.com/jeremyevans/sequel/blob/master/lib/sequel/database/connecting.rb
5
+ # See information about disable_insert_returning in https://github.com/jeremyevans/sequel/blob/master/doc/release_notes/4.9.0.txt
5
6
  module Sequel
6
7
  module Sequence
7
8
  module Database
@@ -62,7 +63,7 @@ module Sequel
62
63
  out
63
64
  end
64
65
 
65
- # for db.database_type = :postgres
66
+ # for Postgres
66
67
  def currval(name)
67
68
  name = quote(name.to_s)
68
69
  out = nil
@@ -72,7 +73,7 @@ module Sequel
72
73
  out
73
74
  end
74
75
 
75
- # for db.database_type = :mysql2
76
+ # for MariaDB
76
77
  alias lastval currval
77
78
 
78
79
  def setval(name, value)
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sequel
4
+ module Sequence
5
+ module Database
6
+ module Server
7
+ module Mariadb
8
+ def quote_column_name(name)
9
+ "`#{name.gsub('`', '``')}`"
10
+ end
11
+
12
+ def quote_sequence_name(name)
13
+ "`#{name.gsub(/[`"']/, '')}`"
14
+ end
15
+
16
+ def check_sequences
17
+ fetch("SHOW FULL TABLES WHERE Table_type = 'SEQUENCE';").all.to_a
18
+ end
19
+
20
+ def create_sequence(name, options = {})
21
+ increment = options[:increment] || options[:step]
22
+ if_exists = build_exists_condition(options[:if_exists])
23
+ name = quote_name(name.to_s)
24
+
25
+ sql = ["CREATE SEQUENCE #{if_exists || Sequel::Database::IF_NOT_EXISTS} #{name}"]
26
+ sql << "INCREMENT BY #{increment}" if increment
27
+ sql << "START WITH #{options[:start]}" if options[:start]
28
+ sql << ';'
29
+
30
+ run(sql.join("\n"))
31
+ end
32
+
33
+ def drop_sequence(name)
34
+ name = quote_name(name.to_s)
35
+ sql = "DROP SEQUENCE IF EXISTS #{name}"
36
+ run(sql)
37
+ end
38
+
39
+ def nextval(name)
40
+ name = quote(name.to_s)
41
+ out = nil
42
+ fetch("SELECT nextval(#{name});") do |row|
43
+ out = row["nextval(#{name})".to_sym]
44
+ end
45
+ out
46
+ end
47
+
48
+ def lastval(name)
49
+ name = quote(name.to_s)
50
+ out = nil
51
+ fetch("SELECT lastval(#{name});") do |row|
52
+ out = row["lastval(#{name})".to_sym]
53
+ end
54
+ out
55
+ end
56
+
57
+ alias currval lastval
58
+
59
+ def setval(name, value)
60
+ name = quote(name.to_s)
61
+ out = nil
62
+ fetch("SELECT setval(#{name}, #{value});") do |row|
63
+ out = row["setval(#{name}, #{value})".to_sym]
64
+ end
65
+ out
66
+ end
67
+
68
+ def set_column_default_nextval(table, column, sequence)
69
+ table = table.to_s
70
+ column = column.to_s
71
+ sequence = quote(sequence.to_s)
72
+ run "ALTER TABLE IF EXISTS #{table} " \
73
+ "ALTER COLUMN #{column} SET DEFAULT nextval(#{sequence});"
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end