Module: OpenLoam::Generators::PrimaryKeyOptions

Included in:
EntityGenerator, InstallGenerator
Defined in:
lib/generators/open_loam/primary_key_options.rb

Overview

Shared primary-key shaping for open_loam:install and open_loam:entity.

OpenLoam's migrations used to spell every key bigint — implicitly in create_table, and explicitly in t.references and the polymorphic *_id columns. In an app whose own tables use string or uuid keys the foreign keys then do not line up with the tables they point at, and every generated migration has to be hand-edited after the generator runs.

The key type is taken from, in order: an explicit --primary-key-type, the host app's own config.generators setting (so an app that already told Rails what its keys look like does not have to tell OpenLoam separately), and bigint as the Rails default.

The templates ask for the rendered fragment rather than the type, so bigint keeps emitting exactly the migrations it emitted before — no id: and no type: — and only a non-default key type adds anything.

Constant Summary collapse

SUPPORTED =
%w[bigint uuid string].freeze
DEFAULT_STRING_LIMIT =

varchar(36) is the usual shape for a UUID stored as a string, and it is the case in the report that prompted this. An app that stores them some other width passes --key-limit.

36

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/generators/open_loam/primary_key_options.rb', line 27

def self.included(base)
  base.class_option :primary_key_type, type: :string, default: nil,
                                       desc: "Key type for the generated tables: #{SUPPORTED.join(', ')}. " \
                                             "Defaults to the app's config.generators primary_key_type, then bigint."
  base.class_option :key_limit, type: :numeric, default: nil,
                                desc: "Column limit for a string key (default #{DEFAULT_STRING_LIMIT}). Ignored for bigint and uuid."
end