Module: DynamicMigrations::Postgres::Server::Database::Schema::Tables

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

Defined Under Namespace

Classes: TableAlreadyExistsError, TableDoesNotExistError

Instance Method Summary collapse

Instance Method Details

#add_table(table_name, description: nil) ⇒ Object

create and add a new table from a provided table name



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dynamic_migrations/postgres/server/database/schema/tables.rb', line 16

def add_table table_name, description: nil
  raise ExpectedSymbolError, table_name unless table_name.is_a? Symbol
  if has_table? table_name
    raise(TableAlreadyExistsError, "Table #{table_name} already exists")
  end
  included_target = self
  if included_target.is_a? Schema
    new_table = @tables[table_name] = Table.new source, included_target, table_name, description: description
  else
    raise ModuleIncludedIntoUnexpectedTargetError, included_target
  end
  # sort the hash so that the tables are in alphabetical order by name
  sorted_tables = {}
  @tables.keys.sort.each do |table_name|
    sorted_tables[table_name] = @tables[table_name]
  end
  @tables = sorted_tables
  # return the new table
  new_table
end

#has_table?(table_name) ⇒ Boolean

returns true/false representing if a table with the provided name exists

Returns:

  • (Boolean)

Raises:



45
46
47
48
# File 'lib/dynamic_migrations/postgres/server/database/schema/tables.rb', line 45

def has_table? table_name
  raise ExpectedSymbolError, table_name unless table_name.is_a? Symbol
  @tables.key? table_name
end

#table(table_name) ⇒ Object

return a table by its name, raises an error if the table does not exist



38
39
40
41
42
# File 'lib/dynamic_migrations/postgres/server/database/schema/tables.rb', line 38

def table table_name
  raise ExpectedSymbolError, table_name unless table_name.is_a? Symbol
  raise TableDoesNotExistError unless has_table? table_name
  @tables[table_name]
end

#tablesObject

returns an array of all tables in the schema



51
52
53
# File 'lib/dynamic_migrations/postgres/server/database/schema/tables.rb', line 51

def tables
  @tables.values
end

#tables_hashObject



55
56
57
# File 'lib/dynamic_migrations/postgres/server/database/schema/tables.rb', line 55

def tables_hash
  @tables
end