sequel-sequence 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/CHANGELOG.md +16 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/CONTRIBUTING.md +120 -0
- data/Gemfile +5 -0
- data/LICENSE.md +20 -0
- data/README.md +108 -0
- data/Rakefile +15 -0
- data/lib/sequel/error.rb +12 -0
- data/lib/sequel/sequence/database/mysql.rb +10 -0
- data/lib/sequel/sequence/database/mysql2.rb +80 -0
- data/lib/sequel/sequence/database/postgresql.rb +96 -0
- data/lib/sequel/sequence/database.rb +47 -0
- data/lib/sequel/sequence/version.rb +7 -0
- data/lib/sequel/sequence.rb +32 -0
- data/lib/sequel-sequence.rb +3 -0
- data/sequel-sequence.gemspec +45 -0
- data/test/mysql_test_helper.rb +28 -0
- data/test/postgresql_test_helper.rb +28 -0
- data/test/sequel/mysql_sequence_test.rb +172 -0
- data/test/sequel/postgresql_sequence_test.rb +183 -0
- data/test/test_helper.rb +11 -0
- metadata +197 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 10902f0e1d1afb59a3461ea44138203c9ddf3f0455b49ffe811c7b23aadc8ede
|
4
|
+
data.tar.gz: 6e928d7c8827d2cc67c32d4fe28156490b99b187cbf6cbc19f546c2706e55087
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 545bb293f276fd0f92441941fa9efc7dc432fd1b1a5131bb10a7465aba833db72f2ca2e29abc3f6cc427b958df3bc47354abf1d9d420efa77d1d610de85e2166
|
7
|
+
data.tar.gz: 20273fe64a4e90fd2f8345ea8b08d96819bb4c52ff26684b2e67d49674c0360fc0926a66c565041b6a16f8e49d348133cca80cc629c4c14614a1116eb3e6dc60
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
<!--
|
4
|
+
Prefix your message with one of the following:
|
5
|
+
|
6
|
+
- [Added] for new features.
|
7
|
+
- [Changed] for changes in existing functionality.
|
8
|
+
- [Deprecated] for soon-to-be removed features.
|
9
|
+
- [Removed] for now removed features.
|
10
|
+
- [Fixed] for any bug fixes.
|
11
|
+
- [Security] in case of vulnerabilities.
|
12
|
+
-->
|
13
|
+
|
14
|
+
## v0.1.0 - 2023-09-10
|
15
|
+
|
16
|
+
- Initial release.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
it.architect@yahoo.com.
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# Contributing to sequel-sequence
|
2
|
+
|
3
|
+
👍🎉 First off, thanks for taking the time to contribute! 🎉👍
|
4
|
+
|
5
|
+
The following is a set of guidelines for contributing to this project. These are
|
6
|
+
mostly guidelines, not rules. Use your best judgment, and feel free to propose
|
7
|
+
changes to this document in a pull request.
|
8
|
+
|
9
|
+
## Code of Conduct
|
10
|
+
|
11
|
+
Everyone interacting in this project's codebases, issue trackers, chat rooms and
|
12
|
+
mailing lists is expected to follow the [code of conduct](https://github.com/fnando/sequel-sequence/blob/main/CODE_OF_CONDUCT.md).
|
13
|
+
|
14
|
+
## Reporting bugs
|
15
|
+
|
16
|
+
This section guides you through submitting a bug report. Following these
|
17
|
+
guidelines helps maintainers and the community understand your report, reproduce
|
18
|
+
the behavior, and find related reports.
|
19
|
+
|
20
|
+
- Before creating bug reports, please check the open issues; somebody may
|
21
|
+
already have submitted something similar, and you may not need to create a new
|
22
|
+
one.
|
23
|
+
- When you are creating a bug report, please include as many details as
|
24
|
+
possible, with an example reproducing the issue.
|
25
|
+
|
26
|
+
## Contributing with code
|
27
|
+
|
28
|
+
Before making any radicals changes, please make sure you discuss your intention
|
29
|
+
by [opening an issue on Github](https://github.com/fnando/sequel-sequence/issues).
|
30
|
+
|
31
|
+
When you're ready to make your pull request, follow checklist below to make sure
|
32
|
+
your contribution is according to how this project works.
|
33
|
+
|
34
|
+
1. [Fork](https://help.github.com/forking/) sequel-sequence
|
35
|
+
2. Create a topic branch - `git checkout -b my_branch`
|
36
|
+
3. Make your changes using [descriptive commit messages](#commit-messages)
|
37
|
+
4. Update CHANGELOG.md describing your changes by adding an entry to the
|
38
|
+
"Unreleased" section. If this section is not available, create one right
|
39
|
+
before the last version.
|
40
|
+
5. Push to your branch - `git push origin my_branch`
|
41
|
+
6. [Create a pull request](https://docs.github.com/articles/creating-a-pull-request)
|
42
|
+
7. That's it!
|
43
|
+
|
44
|
+
## Styleguides
|
45
|
+
|
46
|
+
### Commit messages
|
47
|
+
|
48
|
+
- Use the present tense ("Add feature" not "Added feature")
|
49
|
+
- Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
|
50
|
+
- Limit the first line to 72 characters or less
|
51
|
+
- Reference issues and pull requests liberally after the first line
|
52
|
+
|
53
|
+
### Changelog
|
54
|
+
|
55
|
+
- Add a message describing your changes to the "Unreleased" section. The
|
56
|
+
changelog message should follow the same style as the commit message.
|
57
|
+
- Prefix your message with one of the following:
|
58
|
+
- `[Added]` for new features.
|
59
|
+
- `[Changed]` for changes in existing functionality.
|
60
|
+
- `[Deprecated]` for soon-to-be removed features.
|
61
|
+
- `[Removed]` for now removed features.
|
62
|
+
- `[Fixed]` for any bug fixes.
|
63
|
+
- `[Security]` in case of vulnerabilities.
|
64
|
+
|
65
|
+
### Ruby code
|
66
|
+
|
67
|
+
- This project uses [Rubocop](https://rubocop.org) to enforce code style. Before
|
68
|
+
submitting your changes, make sure your tests are passing and code conforms to
|
69
|
+
the expected style by running `rake`.
|
70
|
+
- Do not change the library version. This will be done by the maintainer
|
71
|
+
whenever a new version is about to be released.
|
72
|
+
|
73
|
+
### Ruby tests
|
74
|
+
|
75
|
+
- Make sure you have a test PostgreSQL database:
|
76
|
+
```bash
|
77
|
+
sudo psql -U USER_NAME -d test
|
78
|
+
test=# \dt
|
79
|
+
List of relations
|
80
|
+
Schema | Name | Type | Owner
|
81
|
+
--------+---------+-------+-----------
|
82
|
+
public | masters | table | USER_NAME
|
83
|
+
public | things | table | USER_NAME
|
84
|
+
(2 rows)
|
85
|
+
```
|
86
|
+
- If it doesn't exists, create one with a couple of tables:
|
87
|
+
```bash
|
88
|
+
sudo psql -U USER_NAME -d postgres
|
89
|
+
postgres=# CREATE DATABASE test;
|
90
|
+
postgres=# \c test
|
91
|
+
test=# CREATE TABLE IF NOT EXISTS things ();
|
92
|
+
test=# CREATE TABLE IF NOT EXISTS masters ();
|
93
|
+
test=# \q
|
94
|
+
```
|
95
|
+
- Make sure you have a test MySQL database:
|
96
|
+
```bash
|
97
|
+
mysql
|
98
|
+
MariaDB [(none)]> show databases;
|
99
|
+
MariaDB [(none)]> USE test;
|
100
|
+
MariaDB [test]> SHOW TABLES;
|
101
|
+
+----------------------+
|
102
|
+
| Tables_in_test |
|
103
|
+
+----------------------+
|
104
|
+
| builders |
|
105
|
+
| wares |
|
106
|
+
+----------------------+
|
107
|
+
4 rows in set (0.001 sec)
|
108
|
+
```
|
109
|
+
- If it doesn't exists, create one with a couple of tables:
|
110
|
+
```bash
|
111
|
+
MariaDB [(none)]> CREATE DATABASE test;
|
112
|
+
MariaDB [(none)]> USE test;
|
113
|
+
MariaDB [test]> CREATE TABLE IF NOT EXISTS wares(id int auto_increment, primary key(id));
|
114
|
+
MariaDB [test]> CREATE TABLE IF NOT EXISTS builders(id int auto_increment, primary key(id));
|
115
|
+
```
|
116
|
+
- Run the tests separately:
|
117
|
+
```bash
|
118
|
+
bundle exec rake TEST=test/ar/postgresql_sequence_test.rb
|
119
|
+
bundle exec rake TEST=test/ar/mysql_sequence_test.rb
|
120
|
+
```
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Nikolai Bocharov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# sequel-sequence
|
2
|
+
|
3
|
+
[![Tests](https://github.com/oreol-group/sequel-sequence/workflows/Tests/badge.svg)](https://github.com/oreol-group/sequel-sequence)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/sequel-sequence.svg)](https://rubygems.org/gems/sequel-sequence)
|
5
|
+
[![Downloads total](https://img.shields.io/gem/dt/sequel-sequence.svg)](https://rubygems.org/gems/sequel-sequence)
|
6
|
+
|
7
|
+
Adds a useful interface and support for PostgreSQL and MySQL `SEQUENCE` on Sequel migrations
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
```bash
|
12
|
+
gem install sequel-sequence
|
13
|
+
```
|
14
|
+
|
15
|
+
Or add the following line to your project's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem "sequel-sequence"
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To create a `SEQUENCE`, just use the method `create_sequence`.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Sequel.migration do
|
27
|
+
up do
|
28
|
+
create_sequence :position
|
29
|
+
end
|
30
|
+
|
31
|
+
down do
|
32
|
+
drop_sequence :position
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
You can also specify the initial value and increment:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
create_sequence :position, increment: 2
|
41
|
+
create_sequence :position, start: 100
|
42
|
+
```
|
43
|
+
|
44
|
+
To define a column that has a sequence as its default value, use something like
|
45
|
+
the following:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
Sequel.migration do
|
49
|
+
change do
|
50
|
+
create_sequence :position_id
|
51
|
+
|
52
|
+
create_table(:things) do
|
53
|
+
primary_key :id
|
54
|
+
String :name, text: true
|
55
|
+
|
56
|
+
# PostgreSQL uses bigint as the sequence's default type.
|
57
|
+
Bignum :position, null: false
|
58
|
+
|
59
|
+
Time :created_at, null: false
|
60
|
+
Time :updated_at, null: false
|
61
|
+
end
|
62
|
+
|
63
|
+
set_column_default_nextval :things, :position, :position_id
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
This gem also adds a few helpers to interact with `SEQUENCE`s.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
DB = Sequel.connect('...')
|
72
|
+
# Advance sequence and return new value
|
73
|
+
DB.nextval("position")
|
74
|
+
|
75
|
+
# Return value most recently obtained with nextval for specified sequence, either
|
76
|
+
DB.currval("position")
|
77
|
+
# or
|
78
|
+
DB.lastval("position")
|
79
|
+
# Both options are acceptable in PostgreSQL and MySQL.
|
80
|
+
|
81
|
+
# Set sequence's current value. It must be greater than lastval or currval.
|
82
|
+
DB.setval("position", 1234)
|
83
|
+
```
|
84
|
+
|
85
|
+
## Maintainer
|
86
|
+
|
87
|
+
- [Nikolai Bocharov](https://github.com/oreol-group)
|
88
|
+
|
89
|
+
## Contributors
|
90
|
+
|
91
|
+
- https://github.com/oreol-group/sequel-sequence/contributors
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
For more details about how to contribute, please read
|
96
|
+
https://github.com/oreol-group/sequel-sequence/blob/main/CONTRIBUTING.md.
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the
|
101
|
+
[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
|
102
|
+
found at https://github.com/oreol-group/sequel-sequence/blob/main/LICENSE.md.
|
103
|
+
|
104
|
+
## Code of Conduct
|
105
|
+
|
106
|
+
Everyone interacting in the sequel-sequence project's codebases, issue trackers,
|
107
|
+
chat rooms and mailing lists is expected to follow the
|
108
|
+
[code of conduct](https://github.com/oreol-group/sequel-sequence/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << 'test'
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.test_files = FileList['test/**/*_test.rb']
|
11
|
+
end
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
data/lib/sequel/error.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# https://sequel.jeremyevans.net/rdoc/files/doc/sql_rdoc.html
|
4
|
+
# https://github.com/jeremyevans/sequel/blob/master/lib/sequel/database/connecting.rb
|
5
|
+
module Sequel
|
6
|
+
module Sequence
|
7
|
+
module Database
|
8
|
+
module Mysql2
|
9
|
+
def quote_column_name(name)
|
10
|
+
"`#{name.gsub('`', '``')}`"
|
11
|
+
end
|
12
|
+
|
13
|
+
def quote_sequence_name(name)
|
14
|
+
"`#{name.gsub(/[`"']/, '')}`"
|
15
|
+
end
|
16
|
+
|
17
|
+
def check_sequences
|
18
|
+
fetch("SHOW FULL TABLES WHERE Table_type = 'SEQUENCE';").all.to_a
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_sequence(name, options = {})
|
22
|
+
increment = options[:increment] || options[:step]
|
23
|
+
name = quote_name(name.to_s)
|
24
|
+
|
25
|
+
sql = ["CREATE SEQUENCE 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 #{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
|
+
# for db.database_type = :mysql2
|
49
|
+
def lastval(name)
|
50
|
+
name = quote(name.to_s)
|
51
|
+
out = nil
|
52
|
+
fetch("SELECT lastval(#{name});") do |row|
|
53
|
+
out = row["lastval(#{name})".to_sym]
|
54
|
+
end
|
55
|
+
out
|
56
|
+
end
|
57
|
+
|
58
|
+
# for db.database_type = :postgres
|
59
|
+
alias currval lastval
|
60
|
+
|
61
|
+
def setval(name, value)
|
62
|
+
name = quote(name.to_s)
|
63
|
+
out = nil
|
64
|
+
fetch("SELECT setval(#{name}, #{value});") do |row|
|
65
|
+
out = row["setval(#{name}, #{value})".to_sym]
|
66
|
+
end
|
67
|
+
out
|
68
|
+
end
|
69
|
+
|
70
|
+
def set_column_default_nextval(table, column, sequence)
|
71
|
+
table = table.to_s
|
72
|
+
column = column.to_s
|
73
|
+
sequence = quote(sequence.to_s)
|
74
|
+
run "ALTER TABLE IF EXISTS #{table} " \
|
75
|
+
"ALTER COLUMN #{column} SET DEFAULT nextval(#{sequence});"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# https://sequel.jeremyevans.net/rdoc/files/doc/sql_rdoc.html
|
4
|
+
# https://github.com/jeremyevans/sequel/blob/master/lib/sequel/database/connecting.rb
|
5
|
+
module Sequel
|
6
|
+
module Sequence
|
7
|
+
module Database
|
8
|
+
module PostgreSQL
|
9
|
+
SEQUENCE_COMMENT = 'created by sequel-sequence'
|
10
|
+
|
11
|
+
def quote_column_name(name)
|
12
|
+
PG::Connection.quote_ident(name).freeze
|
13
|
+
end
|
14
|
+
|
15
|
+
def quote_sequence_name(name)
|
16
|
+
PG::Connection.quote_connstr(name).freeze
|
17
|
+
end
|
18
|
+
|
19
|
+
def custom_sequence?(sequence_name)
|
20
|
+
out = ''
|
21
|
+
begin
|
22
|
+
fetch("SELECT obj_description('#{sequence_name}'::regclass, 'pg_class');") do |row|
|
23
|
+
out = row[:obj_description]
|
24
|
+
end
|
25
|
+
rescue Sequel::DatabaseError # PG::UndefinedTable
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
out == SEQUENCE_COMMENT
|
30
|
+
end
|
31
|
+
|
32
|
+
def check_sequences
|
33
|
+
fetch('SELECT * FROM information_schema.sequences ORDER BY sequence_name').all.to_a
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_sequence(name, options = {})
|
37
|
+
increment = options[:increment] || options[:step]
|
38
|
+
name = quote_name(name.to_s)
|
39
|
+
|
40
|
+
sql = ["CREATE SEQUENCE IF NOT EXISTS #{name}"]
|
41
|
+
sql << "INCREMENT BY #{increment}" if increment
|
42
|
+
sql << "START WITH #{options[:start]}" if options[:start]
|
43
|
+
sql << ';'
|
44
|
+
sql << "COMMENT ON SEQUENCE #{name} IS '#{SEQUENCE_COMMENT}';"
|
45
|
+
|
46
|
+
run(sql.join("\n"))
|
47
|
+
end
|
48
|
+
|
49
|
+
def drop_sequence(name)
|
50
|
+
name = quote_name(name.to_s)
|
51
|
+
sql = "DROP SEQUENCE #{name}"
|
52
|
+
run(sql)
|
53
|
+
end
|
54
|
+
|
55
|
+
def nextval(name)
|
56
|
+
name = quote(name.to_s)
|
57
|
+
out = nil
|
58
|
+
fetch("SELECT nextval(#{name})") do |row|
|
59
|
+
out = row[:nextval]
|
60
|
+
end
|
61
|
+
out
|
62
|
+
end
|
63
|
+
|
64
|
+
# for db.database_type = :postgres
|
65
|
+
def currval(name)
|
66
|
+
name = quote(name.to_s)
|
67
|
+
out = nil
|
68
|
+
fetch("SELECT currval(#{name})") do |row|
|
69
|
+
out = row[:currval]
|
70
|
+
end
|
71
|
+
out
|
72
|
+
end
|
73
|
+
|
74
|
+
# for db.database_type = :mysql2
|
75
|
+
alias lastval currval
|
76
|
+
|
77
|
+
def setval(name, value)
|
78
|
+
name = quote(name.to_s)
|
79
|
+
out = nil
|
80
|
+
fetch("SELECT setval(#{name}, #{value})") do |row|
|
81
|
+
out = row[:setval]
|
82
|
+
end
|
83
|
+
out
|
84
|
+
end
|
85
|
+
|
86
|
+
def set_column_default_nextval(table, column, sequence)
|
87
|
+
table = table.to_s
|
88
|
+
column = column.to_s
|
89
|
+
sequence = quote(sequence.to_s)
|
90
|
+
run "ALTER TABLE IF EXISTS #{table} " \
|
91
|
+
"ALTER COLUMN #{column} SET DEFAULT nextval(#{sequence}::regclass)"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Sequence
|
5
|
+
module Database
|
6
|
+
# def custom_sequence?(_sequence_name)
|
7
|
+
# false
|
8
|
+
# end
|
9
|
+
|
10
|
+
def check_sequences
|
11
|
+
[]
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_sequence(_name, _options = {})
|
15
|
+
raise Sequel::MethodNotAllowed, Sequel::MethodNotAllowed::METHOD_NOT_ALLOWED
|
16
|
+
end
|
17
|
+
|
18
|
+
def drop_sequence(_name)
|
19
|
+
raise Sequel::MethodNotAllowed, Sequel::MethodNotAllowed::METHOD_NOT_ALLOWED
|
20
|
+
end
|
21
|
+
|
22
|
+
def quote_name(name)
|
23
|
+
name.to_s.split('.', 2).map { |part| quote_column_name(part) }.join('.')
|
24
|
+
end
|
25
|
+
|
26
|
+
def quote(name)
|
27
|
+
name.to_s.split('.', 2).map { |part| quote_sequence_name(part) }.join('.')
|
28
|
+
end
|
29
|
+
|
30
|
+
def nextval(_name)
|
31
|
+
raise Sequel::MethodNotAllowed, Sequel::MethodNotAllowed::METHOD_NOT_ALLOWED
|
32
|
+
end
|
33
|
+
|
34
|
+
# for connection.adapter_name = "PostgreSQL"
|
35
|
+
def currval(_name)
|
36
|
+
raise Sequel::MethodNotAllowed, Sequel::MethodNotAllowed::METHOD_NOT_ALLOWED
|
37
|
+
end
|
38
|
+
|
39
|
+
# for connection.adapter_name = "Mysql2"
|
40
|
+
alias lastval currval
|
41
|
+
|
42
|
+
def setval(_name, _value)
|
43
|
+
raise AR::MethodNotAllowed, AR::MethodNotAllowed::METHOD_NOT_ALLOWED
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|