Module: GenevaDrive::MigrationHelpers

Defined in:
lib/geneva_drive/migration_helpers.rb

Overview

Helper methods for GenevaDrive migrations. Provides runtime detection of database adapter and primary key format.

Examples:

Using in a migration

class CreateGenevaDriveWorkflows < ActiveRecord::Migration[7.2]
  include GenevaDrive::MigrationHelpers

  def change
    create_table :geneva_drive_workflows, **geneva_drive_table_options do |t|
      t.references :hero, polymorphic: true, type: geneva_drive_key_type
      # ...
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#geneva_drive_key_typeSymbol

Detects the appropriate key type for GenevaDrive tables. Returns :uuid if the schema predominantly uses UUIDs, otherwise :bigint.

Returns:

  • (Symbol)

    :uuid or :bigint



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/geneva_drive/migration_helpers.rb', line 23

def geneva_drive_key_type
  @_geneva_drive_key_type ||= begin
    id_columns = _geneva_drive_app_id_columns
    return :bigint if id_columns.empty?

    uuid_count = id_columns.count { |col| col.sql_type.downcase.match?(/uuid|char\(36\)|varchar\(36\)/) }
    other_count = id_columns.size - uuid_count

    (uuid_count > other_count) ? :uuid : :bigint
  end
end

#geneva_drive_table_optionsHash

Returns options hash for create_table based on detected primary key type. When UUIDs are detected, includes the most common default function from existing tables so that new tables match the application's convention (e.g. uuid_generate_v7 instead of gen_random_uuid).

Returns:

  • (Hash)

    options for create_table (e.g., {id: :uuid, default: "uuid7()"} or {})



41
42
43
44
45
46
47
48
49
# File 'lib/geneva_drive/migration_helpers.rb', line 41

def geneva_drive_table_options
  return {} unless geneva_drive_key_type == :uuid

  options = {id: :uuid}
  if (default_function = _geneva_drive_dominant_uuid_default)
    options[:default] = default_function
  end
  options
end