Giter VIP home page Giter VIP logo

ar-sequence's Introduction

ar-sequence

Tests Gem Gem

Add support for PostgreSQL's SEQUENCE on ActiveRecord migrations.

Installation

gem install ar-sequence

Or add the following line to your project's Gemfile:

gem "ar-sequence"

Usage

To create a SEQUENCE, just use the method create_sequence.

class CreateUsers < ActiveRecord::Migration[5.2]
 def up
   create_sequence :position
 end

 def down
   drop_sequence :position
 end
end

You can also specify the initial value and increment:

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:

class CreateThings < ActiveRecord::Migration[5.2]
  def change
    create_sequence :position

    create_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 SEQUENCEs.

# 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

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. 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.

ar-sequence's People

Contributors

fnando avatar dim avatar vdbilt avatar manuraj17 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.