Module: Ductwork::MigrationHelper

Included in:
Migration
Defined in:
lib/ductwork/migration_helper.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(table_object, association_name, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ductwork/migration_helper.rb', line 16

def belongs_to(table_object, association_name, **options)
  full_options = if postgresql?
                   { type: uuid_column_type }.merge(options)
                 else
                   { type: uuid_column_type, limit: 36 }.merge(options)
                 end

  table_object.belongs_to association_name, **full_options
end

#create_ductwork_table(table_name, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/ductwork/migration_helper.rb', line 5

def create_ductwork_table(table_name, &block)
  if postgresql?
    create_table table_name, id: :uuid, &block
  else
    create_table table_name, id: false do |table|
      table.string :id, limit: 36, null: false, primary_key: true
      block.call(table)
    end
  end
end

#mysql?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ductwork/migration_helper.rb', line 38

def mysql?
  connection.adapter_name.match?(/mysql|trilogy/i)
end

#postgresql?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ductwork/migration_helper.rb', line 34

def postgresql?
  connection.adapter_name.match?(/postgresql/i)
end

#uuid_column_typeObject



26
27
28
29
30
31
32
# File 'lib/ductwork/migration_helper.rb', line 26

def uuid_column_type
  if postgresql?
    :uuid
  else
    :string
  end
end