Module: DynamicMigrations::Postgres::Server::Database::Schema::Table::Columns

Included in:
DynamicMigrations::Postgres::Server::Database::Schema::Table
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb

Overview

This module has all the tables methods for working with columns

Defined Under Namespace

Classes: ColumnDoesNotExistError, DuplicateColumnError

Instance Method Summary collapse

Instance Method Details

#add_column(name, data_type, **column_options) ⇒ Object

adds a new column to this table, and returns it



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb', line 43

def add_column name, data_type, **column_options
  if has_column? name
    raise DuplicateColumnError, "Column `#{name}` already exists"
  end
  included_target = self
  if included_target.is_a? Table
    new_column = @columns[name] = Column.new source, included_target, name, data_type, **column_options
  else
    raise ModuleIncludedIntoUnexpectedTargetError, included_target
  end
  # sort the hash so that the columns are in alphabetical order by name
  sorted_columns = {}
  @columns.keys.sort.each do |name|
    sorted_columns[name] = @columns[name]
  end
  @columns = sorted_columns
  # return the new column
  new_column
end

#column(name) ⇒ Object

returns the column object for the provided column name, and raises an error if the column does not exist



19
20
21
22
23
24
25
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb', line 19

def column name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  unless has_column? name
    raise ColumnDoesNotExistError, "column `#{name}` does not exist within table `#{self.name}`"
  end
  @columns[name]
end

#columnsObject

returns an array of this tables columns



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

def columns
  @columns.values
end

#columns_hashObject



38
39
40
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb', line 38

def columns_hash
  @columns
end

#has_column?(name) ⇒ Boolean

returns true if this table has a column with the provided name, otherwise false

Returns:

  • (Boolean)

Raises:



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

def has_column? name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @columns.key? name
end