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

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

Overview

This class represents a single column within a postgres table

Defined Under Namespace

Classes: ExpectedTableError, UnexpectedEnumError

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, name, data_type, null: true, default: nil, description: nil, interval_type: nil, enum: nil) ⇒ Column

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

Raises:



27
28
29
30
31
32
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
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 27

def initialize source, table, name, data_type, null: true, default: nil, description: nil, interval_type: nil, enum: nil
  super source
  raise ExpectedTableError, table unless table.is_a? Table
  @table = table

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

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

  @null = null

  unless default.nil?
    raise ExpectedStringError, default unless default.is_a? String
    @default = default
  end

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

  @interval_type = interval_type

  if enum
    unless enum.is_a? Enum
      raise UnexpectedEnumError, "#{enum} is not a valid enum"
    end
    if (array? && @data_type != :"#{enum.full_name}[]") || (!array? && @data_type != enum.full_name)
      raise UnexpectedEnumError, "enum `#{enum.full_name}` does not match this column's data type `#{@data_type}`"
    end
    @enum = enum
    # associate this column with the enum (so they are aware of each other)
    enum.add_column self

  end
end

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



19
20
21
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 19

def data_type
  @data_type
end

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



20
21
22
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 20

def description
  @description
end

#enumObject (readonly)

Returns the value of attribute enum.



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

def enum
  @enum
end

#interval_typeObject (readonly)

Returns the value of attribute interval_type.



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

def interval_type
  @interval_type
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 18

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



21
22
23
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 21

def null
  @null
end

#tableObject (readonly)

Returns the value of attribute table.



17
18
19
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 17

def table
  @table
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 72

def array?
  @data_type.end_with? "[]"
end

#base_data_typeObject

for arrays returns the column type without the array brackets, for non arrays jsut returnms the column type



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

def base_data_type
  array? ? @data_type[0..-3]&.to_sym : @data_type
end

#enum?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 76

def enum?
  !@enum.nil?
end

#has_description?Boolean

return true if this column has a description, otherwise false

Returns:

  • (Boolean)


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

def has_description?
  !@description.nil?
end