Module: DeclareSchema

Defined in:
lib/declare_schema.rb,
lib/declare_schema/dsl.rb,
lib/declare_schema/model.rb,
lib/declare_schema/command.rb,
lib/declare_schema/railtie.rb,
lib/declare_schema/version.rb,
lib/declare_schema/model/column.rb,
lib/declare_schema/model/field_spec.rb,
lib/declare_schema/schema_change/all.rb,
lib/declare_schema/schema_change/base.rb,
lib/declare_schema/model/habtm_model_shim.rb,
lib/declare_schema/model/index_definition.rb,
lib/declare_schema/schema_change/index_add.rb,
lib/declare_schema/schema_change/table_add.rb,
lib/declare_schema/schema_change/column_add.rb,
lib/generators/declare_schema/support/model.rb,
lib/declare_schema/model/deferred_field_spec.rb,
lib/declare_schema/schema_change/index_remove.rb,
lib/declare_schema/schema_change/table_change.rb,
lib/declare_schema/schema_change/table_remove.rb,
lib/declare_schema/schema_change/table_rename.rb,
lib/declare_schema/schema_change/column_change.rb,
lib/declare_schema/schema_change/column_remove.rb,
lib/declare_schema/schema_change/column_rename.rb,
lib/declare_schema/model/foreign_key_definition.rb,
lib/declare_schema/schema_change/foreign_key_add.rb,
lib/generators/declare_schema/support/thor_shell.rb,
lib/declare_schema/model/table_options_definition.rb,
lib/declare_schema/schema_change/foreign_key_remove.rb,
lib/declare_schema/schema_change/primary_key_change.rb,
lib/generators/declare_schema/model/model_generator.rb,
lib/generators/declare_schema/support/eval_template.rb,
lib/generators/declare_schema/migration/migration_generator.rb,
lib/declare_schema/extensions/active_record/fields_declaration.rb

Defined Under Namespace

Modules: Command, Macros, Model, SchemaChange, Support Classes: Boolean, Dsl, MigrationGenerator, ModelGenerator, MysqlTextMayNotHaveDefault, Railtie, UnknownTypeError

Constant Summary collapse

PLAIN_TYPES =
{
  boolean:  Boolean,
  date:     Date,
  datetime: ActiveSupport::TimeWithZone,
  time:     Time,
  integer:  Integer,
  decimal:  BigDecimal,
  float:    Float,
  string:   String,
  text:     String
}.freeze
SEMVER_8 =
Gem::Version.new('8.0.0').freeze
VERSION =
"4.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.db_migrate_commandObject

Returns the value of attribute db_migrate_command.



40
41
42
# File 'lib/declare_schema.rb', line 40

def db_migrate_command
  @db_migrate_command
end

.default_generate_foreign_keysObject

Returns the value of attribute default_generate_foreign_keys.



40
41
42
# File 'lib/declare_schema.rb', line 40

def default_generate_foreign_keys
  @default_generate_foreign_keys
end

.default_generate_indexingObject

Returns the value of attribute default_generate_indexing.



40
41
42
# File 'lib/declare_schema.rb', line 40

def default_generate_indexing
  @default_generate_indexing
end

.default_nullObject

Returns the value of attribute default_null.



40
41
42
# File 'lib/declare_schema.rb', line 40

def default_null
  @default_null
end

.default_string_limitObject

Returns the value of attribute default_string_limit.



40
41
42
# File 'lib/declare_schema.rb', line 40

def default_string_limit
  @default_string_limit
end

.default_text_limitObject

Returns the value of attribute default_text_limit.



40
41
42
# File 'lib/declare_schema.rb', line 40

def default_text_limit
  @default_text_limit
end

.ignore_modelsObject

Returns the value of attribute ignore_models.



40
41
42
# File 'lib/declare_schema.rb', line 40

def ignore_models
  @ignore_models
end

.ignore_tablesObject

Returns the value of attribute ignore_tables.



40
41
42
# File 'lib/declare_schema.rb', line 40

def ignore_tables
  @ignore_tables
end

.mysql_versionObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/declare_schema.rb', line 54

