Module: DynamicMigrations::Postgres::Server::Database::Schema::Table::ForeignKeyConstraints

Included in:
DynamicMigrations::Postgres::Server::Database::Schema::Table
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb

Overview

This module has all the tables methods for working with foreign keys

Defined Under Namespace

Classes: ForeignKeyConstraintAlreadyExistsError, ForeignKeyConstraintDoesNotExistError

Instance Method Summary collapse

Instance Method Details

#add_foreign_key_constraint(name, column_names, foreign_schema_name, foreign_table_name, foreign_column_names, **foreign_key_constraint_options) ⇒ Object

adds a new foreign_key_constraint to this table, and returns it



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb', line 41

def add_foreign_key_constraint name, column_names, foreign_schema_name, foreign_table_name, foreign_column_names, **foreign_key_constraint_options
  if has_foreign_key_constraint? name
    raise(ForeignKeyConstraintAlreadyExistsError, "foreign_key_constraint #{name} already exists")
  end
  columns = column_names.map { |column_name| column column_name }
  foreign_schema = schema.database.schema foreign_schema_name, source
  foreign_table = foreign_schema.table foreign_table_name
  foreign_columns = foreign_column_names.map { |column_name| foreign_table.column column_name }
  included_target = self
  if included_target.is_a? Table
    new_foreign_key_constraint = @foreign_key_constraints[name] = ForeignKeyConstraint.new source, included_target, columns, foreign_table, foreign_columns, name, **foreign_key_constraint_options
  else
    raise ModuleIncludedIntoUnexpectedTargetError, included_target
  end
  # sort the hash so that the foreign_key_constraints are in alphabetical order by name
  sorted_foreign_key_constraints = {}
  @foreign_key_constraints.keys.sort.each do |name|
    sorted_foreign_key_constraints[name] = @foreign_key_constraints[name]
  end
  @foreign_key_constraints = sorted_foreign_key_constraints
  # return the new foreign_key_constraint
  new_foreign_key_constraint
end

#add_remote_foreign_key_constraint(foreign_key_constraint) ⇒ Object

called automatically from the other side of the foreign key constraint to keep track of the foreign key from both sides



66
67
68
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb', line 66

def add_remote_foreign_key_constraint foreign_key_constraint
  @remote_foreign_key_constraints << foreign_key_constraint
end

#foreign_key_constraint(name) ⇒ Object

returns the foreign_key_constraint object for the provided foreign_key_constraint name, and raises an error if the foreign_key_constraint does not exist



19
20
21
22
23
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb', line 19

def foreign_key_constraint name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  raise ForeignKeyConstraintDoesNotExistError unless has_foreign_key_constraint? name
  @foreign_key_constraints[name]
end

#foreign_key_constraintsObject

returns an array of this tables foreign_key_constraints



32
33
34
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb', line 32

def foreign_key_constraints
  @foreign_key_constraints.values
end

#foreign_key_constraints_hashObject



36
37
38
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb', line 36

def foreign_key_constraints_hash
  @foreign_key_constraints
end

#has_foreign_key_constraint?(name) ⇒ Boolean

returns true if this table has a foreign_key_constraint with the provided name, otherwise false

Returns:

  • (Boolean)

Raises:



26
27
28
29
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb', line 26

def has_foreign_key_constraint? name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @foreign_key_constraints.key? name
end