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 =
"1.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.db_migrate_commandObject

Returns the value of attribute db_migrate_command.



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

def db_migrate_command
  @db_migrate_command
end

.default_generate_foreign_keysObject

Returns the value of attribute default_generate_foreign_keys.



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

def default_generate_foreign_keys
  @default_generate_foreign_keys
end

.default_generate_indexingObject

Returns the value of attribute default_generate_indexing.



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

def default_generate_indexing
  @default_generate_indexing
end

.default_nullObject

Returns the value of attribute default_null.



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

def default_null
  @default_null
end

.default_string_limitObject

Returns the value of attribute default_string_limit.



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

def default_string_limit
  @default_string_limit
end

.default_text_limitObject

Returns the value of attribute default_text_limit.



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

def default_text_limit
  @default_text_limit
end

.max_index_and_constraint_name_lengthObject

Returns the value of attribute max_index_and_constraint_name_length.



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

def max_index_and_constraint_name_length
  @max_index_and_constraint_name_length
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



136
137
138
# File 'lib/declare_schema.rb', line 136

def clear_default_schema
  @default_schema = nil
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_schema(&block) ⇒ Object



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

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

.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