sequel-sequence 0.2.0 → 0.4.0
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 +47 -8
- data/.gitignore +3 -0
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +14 -0
- data/CONTRIBUTING.md +118 -10
- data/README.md +45 -7
- data/Rakefile +9 -1
- data/lib/sequel/sequence/database/postgresql.rb +6 -4
- data/lib/sequel/sequence/database/server/mariadb.rb +71 -0
- data/lib/sequel/sequence/database/server/mysql.rb +178 -0
- data/lib/sequel/sequence/database/sqlite.rb +139 -0
- data/lib/sequel/sequence/database.rb +26 -3
- data/lib/sequel/sequence/database_ext_connection.rb +25 -0
- data/lib/sequel/sequence/version.rb +1 -1
- data/lib/sequel/sequence.rb +5 -8
- data/sequel-sequence.gemspec +5 -2
- data/test/mariadb_test_helper.rb +32 -0
- data/test/mysql_test_helper.rb +11 -9
- data/test/sequel/mariadb_sequence_test.rb +182 -0
- data/test/sequel/mysql_sequence_test.rb +216 -43
- data/test/sequel/postgresql_sequence_test.rb +2 -2
- data/test/sequel/sqlite_sequence_test.rb +329 -0
- data/test/sqlite_test_helper.rb +30 -0
- metadata +28 -6
- data/lib/sequel/sequence/database/mysql.rb +0 -10
- data/lib/sequel/sequence/database/mysql2.rb +0 -80
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba4270613514ab8a88b24fa9034314333feba102dff7719a8e238ca1c84e67d1
|
4
|
+
data.tar.gz: 950c6494e0c9f55f6465beee78209649c784508c9c9c0dab1b9817576cbf3a7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed1505fd942d5247ea8ad64b47e1caa9cc420826ace83e4f8c075db257090576772219aa9f48007a0c9d84793f127ec82604a310688717b18ece0ceec8824d66
|
7
|
+
data.tar.gz: c781c917e87029eec161ae656bb331552e6e0f531ceb8c6f3df8b836686f9f1bc7a4f277f81c15a9c515f2861624ed73f978a7be7b7db35d29a32c09aef277f5
|
data/.github/workflows/ci.yml
CHANGED
@@ -34,16 +34,27 @@ jobs:
|
|
34
34
|
--health-timeout 5s
|
35
35
|
--health-retries 5
|
36
36
|
mysql:
|
37
|
-
image:
|
37
|
+
image: mysql:latest
|
38
38
|
env:
|
39
|
-
|
39
|
+
MYSQL_ROOT_PASSWORD: rootroot
|
40
40
|
ports:
|
41
41
|
- 3306:3306
|
42
42
|
options: >-
|
43
|
-
--health-cmd="
|
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,8 +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 -
|
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
|
70
|
+
sqlite3 --version
|
71
|
+
# mysql --host 127.0.0.1 --port ${{ job.services.mariadb.ports[3306] }} -uroot -proot -e "SHOW GRANTS FOR 'root'@'localhost'"
|
59
72
|
|
60
73
|
- name: Set up Ruby
|
61
74
|
uses: ruby/setup-ruby@v1
|
@@ -65,7 +78,11 @@ jobs:
|
|
65
78
|
|
66
79
|
- name: Create MySQL database
|
67
80
|
run: |
|
68
|
-
mysql -e 'create database test;
|
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
|
69
86
|
|
70
87
|
- name: Create PostgreSQL database
|
71
88
|
env:
|
@@ -73,19 +90,41 @@ jobs:
|
|
73
90
|
run: |
|
74
91
|
psql -c 'create database "test";' -U postgres -h localhost -p ${{ job.services.postgres.ports[5432] }}
|
75
92
|
|
76
|
-
- name:
|
77
|
-
run:
|
93
|
+
- name: Create SQLite database
|
94
|
+
run: |
|
95
|
+
mkdir ./db && touch ./db/test.sqlite3 && sqlite3 ./db/test.sqlite3
|
96
|
+
|
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
|
78
108
|
env:
|
79
109
|
TEST_POSTGRES_DATABASE: test
|
80
110
|
TEST_POSTGRES_HOST: localhost
|
81
111
|
TEST_POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
|
82
112
|
TEST_POSTGRES_USERNAME: postgres
|
83
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:
|
84
118
|
TEST_MYSQL_DATABASE: test
|
85
119
|
TEST_MYSQL_HOST: 127.0.0.1
|
86
120
|
TEST_MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}
|
87
121
|
TEST_MYSQL_USERNAME: root
|
88
|
-
TEST_MYSQL_PASSWORD:
|
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:
|
127
|
+
TEST_SQLITE_DATABASE: "db/test.sqlite3"
|
89
128
|
|
90
129
|
- name: Run Rubocop
|
91
130
|
run: bundle exec rake rubocop
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
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.0 - 2023-09-26
|
15
|
+
|
16
|
+
- [Fixed] Differences between MySQL and MariaDB.
|
17
|
+
- [Added] Gem API support for MySQL databases.
|
18
|
+
- [Changed] README.md, CONTRIBUTING.md and .gemspec description.
|
19
|
+
- [Fixed] Some API support for SQLite databases.
|
20
|
+
|
21
|
+
## v0.3.0 - 2023-09-21
|
22
|
+
|
23
|
+
- [Added] A parametrized 'IF EXISTS' condition into the drop_sequence.
|
24
|
+
- [Added] A parametrized 'IF NOT EXISTS' condition into the create_sequence.
|
25
|
+
- [Added] Gem API support for SQLite databases.
|
26
|
+
- [Fixed] Tests for the Mysql database.
|
27
|
+
|
14
28
|
## v0.2.0 - 2023-09-14
|
15
29
|
|
16
30
|
- [Added] CI features based on GitHub Actions.
|
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
|
-
|
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
|
-
|
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,8 +133,106 @@ 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
|
```
|
126
|
-
|
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
|
+
|
220
|
+
- Add a test SQLite database:
|
221
|
+
```bash
|
222
|
+
$ mkdir db && touch db/test.sqlite3
|
223
|
+
```
|
224
|
+
- Add a couple of tables in the SQLite database:
|
225
|
+
```bash
|
226
|
+
$ sqlite3 db/test.sqlite3
|
227
|
+
sqlite> create table objects(id integer primary key autoincrement);
|
228
|
+
sqlite> create table apprentices(id integer primary key autoincrement);
|
229
|
+
```
|
230
|
+
|
231
|
+
### Running tests:
|
232
|
+
|
127
233
|
```bash
|
128
|
-
bundle exec rake TEST=test/sequel/postgresql_sequence_test.rb
|
129
|
-
bundle exec rake TEST=test/sequel/
|
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
|
130
238
|
```
|
data/README.md
CHANGED
@@ -3,8 +3,9 @@
|
|
3
3
|
[![CI](https://github.com/oreol-group/sequel-sequence/actions/workflows/ci.yml/badge.svg)](https://github.com/oreol-group/sequel-sequence)
|
4
4
|
[![Gem](https://img.shields.io/gem/v/sequel-sequence.svg)](https://rubygems.org/gems/sequel-sequence)
|
5
5
|
[![Downloads total](https://img.shields.io/gem/dt/sequel-sequence.svg)](https://rubygems.org/profiles/it_architect)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/Oreol-Group/sequel-sequence.svg)](https://codeclimate.com/github/Oreol-Group/sequel-sequence)
|
6
7
|
|
7
|
-
Adds a useful interface
|
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.
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
@@ -18,10 +19,23 @@ Or add the following line to your project's Gemfile:
|
|
18
19
|
gem "sequel-sequence"
|
19
20
|
```
|
20
21
|
|
21
|
-
## Usage
|
22
|
+
## Usage with PostgreSQL and MariaDB
|
22
23
|
|
23
|
-
To create a `SEQUENCE`,
|
24
|
+
To create and delete a `SEQUENCE`, simply use the `create_sequence` and `drop_sequence` methods.
|
24
25
|
|
26
|
+
```ruby
|
27
|
+
Sequel.migration do
|
28
|
+
up do
|
29
|
+
create_sequence :position, if_exists: false
|
30
|
+
end
|
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:
|
25
39
|
```ruby
|
26
40
|
Sequel.migration do
|
27
41
|
up do
|
@@ -38,23 +52,26 @@ You can also specify the initial value and increment:
|
|
38
52
|
|
39
53
|
```ruby
|
40
54
|
create_sequence :position, increment: 2
|
55
|
+
create_sequence :position, step: 2
|
41
56
|
create_sequence :position, start: 100
|
57
|
+
create_sequence :position, if_exists: false
|
42
58
|
```
|
43
59
|
|
44
|
-
|
45
|
-
|
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
|
+
|
62
|
+
To define a column that has a sequence as its default value, use something like the following:
|
46
63
|
|
47
64
|
```ruby
|
48
65
|
Sequel.migration do
|
49
66
|
change do
|
50
|
-
create_sequence :position_id
|
67
|
+
create_sequence :position_id, if_exists: false, start: 1000
|
51
68
|
|
52
69
|
create_table(:things) do
|
53
70
|
primary_key :id
|
54
71
|
String :name, text: true
|
55
72
|
|
56
73
|
# PostgreSQL uses bigint as the sequence's default type.
|
57
|
-
Bignum :position
|
74
|
+
Bignum :position
|
58
75
|
|
59
76
|
Time :created_at, null: false
|
60
77
|
Time :updated_at, null: false
|
@@ -82,6 +99,27 @@ DB.lastval("position")
|
|
82
99
|
DB.setval("position", 1234)
|
83
100
|
```
|
84
101
|
|
102
|
+
## Usage with SQLite and MySQL
|
103
|
+
|
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`.
|
105
|
+
```sql
|
106
|
+
CREATE TABLE `name_of_your_sequence_table`
|
107
|
+
(id integer primary key autoincrement, fiction integer);
|
108
|
+
```
|
109
|
+
|
110
|
+
You might utilize the last field 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.
|
111
|
+
```ruby
|
112
|
+
create_sequence :position, if_exists: false, start: 1000, numeric_label: 1
|
113
|
+
```
|
114
|
+
and
|
115
|
+
```ruby
|
116
|
+
DB.nextval_with_label(:position, 1)
|
117
|
+
```
|
118
|
+
|
119
|
+
By default, `fiction` has a zero value.
|
120
|
+
|
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
|
+
|
85
123
|
## Maintainer
|
86
124
|
|
87
125
|
- [Nikolai Bocharov](https://github.com/oreol-group)
|
data/Rakefile
CHANGED
@@ -7,7 +7,15 @@ 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
|
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
|
+
end
|
14
|
+
|
15
|
+
Rake::TestTask.new(:mysql) do |t|
|
16
|
+
t.libs << 'test'
|
17
|
+
t.libs << 'lib'
|
18
|
+
t.test_files = FileList['test/sequel/mysql_sequence_test.rb']
|
11
19
|
end
|
12
20
|
|
13
21
|
RuboCop::RakeTask.new
|
@@ -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
|
@@ -35,9 +36,10 @@ module Sequel
|
|
35
36
|
|
36
37
|
def create_sequence(name, options = {})
|
37
38
|
increment = options[:increment] || options[:step]
|
39
|
+
if_exists = build_exists_condition(options[:if_exists])
|
38
40
|
name = quote_name(name.to_s)
|
39
41
|
|
40
|
-
sql = ["CREATE SEQUENCE
|
42
|
+
sql = ["CREATE SEQUENCE #{if_exists} #{name}"]
|
41
43
|
sql << "INCREMENT BY #{increment}" if increment
|
42
44
|
sql << "START WITH #{options[:start]}" if options[:start]
|
43
45
|
sql << ';'
|
@@ -48,7 +50,7 @@ module Sequel
|
|
48
50
|
|
49
51
|
def drop_sequence(name)
|
50
52
|
name = quote_name(name.to_s)
|
51
|
-
sql = "DROP SEQUENCE #{name}"
|
53
|
+
sql = "DROP SEQUENCE IF EXISTS #{name}"
|
52
54
|
run(sql)
|
53
55
|
end
|
54
56
|
|
@@ -61,7 +63,7 @@ module Sequel
|
|
61
63
|
out
|
62
64
|
end
|
63
65
|
|
64
|
-
# for
|
66
|
+
# for Postgres
|
65
67
|
def currval(name)
|
66
68
|
name = quote(name.to_s)
|
67
69
|
out = nil
|
@@ -71,7 +73,7 @@ module Sequel
|
|
71
73
|
out
|
72
74
|
end
|
73
75
|
|
74
|
-
# for
|
76
|
+
# for MariaDB
|
75
77
|
alias lastval currval
|
76
78
|
|
77
79
|
def setval(name, value)
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mariadb
|
4
|
+
def quote_column_name(name)
|
5
|
+
"`#{name.gsub('`', '``')}`"
|
6
|
+
end
|
7
|
+
|
8
|
+
def quote_sequence_name(name)
|
9
|
+
"`#{name.gsub(/[`"']/, '')}`"
|
10
|
+
end
|
11
|
+
|
12
|
+
def check_sequences
|
13
|
+
fetch("SHOW FULL TABLES WHERE Table_type = 'SEQUENCE';").all.to_a
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_sequence(name, options = {})
|
17
|
+
increment = options[:increment] || options[:step]
|
18
|
+
if_exists = build_exists_condition(options[:if_exists])
|
19
|
+
name = quote_name(name.to_s)
|
20
|
+
|
21
|
+
sql = ["CREATE SEQUENCE #{if_exists || Sequel::Database::IF_NOT_EXISTS} #{name}"]
|
22
|
+
sql << "INCREMENT BY #{increment}" if increment
|
23
|
+
sql << "START WITH #{options[:start]}" if options[:start]
|
24
|
+
sql << ';'
|
25
|
+
|
26
|
+
run(sql.join("\n"))
|
27
|
+
end
|
28
|
+
|
29
|
+
def drop_sequence(name)
|
30
|
+
name = quote_name(name.to_s)
|
31
|
+
sql = "DROP SEQUENCE IF EXISTS #{name}"
|
32
|
+
run(sql)
|
33
|
+
end
|
34
|
+
|
35
|
+
def nextval(name)
|
36
|
+
name = quote(name.to_s)
|
37
|
+
out = nil
|
38
|
+
fetch("SELECT nextval(#{name});") do |row|
|
39
|
+
out = row["nextval(#{name})".to_sym]
|
40
|
+
end
|
41
|
+
out
|
42
|
+
end
|
43
|
+
|
44
|
+
def lastval(name)
|
45
|
+
name = quote(name.to_s)
|
46
|
+
out = nil
|
47
|
+
fetch("SELECT lastval(#{name});") do |row|
|
48
|
+
out = row["lastval(#{name})".to_sym]
|
49
|
+
end
|
50
|
+
out
|
51
|
+
end
|
52
|
+
|
53
|
+
alias currval lastval
|
54
|
+
|
55
|
+
def setval(name, value)
|
56
|
+
name = quote(name.to_s)
|
57
|
+
out = nil
|
58
|
+
fetch("SELECT setval(#{name}, #{value});") do |row|
|
59
|
+
out = row["setval(#{name}, #{value})".to_sym]
|
60
|
+
end
|
61
|
+
out
|
62
|
+
end
|
63
|
+
|
64
|
+
def set_column_default_nextval(table, column, sequence)
|
65
|
+
table = table.to_s
|
66
|
+
column = column.to_s
|
67
|
+
sequence = quote(sequence.to_s)
|
68
|
+
run "ALTER TABLE IF EXISTS #{table} " \
|
69
|
+
"ALTER COLUMN #{column} SET DEFAULT nextval(#{sequence});"
|
70
|
+
end
|
71
|
+
end
|