Sha256: 1b075f5c5ece42dfe53a71f75347d1a2997b23f52b7943b5246a630b1594d3c9

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

require 'skr/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.config.table_prefix}sequential_ids add primary key (name)"
        execute <<-EOS
create or replace function #{Skr.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.config.table_prefix}next_sequential_id(varchar)"
    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stockor-0.5.0 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.4.0 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.3.0 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.2 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.1.9 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.1.8 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.1.7 db/migrate/20120110142845_create_skr_sequential_ids.rb
stockor-0.1.5 db/migrate/20120110142845_create_skr_sequential_ids.rb