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.
Instance Method Summary collapse
-
#geneva_drive_key_type ⇒ Symbol
Detects the appropriate key type for GenevaDrive tables.
-
#geneva_drive_table_options ⇒ Hash
Returns options hash for create_table based on detected primary key type.
Instance Method Details
#geneva_drive_key_type ⇒ Symbol
Detects the appropriate key type for GenevaDrive tables. Returns :uuid if the schema predominantly uses UUIDs, otherwise :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_options ⇒ Hash
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).
41 42 43 44 45 46 47 48 49 |
# File 'lib/geneva_drive/migration_helpers.rb', line 41 def return {} unless geneva_drive_key_type == :uuid = {id: :uuid} if (default_function = _geneva_drive_dominant_uuid_default) [:default] = default_function end end |