Class: DynamicMigrations::Postgres::Server::Database::Schema::Table::ForeignKeyConstraint

Inherits:
DynamicMigrations::Postgres::Server::Database::Source show all
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb

Overview

This class represents a postgres foreign key constraint

Defined Under Namespace

Classes: DuplicateColumnError, ExpectedArrayOfColumnsError, ExpectedTableError, UnexpectedReferentialActionError

Instance Attribute Summary collapse

Attributes inherited from DynamicMigrations::Postgres::Server::Database::Source

#source

Instance Method Summary collapse

Methods inherited from DynamicMigrations::Postgres::Server::Database::Source

#assert_is_a_symbol!, #from_configuration?, #from_database?

Constructor Details

#initialize(source, table, columns, foreign_table, foreign_columns, name, description: nil, deferrable: false, initially_deferred: false, on_delete: :no_action, on_update: :no_action) ⇒ ForeignKeyConstraint

initialize a new object to represent a foreign_key_constraint in a postgres table

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 33

def initialize source, table, columns, foreign_table, foreign_columns, name, description: nil, deferrable: false, initially_deferred: false, on_delete: :no_action, on_update: :no_action
  super source

  raise ExpectedTableError, table unless table.is_a? Table
  raise ExpectedTableError, foreign_table unless foreign_table.is_a? Table

  # assert that the provided columns is an array
  unless columns.is_a?(Array) && columns.count > 0
    raise ExpectedArrayOfColumnsError
  end

  # assert that the provided foreign columns is an array
  unless foreign_columns.is_a?(Array) && foreign_columns.count > 0
    raise ExpectedArrayOfColumnsError
  end

  # tables must be set before the columns are added
  @table = table
  @foreign_table = foreign_table

  # add this foreign_key_constraint to the remote table (so we can always find
  # these from both sides of the association)
  @foreign_table.add_remote_foreign_key_constraint self

  @columns = {}
  columns.each do |column|
    add_column column
  end

  @foreign_columns = {}
  foreign_columns.each do |column|
    add_column column, true
  end

  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @name = name

  unless description.nil?
    raise ExpectedStringError, description unless description.is_a? String
    @description = description.strip
    @description = nil if description == ""
  end

  raise ExpectedBooleanError, deferrable unless [true, false].include?(deferrable)
  @deferrable = deferrable

  raise ExpectedBooleanError, initially_deferred unless [true, false].include?(initially_deferred)
  @initially_deferred = initially_deferred

  raise UnexpectedReferentialActionError, on_delete unless [:no_action, :restrict, :cascade, :set_null, :set_default].include?(on_delete)
  @on_delete = on_delete

  raise UnexpectedReferentialActionError, on_update unless [:no_action, :restrict, :cascade, :set_null, :set_default].include?(on_update)
  @on_update = on_update
end

Instance Attribute Details

#deferrableObject (readonly)

Returns the value of attribute deferrable.



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

def deferrable
  @deferrable
end

#descriptionObject (readonly)

Returns the value of attribute description.



30
31
32
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 30

def description
  @description
end

#foreign_tableObject (readonly)

Returns the value of attribute foreign_table.



24
25
26
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 24

def foreign_table
  @foreign_table
end

#initially_deferredObject (readonly)

Returns the value of attribute initially_deferred.



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

def initially_deferred
  @initially_deferred
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 25

def name
  @name
end

#on_deleteObject (readonly)

Returns the value of attribute on_delete.



28
29
30
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 28

def on_delete
  @on_delete
end

#on_updateObject (readonly)

Returns the value of attribute on_update.



29
30
31
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 29

def on_update
  @on_update
end

#tableObject (readonly)

Returns the value of attribute table.



23
24
25
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 23

def table
  @table
end

Instance Method Details

#column_namesObject



98
99
100
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 98

def column_names
  @columns.keys
end

#columnsObject



94
95
96
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 94

def columns
  @columns.values
end

#differences_descriptions(other_foreign_key_constraint) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 118

def differences_descriptions other_foreign_key_constraint
  method_differences_descriptions other_foreign_key_constraint, [
    :column_names,
    :foreign_schema_name,
    :foreign_table_name,
    :foreign_column_names,
    :deferrable,
    :initially_deferred,
    :on_delete,
    :on_update
  ]
end

#foreign_column_namesObject



114
115
116
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 114

def foreign_column_names
  @foreign_columns.keys
end

#foreign_columnsObject



102
103
104
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 102

def foreign_columns
  @foreign_columns.values
end

#foreign_schema_nameObject



106
107
108
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 106

def foreign_schema_name
  @foreign_table.schema.name
end

#foreign_table_nameObject



110
111
112
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 110

def foreign_table_name
  @foreign_table.name
end

#has_description?Boolean

return true if this has a description, otherwise false

Returns:

  • (Boolean)


90
91
92
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb', line 90

def has_description?
  !@description.nil?
end