Module: DynamicMigrations::Postgres::Server::Database::Differences::ToMigrations::Schemas::Tables

Included in:
DynamicMigrations::Postgres::Server::Database::Differences::ToMigrations
Defined in:
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/columns.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/indexes.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/triggers.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/primary_key.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/unique_constraints.rb,
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/foreign_key_constraints.rb

Defined Under Namespace

Modules: Columns, ForeignKeyConstraints, Indexes, PrimaryKey, Triggers, UniqueConstraints, Validations

Instance Method Summary collapse

Instance Method Details

#process_dependents(schema_name, table_name, configuration_table, database_table, skip_columns: false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables.rb', line 74

def process_dependents schema_name, table_name, configuration_table, database_table, skip_columns: false
  # we skip columns if we are processing the table for the first time, as they are processed within the table creation
  unless skip_columns
    process_columns schema_name, table_name, configuration_table[:columns] || {}, database_table[:columns] || {}
  end
  process_foreign_key_constraints schema_name, table_name, configuration_table[:foreign_key_constraints] || {}, database_table[:foreign_key_constraints] || {}
  process_indexes schema_name, table_name, configuration_table[:indexes] || {}, database_table[:indexes] || {}
  process_triggers schema_name, table_name, configuration_table[:triggers] || {}, database_table[:triggers] || {}
  process_unique_constraints schema_name, table_name, configuration_table[:unique_constraints] || {}, database_table[:unique_constraints] || {}
  process_validations schema_name, table_name, configuration_table[:validations] || {}, database_table[:validations] || {}
  # Process the primary key. The primary key is singular (max of one per table)
  process_primary_key schema_name, table_name, configuration_table[:primary_key], database_table[:primary_key]
end

#process_table(schema_name, table_name, configuration_table, database_table) ⇒ Object



21
22
23
24
25
26
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
66
67
68
69
70
71
72
# File 'lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables.rb', line 21

def process_table schema_name, table_name, configuration_table, database_table
  # If the table exists in the configuration but not in the database
  # then we have to create it.
  if configuration_table[:exists] == true && !database_table[:exists]
    log.debug "  Table `#{table_name}` exists in configuration but not in the database"

    # a migration to create the table
    table = @database.configured_schema(schema_name).table(table_name)
    @generator.create_table table

    # we process everything else after we create the table, because the other
    # database objects are dependent on the table
    process_dependents schema_name, table_name, configuration_table, {}, skip_columns: true

  # If the schema exists in the database but not in the configuration
  # then we need to delete it.
  elsif database_table[:exists] == true && !configuration_table[:exists]
    log.debug "  Table `#{table_name}` exists in database but not in the configuration"

    # we process everything else before we drop the table, because the other
    # database objects are dependent on the table
    process_dependents schema_name, table_name, {}, database_table

    # a migration to remove the table
    table = @database.loaded_schema(schema_name).table(table_name)
    @generator.drop_table table

  # If the table exists in both the configuration and database representations
  # but the description is different then we need to update the description.
  elsif configuration_table[:description][:matches] == false
    log.debug "  Table `#{table_name}` exists in both configuration and the database"

    table = @database.configured_schema(schema_name).table(table_name)
    # if the description was removed
    if configuration_table[:description].nil?
      log.debug "    Table `#{table_name}` description exists in database but not in the configuration"
      @generator.remove_table_comment table
    else
      log.debug "    Table `#{table_name}` description does not match"
      @generator.set_table_comment table
    end

    # process everything else
    process_dependents schema_name, table_name, configuration_table, database_table

  else
    log.debug "  Table `#{table_name}` exists in both configuration and the database"
    # process everything else
    process_dependents schema_name, table_name, configuration_table, database_table

  end
end

#process_tables(schema_name, configuration_tables, database_tables) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables.rb', line 11

def process_tables schema_name, configuration_tables, database_tables
  # process all the tables
  log.debug "  Processing Tables"
  table_names = (configuration_tables.keys + database_tables.keys).uniq
  table_names.each do |table_name|
    log.debug "  Processing Table #{table_name}"
    process_table schema_name, table_name, configuration_tables[table_name] || {}, database_tables[table_name] || {}
  end
end