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

Inherits:
DynamicMigrations::Postgres::Server::Database::Source show all
Includes:
Columns, ForeignKeyConstraints, Indexes, Triggers, UniqueConstraints, Validations
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/index.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/column.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/trigger.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/triggers.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/primary_key.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraint.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb,
lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb

Overview

This class represents a postgres table.

Defined Under Namespace

Modules: Columns, ForeignKeyConstraints, Indexes, Triggers, UniqueConstraints, Validations Classes: Column, ExpectedSchemaError, ForeignKeyConstraint, Index, PrimaryKey, PrimaryKeyAlreadyExistsError, PrimaryKeyDoesNotExistError, Trigger, UniqueConstraint, Validation

Instance Attribute Summary collapse

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

#source

Instance Method Summary collapse

Methods included from UniqueConstraints

#add_unique_constraint, #has_unique_constraint?, #unique_constraint, #unique_constraints, #unique_constraints_hash

Methods included from Triggers

#add_trigger, #has_trigger?, #trigger, #triggers, #triggers_hash

Methods included from ForeignKeyConstraints

#add_foreign_key_constraint, #add_remote_foreign_key_constraint, #foreign_key_constraint, #foreign_key_constraints, #foreign_key_constraints_hash, #has_foreign_key_constraint?

Methods included from Indexes

#add_index, #has_index?, #index, #indexes, #indexes_hash

Methods included from Validations

#add_validation, #has_validation?, #validation, #validations, #validations_hash

Methods included from Columns

#add_column, #column, #columns, #columns_hash, #has_column?

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

#assert_is_a_symbol!, #from_configuration?, #from_database?

Constructor Details

#initialize(source, schema, name, description: nil) ⇒ Table

initialize a new object to represent a postgres table



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dynamic_migrations/postgres/server/database/schema/table.rb', line 32

def initialize source, schema, name, description: nil
  super source

  raise ExpectedSchemaError, schema unless schema.is_a? Schema
  @schema = schema

  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

  @columns = {}
  @validations = {}
  @indexes = {}
  @foreign_key_constraints = {}
  @remote_foreign_key_constraints = []
  @triggers = {}
  @unique_constraints = {}
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#remote_foreign_key_constraintsObject (readonly)

Returns the value of attribute remote_foreign_key_constraints.



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

def remote_foreign_key_constraints
  @remote_foreign_key_constraints
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Instance Method Details

#add_primary_key(name, column_names, **primary_key_options) ⇒ Object

add a primary key to this table



62
63
64
65
66
# File 'lib/dynamic_migrations/postgres/server/database/schema/table.rb', line 62

def add_primary_key name, column_names, **primary_key_options
  raise PrimaryKeyAlreadyExistsError if @primary_key
  columns = column_names.map { |column_name| column column_name }
  @primary_key = PrimaryKey.new source, self, columns, name, **primary_key_options
end

#create_temp_table(connection, temp_table_name) ⇒ Object

Used within validations and triggers when normalizing check clauses and other SQL statements which require a table to process the SQL.

This method returns a hash representation of any temporary enums created to satisfy the columns in the table



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dynamic_migrations/postgres/server/database/schema/table.rb', line 87

def create_temp_table connection, temp_table_name
  # create the temp table and add the expected columns

  # if any of the columns are enums, then we need to create a temporary enum type for them.
  # we cant just create temporary columns as text fields because postgres may automatically
  # add casts to those columns, which would result in a different normalized check clause
  temp_enums = {}

  # an array of sql column definitions for within the create table SQL
  # we process each column individually like this so that we can create temporary enums for
  # any enum columns
  columns_sql = columns.map do |column|
    enum = column.enum
    if enum
      # create the temporary enum type
      temp_enum_name = "#{temp_table_name}_enum_#{temp_enums.count}"
      connection.exec("        CREATE TYPE \#{temp_enum_name} as ENUM ('\#{enum.values.join(\"','\")}');\n      SQL\n      temp_enums[temp_enum_name] = enum\n\n      # return the column definition used within the CREATE TABLE SQL\n      data_type = column.array? ? \"\#{temp_enum_name}[]\" : temp_enum_name\n      \"\\\"\#{column.name}\\\" \#{data_type}\"\n\n    else\n      # return the column definition used within the CREATE TABLE SQL\n      \"\\\"\#{column.name}\\\" \#{column.data_type}\"\n    end\n  end\n\n  # in case any of the columnbs are citext columns\n  connection.exec(\"CREATE EXTENSION IF NOT EXISTS citext;\")\n\n  # note, this is not actually a TEMP TABLE, it is created within a transaction\n  # and rolled back.\n  connection.exec(<<~SQL)\n    CREATE TABLE \#{temp_table_name} (\n      \#{columns_sql.join(\", \")}\n    );\n  SQL\n\n  temp_enums\nend\n")

#has_description?Boolean

returns true if this table has a description, otehrwise false

Returns:

  • (Boolean)


57
58
59
# File 'lib/dynamic_migrations/postgres/server/database/schema/table.rb', line 57

def has_description?
  !@description.nil?
end

#has_primary_key?Boolean

returns true if this table has a primary key, otherwise false

Returns:

  • (Boolean)


69
70
71
# File 'lib/dynamic_migrations/postgres/server/database/schema/table.rb', line 69

def has_primary_key?
  !@primary_key.nil?
end

#primary_keyObject

returns a primary key if one exists, else raises an error



74
75
76
77
78
79
80
# File 'lib/dynamic_migrations/postgres/server/database/schema/table.rb', line 74

def primary_key
  pk = @primary_key
  unless pk
    raise PrimaryKeyDoesNotExistError
  end
  pk
end