Sqlserver::Sequence
A Rails plugin for SQL Server sequences.
Installation
Add this line to your application's Gemfile:
gem 'sqlserver-sequence'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sqlserver-sequence
Usage
Specify the attribute you'd like to sequence
class Supplier < ActiveRecord::Base
sequence :number
end
Now when you create a record number
will be assigned the next value for that sequence.
$ supplier = Supplier.create
$ supplier.number
> 10 # generated by sequence named 'number'
$ other_supplier = Supplier.create
$ other_supplier.number
> 11
You can also define multiple sequences
class Supplier < ActiveRecord::Base
sequence :number
sequence :purchase_order
end
Options
name
By default sqlserver-sequence
will look for a row in sys.sequences
with the same sequence_name
as the attribute. You can use the name
option if the sequence is named differently.
class Supplier < ActiveRecord::Base
sequence :number, name: 'next_supplier_number'
end
$ supplier = Supplier.create
$ supplier.number
> 10 # generated by sequence named 'next_supplier_number'
prefix
You can use the prefix
option to prepend every sequence value with a string.
class Supplier < ActiveRecord::Base
sequence :number, prefix: 'S-'
end
$ supplier = Supplier.create
$ supplier.number
> S-10
format
Pass a lambda to the format
option if you need more control over the assigned sequence value, for example to assign a fixed length string.
class Supplier < ActiveRecord::Base
sequence :number, prefix: 'S-',
format: lambda { |num| num.rjust(10, 0) }
end
$ supplier = Supplier.create
$ supplier.number
> S-0000000010
Testing with SQLite
If your test suite uses SQLite you'll need to turn off sequence generation or provide a stub for next_sequence_value
.
before do
allow_any_instance_of(Supplier).to receive(:next_sequence_value).and_return('10')
end
Contributing
- Fork it ( https://github.com/zacharywelch/sqlserver-sequence/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
See the Running Tests guide for details on how to run the test suite on SQL Server.