def mysql_version
  if defined?(@mysql_version)
    @mysql_version
  else
    @mysql_version =
      if ActiveRecord::Base.connection.class.name.match?(/mysql/i)
        version_string = ActiveRecord::Base.connection.select_value('SELECT VERSION()')
        Gem::Version.new(version_string)
      end
  end
end

Class Method Details

.clear_default_schemaObject



146
147
148
# File 'lib/declare_schema.rb', line 146

def clear_default_schema
  @default_schema = nil
end

.current_adapter(model_class = ActiveRecord::Base) ⇒ Object



176
177
178
# File 'lib/declare_schema.rb', line 176

def current_adapter(model_class = ActiveRecord::Base)
  model_class.connection_db_config.adapter
end

.default_charsetObject



88
89
90
# File 'lib/declare_schema.rb', line 88

def default_charset
  @default_charset ||= normalize_charset(@default_charset_before_normalization)
end

.default_charset=(charset) ⇒ Object



82
83
84
85
86
# File 'lib/declare_schema.rb', line 82

def default_charset=(charset)
  charset.is_a?(String) or raise ArgumentError, "charset must be a string (got #{charset.inspect})"
  @default_charset_before_normalization = charset
  @default_charset = nil
end

.default_collationObject



98
99
100
# File 'lib/declare_schema.rb', line 98

def default_collation
  @default_collation ||= normalize_collation(@default_collation_before_normalization)
end

.default_collation=(collation) ⇒ Object



92
93
94
95
96
# File 'lib/declare_schema.rb', line 92

def default_collation=(collation)
  collation.is_a?(String) or raise ArgumentError, "collation must be a string (got #{collation.inspect})"
  @default_collation_before_normalization = collation
  @default_collation = nil
end

.default_generated_primary_key_typeObject

Default primary key type for generated foreign keys when we have no parent model to mirror (e.g. polymorphic FKs, non-declare_schema parents). Mirrors config.generators.primary_key_type so apps that override the Rails default get consistent behavior. Memoized — Rails config is set at boot.



184
185
186
187
# File 'lib/declare_schema.rb', line 184

def default_generated_primary_key_type
  @default_generated_primary_key_type ||=
    Rails.application.config.generators.options.dig(:active_record, :primary_key_type) || :bigint
end

.default_schema(&block) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/declare_schema.rb', line 137

def default_schema(&block)
  if block.nil?
    @default_schema # equivalent to attr_reader :default_schema
  else
    block.respond_to?(:call) or raise "default_schema must be passed a block that responds to call"
    @default_schema = block
  end
end

.deprecatorObject



172
173
174
# File 'lib/declare_schema.rb', line 172

def deprecator
  @deprecator ||= ActiveSupport::Deprecation.new('3.0', 'DeclareSchema')
end

.max_index_and_constraint_name_lengthObject



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/declare_schema.rb', line 160

def max_index_and_constraint_name_length
  unless defined?(@max_index_and_constraint_name_length)
    @max_index_and_constraint_name_length = case current_adapter
                                            when 'postgresql'
                                              nil
                                            else
                                              64  # limit for MySQL
                                            end
  end
  @max_index_and_constraint_name_length
end

.max_index_and_constraint_name_length=(length) ⇒ Object



155
156
157
158
# File 'lib/declare_schema.rb', line 155

def max_index_and_constraint_name_length=(length)
  length.is_a?(Integer) || length.nil? or raise ArgumentError, "max_index_and_constraint_name_length must be an Integer or nil (meaning unlimited)"
  @max_index_and_constraint_name_length = length
end

.normalize_charset(charset) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/declare_schema.rb', line 66

def normalize_charset(charset)
  if mysql_version && mysql_version >= SEMVER_8 && charset == 'utf8'
    'utf8mb3'
  else
    charset
  end
end

.normalize_collation(collation) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/declare_schema.rb', line 74

def normalize_collation(collation)
  if mysql_version && mysql_version >= SEMVER_8
    collation.sub(/\Autf8_/, 'utf8mb3_')
  else
    collation
  end
end

.to_class(type) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/declare_schema.rb', line 43

def to_class(type)
  case type
  when Class
    type
  when Symbol, String
    PLAIN_TYPES[type.to_sym]
  else
    raise ArgumentError, "expected Class or Symbol or String: got #{type.inspect}"
  end
end