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/field_declaration_dsl.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/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, FieldDeclarationDsl, 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 =
"2.3.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.db_migrate_commandObject

Returns the value of attribute db_migrate_command.



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

def db_migrate_command
  @db_migrate_command
end

.default_generate_foreign_keysObject

Returns the value of attribute default_generate_foreign_keys.



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

def default_generate_foreign_keys
  @default_generate_foreign_keys
end

.default_generate_indexingObject

Returns the value of attribute default_generate_indexing.



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

def default_generate_indexing
  @default_generate_indexing
end

.default_nullObject

Returns the value of attribute default_null.



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

def default_null
  @default_null
end

.default_string_limitObject

Returns the value of attribute default_string_limit.



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

def default_string_limit
  @default_string_limit
end

.default_text_limitObject

Returns the value of attribute default_text_limit.



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

def default_text_limit
  @default_text_limit
end

.mysql_versionObject



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

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



134
135
136
# File 'lib/declare_schema.rb', line 134

def clear_default_schema
  @default_schema = nil
end

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



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

def current_adapter(model_class = ActiveRecord::Base)
  if Rails::VERSION::MAJOR >= 6.1
    model_class.connection_db_config.adapter
  else
    model_class.connection_config[:adapter]
  end
end

.default_charsetObject



86
87
88
# File 'lib/declare_schema.rb', line 86

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

.default_charset=(charset) ⇒ Object



80
81
82
83
84
# File 'lib/declare_schema.rb', line 80

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



96
97
98
# File 'lib/declare_schema.rb', line 96

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

.default_collation=(collation) ⇒ Object



90
91
92
93
94
# File 'lib/declare_schema.rb', line 90

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_schema(&block) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/declare_schema.rb', line 125

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



160
161
162
# File 'lib/declare_schema.rb', line 160

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

.max_index_and_constraint_name_lengthObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/declare_schema.rb', line 148

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



143
144
145
146
# File 'lib/declare_schema.rb', line 143

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



64
65
66
67
68
69
70
# File 'lib/declare_schema.rb', line 64

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

.normalize_collation(collation) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/declare_schema.rb', line 72

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

.to_class(type) ⇒ Object



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

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