Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fnando/ar-sequence
Add support for PostgreSQL's SEQUENCE on ActiveRecord migrations.
https://github.com/fnando/ar-sequence
activerecord postgresql rails sequence
Last synced: 7 days ago
JSON representation
Add support for PostgreSQL's SEQUENCE on ActiveRecord migrations.
- Host: GitHub
- URL: https://github.com/fnando/ar-sequence
- Owner: fnando
- License: mit
- Created: 2018-10-02T16:54:23.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-03-24T01:08:04.000Z (10 months ago)
- Last Synced: 2024-12-25T11:42:08.677Z (18 days ago)
- Topics: activerecord, postgresql, rails, sequence
- Language: Ruby
- Homepage:
- Size: 45.9 KB
- Stars: 16
- Watchers: 4
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ar-sequence
[![Tests](https://github.com/fnando/ar-sequence/workflows/Tests/badge.svg)](https://github.com/fnando/ar-sequence)
[![Gem](https://img.shields.io/gem/v/ar-sequence.svg)](https://rubygems.org/gems/ar-sequence)
[![Gem](https://img.shields.io/gem/dt/ar-sequence.svg)](https://rubygems.org/gems/ar-sequence)Add support for PostgreSQL's `SEQUENCE` on ActiveRecord migrations.
## Installation
```bash
gem install ar-sequence
```Or add the following line to your project's Gemfile:
```ruby
gem "ar-sequence"
```## Usage
To create a `SEQUENCE`, just use the method `create_sequence`.
```ruby
class CreateUsers < ActiveRecord::Migration[5.2]
def up
create_sequence :position
enddef down
drop_sequence :position
end
end
```You can also specify the initial value and increment:
```ruby
create_sequence :position, increment: 2
create_sequence :position, start: 100
```To define a column that has a sequence as its default value, use something like
the following:```ruby
class CreateThings < ActiveRecord::Migration[5.2]
def change
create_sequence :positioncreate_table :things do |t|
t.text :name# PostgreSQL uses bigint as the sequence's default type.
# Use a block to specify the default value on migrations.
t.bigint :position,
null: false,
default: -> { "nextval('position')" }t.timestamps
end
end
end
```This gem also adds a few helpers to interact with `SEQUENCE`s.
```ruby
# Advance sequence and return new value
ActiveRecord::Base.nextval("position")# Return value most recently obtained with nextval for specified sequence.
ActiveRecord::Base.currval("position")# Set sequence's current value
ActiveRecord::Base.setval("position", 1234)
```## Maintainer
- [Nando Vieira](https://github.com/fnando)
## Contributors
- https://github.com/fnando/ar-sequence/contributors
## Contributing
For more details about how to contribute, please read
https://github.com/fnando/ar-sequence/blob/main/CONTRIBUTING.md.## License
The gem is available as open source under the terms of the
[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
found at https://github.com/fnando/ar-sequence/blob/main/LICENSE.md.## Code of Conduct
Everyone interacting in the ar-sequence project's codebases, issue trackers,
chat rooms and mailing lists is expected to follow the
[code of conduct](https://github.com/fnando/ar-sequence/blob/main/CODE_OF_CONDUCT.md).