Class: DbSchema::Reader::Postgres::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/db_schema/reader/postgres/table.rb

Constant Summary collapse

SERIAL_TYPES =
{
  smallint: :smallserial,
  integer:  :serial,
  bigint:   :bigserial
}.freeze
DEFAULT_VALUE =
/\A(
  ('(?<date>\d{4}-\d{2}-\d{2})'::date)
    |
  ('(?<time>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}([+-]\d{2})?)'::timestamp)
    |
  ('(?<string>.*)')
    |
  (?<float>\d+\.\d+)
    |
  (?<integer>\d+)
    |
  (?<boolean>true|false)
)/x
FKEY_ACTIONS =
{
  a: :no_action,
  r: :restrict,
  c: :cascade,
  n: :set_null,
  d: :set_default
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, fields_data, indexes_data, checks_data, fkeys_data) ⇒ Table

Returns a new instance of Table.



35
36
37
38
39
40
41
# File 'lib/db_schema/reader/postgres/table.rb', line 35

def initialize(table_name, fields_data, indexes_data, checks_data, fkeys_data)
  @table_name   = table_name
  @fields_data  = fields_data
  @indexes_data = indexes_data
  @checks_data  = checks_data
  @fkeys_data   = fkeys_data
end

Instance Attribute Details

#checks_dataObject (readonly)

Returns the value of attribute checks_data.



33
34
35
# File 'lib/db_schema/reader/postgres/table.rb', line 33

def checks_data
  @checks_data
end

#fields_dataObject (readonly)

Returns the value of attribute fields_data.



33
34
35
# File 'lib/db_schema/reader/postgres/table.rb', line 33

def fields_data
  @fields_data
end

#fkeys_dataObject (readonly)

Returns the value of attribute fkeys_data.



33
34
35
# File 'lib/db_schema/reader/postgres/table.rb', line 33

def fkeys_data
  @fkeys_data
end

#indexes_dataObject (readonly)

Returns the value of attribute indexes_data.



33
34
35
# File 'lib/db_schema/reader/postgres/table.rb', line 33

def indexes_data
  @indexes_data
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



33
34
35
# File 'lib/db_schema/reader/postgres/table.rb', line 33

def table_name
  @table_name
end

Instance Method Details

#checksObject



65
66
67
68
69
70
71
72
# File 'lib/db_schema/reader/postgres/table.rb', line 65

def checks
  checks_data.map do |check_data|
    Definitions::CheckConstraint.new(
      name:      check_data[:name].to_sym,
      condition: check_data[:condition]
    )
  end
end

#definitionObject



43
44
45
46
47
48
49
50
51
# File 'lib/db_schema/reader/postgres/table.rb', line 43

def definition
  Definitions::Table.new(
    table_name,
    fields:       fields,
    indexes:      indexes,
    checks:       checks,
    foreign_keys: foreign_keys
  )
end

#fieldsObject



53
54
55
56
57
# File 'lib/db_schema/reader/postgres/table.rb', line 53

def fields
  fields_data.map do |field_data|
    build_field(field_data)
  end
end

#foreign_keysObject



74
75
76
77
78
# File 'lib/db_schema/reader/postgres/table.rb', line 74

def foreign_keys
  fkeys_data.map do |foreign_key_data|
    build_foreign_key(foreign_key_data)
  end
end

#indexesObject



59
60
61
62
63
# File 'lib/db_schema/reader/postgres/table.rb', line 59

def indexes
  indexes_data.map do |index_data|
    build_index(index_data)
  end.sort_by(&:name)
end