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

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

Overview

This class represents a postgres table validation

Defined Under Namespace

Classes: DuplicateColumnError, ExpectedArrayOfColumnsError, ExpectedTableColumnsError, ExpectedTableError, UnexpectedTemplateError, UnnormalizableCheckClauseError

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, name, check_clause, description: nil, deferrable: false, initially_deferred: false, template: nil) ⇒ Validation

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

Raises:



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
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 38

def initialize source, table, columns, name, check_clause, description: nil, deferrable: false, initially_deferred: false, template: nil
  super source
  raise ExpectedTableError, table unless table.is_a? Table
  @table = table

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

  raise ExpectedStringError, check_clause unless check_clause.is_a? String
  @check_clause = check_clause.strip

  # if this validation is created via configuration (apposed to being loaded) then they can be lazy loaded
  unless from_configuration? && columns.nil?
    # assert that the provided columns is an array
    unless columns.is_a?(Array) && columns.count > 0
      raise ExpectedArrayOfColumnsError
    end

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

  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

  unless template.nil?
    unless Generator::Validation.has_template? template
      raise UnexpectedTemplateError, "Unrecognised template #{template}"
    end
    @template = template
  end
end

Instance Attribute Details

#check_clauseObject (readonly)

Returns the value of attribute check_clause.



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

def check_clause
  @check_clause
end

#deferrableObject (readonly)

Returns the value of attribute deferrable.



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

def deferrable
  @deferrable
end

#descriptionObject (readonly)

Returns the value of attribute description.



34
35
36
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 34

def description
  @description
end

#initially_deferredObject (readonly)

Returns the value of attribute initially_deferred.



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

def initially_deferred
  @initially_deferred
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#column_namesObject



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

def column_names
  columns.map(&:name)
end

#columnsObject

return an array of this validations columns



88
89
90
91
92
93
94
95
96
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 88

def columns
  if @columns.nil?
    @columns = {}
    normalized_check_clause_and_column_names[:column_names].each do |column_name|
      add_column table.column(column_name)
    end
  end
  @columns.values
end

#differences_descriptions(other_validation) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 102

def differences_descriptions other_validation
  method_differences_descriptions other_validation, [
    :normalized_check_clause,
    :deferrable,
    :initially_deferred
  ]
end

#has_description?Boolean

return true if this has a description, otherwise false

Returns:

  • (Boolean)


83
84
85
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 83

def has_description?
  !@description.nil?
end

#normalized_check_clauseObject

create a temporary table in postgres to represent this validation and fetch the actual normalized check constraint directly from the database



112
113
114
115
116
117
118
119
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 112

def normalized_check_clause
  # no need to normalize check_clauses which originated from the database
  if from_database?
    check_clause
  else
    normalized_check_clause_and_column_names[:check_clause]
  end
end