Module: PgColumnBytePacker::SchemaCreation

Defined in:
lib/pg_column_byte_packer/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#visit_TableDefinition(o) ⇒ Object



6
7
8
9
10
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
# File 'lib/pg_column_byte_packer/schema_statements.rb', line 6

def visit_TableDefinition(o)
  columns_hash = o.instance_variable_get(:@columns_hash)

  sorted_column_tuples = columns_hash.sort_by do |name, col|
    sql_type = type_to_sql(
      col.type,
      :limit => col.limit,
      :precision => col.precision,
      :scale => col.scale,
      :primary_key => col.primary_key?,
    )

    nullable = if sql_type.match(/\A(big)?serial( primary key)?/)
      col.null == true
    else
      col.null.nil? || col.null == true
    end

    PgColumnBytePacker.ordering_key_for_column(
      connection: @conn,
      name: name,
      sql_type: sql_type,
      primary_key: col.options[:primary_key],
      nullable: nullable,
      has_default: !col.default.nil?
    )
  end

  columns_hash.clear
  sorted_column_tuples.each do |(name, column)|
    columns_hash[name] = column
  end

  super(o)
end