Module: PGSpecHelper::PrimaryKeys
- Included in:
- PGSpecHelper
- Defined in:
- lib/pg_spec_helper/primary_keys.rb
Instance Method Summary collapse
-
#create_primary_key(schema_name, table_name, column_names, primary_key_name) ⇒ Object
add a primary_key to the provided table which covers the provided columns.
-
#get_primary_key_name(schema_name, table_name) ⇒ Object
get the primary_key name for the provided table.
Instance Method Details
#create_primary_key(schema_name, table_name, column_names, primary_key_name) ⇒ Object
add a primary_key to the provided table which covers the provided columns
6 7 8 9 10 11 12 13 14 |
# File 'lib/pg_spec_helper/primary_keys.rb', line 6 def create_primary_key schema_name, table_name, column_names, primary_key_name column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ") # add the primary_key connection.exec(<<~SQL) ALTER TABLE #{schema_name}.#{table_name} ADD CONSTRAINT #{primary_key_name} PRIMARY KEY (#{column_names_sql}) SQL end |
#get_primary_key_name(schema_name, table_name) ⇒ Object
get the primary_key name for the provided table
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pg_spec_helper/primary_keys.rb', line 17 def get_primary_key_name schema_name, table_name # get the primary_key name rows = connection.exec(<<~SQL, [schema_name.to_s, table_name.to_s]) SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema = $1 AND table_name = $2 AND constraint_type = 'PRIMARY KEY' SQL rows.map { |r| r["constraint_name"].to_sym }.first end |