Module: PgRandomId::Migrations
- Defined in:
- lib/pg_random_id/migrations.rb
Class Method Summary collapse
-
.install_active_record ⇒ Object
Install the migration functions for ActiveRecord.
-
.install_sequel ⇒ Object
Install the migration functions for Sequel.
Instance Method Summary collapse
-
#create_random_id_functions ⇒ Object
Create in the database the functions necessary for this gem to work.
-
#drop_random_id_functions ⇒ Object
Drop the functions installed by #create_random_id_functions.
-
#random_id(table, column = :id, key = nil) ⇒ Object
Apply a random id to a table.
-
#random_str_id(table, column = :id, key = nil) ⇒ Object
Apply a random string id to a table.
-
#remove_random_id(table, column = :id) ⇒ Object
Changes type of a column to int and restores sequence default on it.
Class Method Details
.install_active_record ⇒ Object
Install the migration functions for ActiveRecord
45 46 47 48 |
# File 'lib/pg_random_id/migrations.rb', line 45 def self.install_active_record require 'active_record/migration' ActiveRecord::Migration.send :include, self end |
.install_sequel ⇒ Object
Install the migration functions for Sequel
51 52 53 |
# File 'lib/pg_random_id/migrations.rb', line 51 def self.install_sequel Sequel::Database.send :include, self end |
Instance Method Details
#create_random_id_functions ⇒ Object
Create in the database the functions necessary for this gem to work.
7 8 9 |
# File 'lib/pg_random_id/migrations.rb', line 7 def create_random_id_functions execute PgRandomId::Sql::install end |
#drop_random_id_functions ⇒ Object
Drop the functions installed by #create_random_id_functions
12 13 14 |
# File 'lib/pg_random_id/migrations.rb', line 12 def drop_random_id_functions execute PgRandomId::Sql::uninstall end |
#random_id(table, column = :id, key = nil) ⇒ Object
Apply a random id to a table. If you don’t give a key, a random one will be generated. The ids will be based on sequence “#table_#column_seq”. You need to make sure the table is empty; migrating existing records is not implemented.
20 21 22 |
# File 'lib/pg_random_id/migrations.rb', line 20 def random_id table, column = :id, key = nil execute PgRandomId::Sql::apply(table, column, key: key) end |
#random_str_id(table, column = :id, key = nil) ⇒ Object
Apply a random string id to a table. Also changes the type of the id column to char(6). If you don’t give a key, a random one will be generated. The ids will be based on sequence “#table_#column_seq”, scrambled and base32-encoded. You need to make sure the table is empty; migrating existing records is not implemented.
40 41 42 |
# File 'lib/pg_random_id/migrations.rb', line 40 def random_str_id table, column = :id, key = nil execute PgRandomId::Sql::apply_str(table, column, key: key) end |
#remove_random_id(table, column = :id) ⇒ Object
Changes type of a column to int and restores sequence default on it.
This is mainly useful for a down migration while developing the database and switching back and forth. Not needed in real use, as dropping a table will obliterate the default value anyway.
You need to make sure the table is empty; migrating existing records is not implemented.
30 31 32 |
# File 'lib/pg_random_id/migrations.rb', line 30 def remove_random_id table, column = :id execute PgRandomId::Sql::unapply(table, column) end |