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, InvalidNameError, 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:



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
66
67
68
69
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 30

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 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 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.freeze
    @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.



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

def data_type
  @data_type
end

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#enumObject (readonly)

Returns the value of attribute enum.



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

def enum
  @enum
end

#interval_typeObject (readonly)

Returns the value of attribute interval_type.



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

def interval_type
  @interval_type
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



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

def null
  @null
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


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

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



86
87
88
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/column.rb', line 86

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

#enum?Boolean

Returns:

  • (Boolean)


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

def enum?
  !@enum.nil?
end

#has_description?Boolean

return true if this column has a description, otherwise false

Returns:

  • (Boolean)


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

def has_description?
  !@description.nil?
end