Giter Site home page Giter Site logo

pg_sequencer's Introduction

pg_sequencer

pg_sequencer adds methods to your migrations to allow you to create, drop and change sequence objects in PostgreSQL. It also dumps sequences to schema.rb.

This is especially useful if you are connecting to a legacy database where the primary key field is declared as an INTEGER and a sequence is queried for the value of the next record.

The design of pg_sequencer is heavily influenced on Matthew Higgins’ Foreigner gem:

Installation

Add this to your Gemfile:

gem 'pg_sequencer'

API

pg_sequencer adds the following methods to migrations:

  • create_sequence(sequence_name, options)

  • change_sequence(sequence_name, options)

  • drop_sequence(sequence_name)

The methods closely mimic the syntax of the PostgreSQL SQL for CREATE SEQUENCE, DROP SEQUENCE and ALTER SEQUENCE. See the REFERENCES section below for more information.

Options

For create_sequence and change_sequence, all options are the same, except create_sequence will look for :start or :start_with, and change_sequence will look for :restart or :restart_with.

  • :increment/:increment_by (integer) - The value to increment the sequence by.

  • :min (integer/false) - The minimum value of the sequence. If specified as false (e.g. :min => false), “NO MINVALUE” is sent to Postgres.

  • :max (integer/false) - The maximum value of the sequence. May be specified as “:max => false” to generate “NO MAXVALUE”

  • :start/:start_with (integer) - The starting value of the sequence (create_sequence only)

  • :restart/:restart_with (integer) The value to restart the sequence with (change_sequence only)

  • :cache (integer) - The number of values the sequence should cache.

  • :cycle (boolean) - Whether the sequence should cycle. Generated at “CYCLE” or “NO CYCLE”

Examples

Creating a sequence

Create a sequence called “seq_user”, incrementing by 1, min of 1, max of 2000000, starts at 1, caches 10 values, and disallows cycles:

create_sequence("seq_user", {
  :increment => 1,
  :min => 1,
  :max => 2000000,
  :start => 1,
  :cache => 10,
  :cycle => false
})

This is equivalent of the following query:

CREATE SEQUENCE seq_user INCREMENT BY 1 MIN 1 MAX 2000000 START 1 CACHE 10 NO CYCLE

Reset a sequence’s value:

change_sequence "seq_accounts", :restart_with => 50

This is equivalent to:

ALTER SEQUENCE seq_accounts RESTART WITH 50

Removing a sequence:

drop_sequence "seq_products"

Caveats / Bugs

  • Tested with postgres 9.0.4, should work down to 8.1.

  • Listing all the sequences in a database creates n+1 queries (1 to get the names and n to describe each sequence). Is there a way to fully describe all sequences in a database in one query?

  • The “SET SCHEMA” fragment of the ALTER command is not implemented.

  • Oracle/other databases not supported

  • Other unknown bugs :)

References

License

Copyright © 2011 Code 42 Software.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

pg_sequencer's People

Contributors

tonyc avatar

Watchers

Peter Kadlot avatar Stuart Harris avatar Sam Taylor avatar James Cloos avatar James Muir-Hopkins avatar Jack Hoy avatar Paul Skarseth avatar Tiago Azevedo avatar Dom avatar Joe Stanton avatar Lily avatar Cristian Wilgenhoff avatar  avatar Samera Butt avatar Graham Mendick avatar  avatar Cain Ullah avatar Joe Dollar-Smirnov avatar Synapticloop avatar Eric Juta avatar  avatar Tom avatar David Basalla avatar Nico Castro avatar Austin  avatar Sinem Erdemli avatar Ranj Kanag avatar Zoe Merchant avatar Sarah Knight avatar Peter Diamant avatar Michel Erler avatar Lani Smash avatar Olga Loyev avatar  avatar  avatar  avatar Estelle Moley avatar  avatar Sam Griffiths avatar Federico Capaldo avatar Chris Brookes avatar Phil Brooks avatar Sam L avatar Joseph Popoola avatar  avatar Nathalie Goepel avatar  avatar Rane Gowan avatar  avatar Harriet Adams avatar  avatar Kyle Patel avatar  avatar Monika avatar  avatar Tracy Wu  avatar Hanna C avatar  avatar Simon Ashbery avatar  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.