Sha256: 704142d47d6b81bccce1c3269aa7c2de0efc24e8bc6bb7369cbb632878a64af9

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'skr/core/db/migration_helpers'

class CreateSkrSequentialIds < ActiveRecord::Migration
    def up
        # rails can suck it here, there's no reason to have a id(int)
        create_skr_table :sequential_ids, :id=>false do |t|
            t.string :name, :null=>false
            t.integer :current_value, :null=>false, :default=>0
        end

        execute "alter table #{Skr::Core.config.table_prefix}sequential_ids add primary key (name)"
        execute <<-EOS
create or replace function #{Skr::Core.config.table_prefix}next_sequential_id( varchar )
returns integer AS '
declare
    next_id integer;
begin
    select current_value into next_id from skr_sequential_ids where name = $1 for update;
    if not found then
        insert into skr_sequential_ids ( name, current_value ) values ( $1, 1 );
        return 1;
    else
        update skr_sequential_ids set current_value = next_id+1 where name = $1;
        return next_id+1;
    end if;
end;
' language plpgsql;
EOS
    end

    def down
        drop_skr_table :sequential_ids
        execute "drop function #{Skr::Core.config.table_prefix}next_sequential_id(varchar)"
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stockor-core-0.2 db/migrate/20120110142845_create_skr_sequential_ids.rb