Postgres Sequence Support

Easy manipulation of PostgreSQL sequences from ActiveRecord.

Examples

User.next_id              # => 1
User.current_id           # => 1

c = ActiveRecord::Base.connection
c.set_sequence('users_id_seq', 1000)
c.next_in_sequence('users_id_seq')      # => 1001

# Note : create sequence don't do anything if sequence already exists
c.create_sequence('my_own_seq')
c.next_in_sequence('my_own_seq')        # => 1

# You can specify sequence start
c.create_sequence('my_own_seq', 1001)
c.next_in_sequence('my_own_seq')        # => 1001

# Check if sequence exists
c.sequence_exists?('my_own_seq')        # => true
c.sequence_exists?('my_other_seq')      # => false

# Drop sequence if it exists
c.drop_sequence('my_own_seq')

Install

As a Rails plugin.

./script/plugin install git://github.com/jqr/postgres_sequence_support.git

Prefer gems? Add this to your environment.rb and run the following command.

config.gem 'jqr-postgres_sequence_support', :lib => 'postgres_sequence_support', :source => 'http://gems.github.com'

$ rake gems:install
Homepage

github.com/jqr/postgres_sequence_support

License

Copyright © 2008 Elijah Miller <[email protected]>, released under the MIT license.