Module: DynamicMigrations::Postgres::Generator::Table

Included in:
DynamicMigrations::Postgres::Generator
Defined in:
lib/dynamic_migrations/postgres/generator/table.rb

Defined Under Namespace

Classes: NoTableColumnCommentError, NoTableCommentError

Instance Method Summary collapse

Instance Method Details

#create_table(table, code_comment = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
# File 'lib/dynamic_migrations/postgres/generator/table.rb', line 11

def create_table table, code_comment = nil
  if table.description.nil?
    raise NoTableCommentError, "Refusing to generate create_table migration, no description was provided for `#{table.schema.name}`.`#{table.name}`"
  end

  # We only add the columns that are not enums from within the add_table block, this
  # is because columns that are enums require those enums to be created first and we
  # want to create those as seperate fragments which have the correct dependency metadata
  columns_without_enums = table.columns.reject(&:enum)
  columns_with_enums = table.columns.select(&:enum)

  fragments = []
  fragments << add_fragment(schema: table.schema,
    table: table,
    migration_method: :create_table,
    object: table,
    code_comment: code_comment,
    migration: <<~RUBY
      table_comment = <<~COMMENT
        #{indent table.description || ""}
      COMMENT
      create_table :#{table.name}, #{table_options table} do |t|
        #{indent table_columns(columns_without_enums)}
      end
    RUBY
  )

  # seperately add the columns that are enums (so dependency managment works correctly)
  fragments += columns_with_enums.map do |column|
    add_column column
  end

  # return all the fragments (we do this with all generators so e can more easily test the methods)
  fragments
end

#drop_table(table, code_comment = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/dynamic_migrations/postgres/generator/table.rb', line 47

def drop_table table, code_comment = nil
  add_fragment schema: table.schema,
    table: table,
    migration_method: :drop_table,
    object: table,
    code_comment: code_comment,
    migration: <<~RUBY
      drop_table :#{table.name}, force: true
    RUBY
end

#remove_table_comment(table, code_comment = nil) ⇒ Object

remove the comment from a table



79
80
81
82
83
84
85
86
87
88
# File 'lib/dynamic_migrations/postgres/generator/table.rb', line 79

def remove_table_comment table, code_comment = nil
  add_fragment schema: table.schema,
    table: table,
    migration_method: :remove_table_comment,
    object: table,
    code_comment: code_comment,
    migration: <<~RUBY
      remove_table_comment :#{table.name}
    RUBY
end

#set_table_comment(table, code_comment = nil) ⇒ Object

add a comment to a table



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dynamic_migrations/postgres/generator/table.rb', line 59

def set_table_comment table, code_comment = nil
  description = table.description

  if description.nil?
    raise MissingDescriptionError, "Missing required description for table `#{table.schema.name}.#{table.name}`"
  end

  add_fragment schema: table.schema,
    table: table,
    migration_method: :set_table_comment,
    object: table,
    code_comment: code_comment,
    migration: <<~RUBY
      set_table_comment :#{table.name}, <<~COMMENT
        #{indent description}
      COMMENT
    RUBY
end