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, InvalidNameError, 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, template: nil) ⇒ Validation

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

Raises:



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

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

  raise InvalidNameError, "Unexpected name `#{name}`. Name should be a Symbol" unless name.is_a? Symbol
  raise InvalidNameError, "The name `#{name}` is too long. Names must be less than 64 characters" unless name.length < 64
  @name = name

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

  # 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.freeze
    @description = nil if description == ""
  end

  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.



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

def check_clause
  @check_clause
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#column_namesObject



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

def column_names
  columns.map(&:name).sort
end

#columnsObject

return an array of this validations columns



84
85
86
87
88
89
90
91
92
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 84

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



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

def differences_descriptions other_validation
  method_differences_descriptions other_validation, [
    :normalized_check_clause
  ]
end

#has_description?Boolean

return true if this has a description, otherwise false

Returns:

  • (Boolean)


79
80
81
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 79

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



106
107
108
109
110
111
112
113
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb', line 106

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