declare_schema 0.8.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/declare_schema_build.yml +1 -1
- data/CHANGELOG.md +37 -0
- data/Gemfile.lock +1 -1
- data/README.md +102 -14
- data/lib/declare_schema.rb +57 -0
- data/lib/declare_schema/dsl.rb +39 -0
- data/lib/declare_schema/extensions/active_record/fields_declaration.rb +23 -4
- data/lib/declare_schema/model.rb +4 -6
- data/lib/declare_schema/model/column.rb +1 -1
- data/lib/declare_schema/model/field_spec.rb +9 -6
- data/lib/declare_schema/model/foreign_key_definition.rb +6 -11
- data/lib/declare_schema/model/index_definition.rb +1 -20
- data/lib/declare_schema/schema_change/all.rb +22 -0
- data/lib/declare_schema/schema_change/base.rb +45 -0
- data/lib/declare_schema/schema_change/column_add.rb +27 -0
- data/lib/declare_schema/schema_change/column_change.rb +32 -0
- data/lib/declare_schema/schema_change/column_remove.rb +20 -0
- data/lib/declare_schema/schema_change/column_rename.rb +23 -0
- data/lib/declare_schema/schema_change/foreign_key_add.rb +25 -0
- data/lib/declare_schema/schema_change/foreign_key_remove.rb +20 -0
- data/lib/declare_schema/schema_change/index_add.rb +33 -0
- data/lib/declare_schema/schema_change/index_remove.rb +20 -0
- data/lib/declare_schema/schema_change/primary_key_change.rb +33 -0
- data/lib/declare_schema/schema_change/table_add.rb +37 -0
- data/lib/declare_schema/schema_change/table_change.rb +36 -0
- data/lib/declare_schema/schema_change/table_remove.rb +22 -0
- data/lib/declare_schema/schema_change/table_rename.rb +22 -0
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/USAGE +14 -24
- data/lib/generators/declare_schema/migration/migration_generator.rb +40 -38
- data/lib/generators/declare_schema/migration/migrator.rb +184 -198
- data/lib/generators/declare_schema/migration/templates/migration.rb.erb +1 -1
- data/lib/generators/declare_schema/support/model.rb +4 -4
- data/spec/lib/declare_schema/api_spec.rb +8 -8
- data/spec/lib/declare_schema/field_declaration_dsl_spec.rb +41 -15
- data/spec/lib/declare_schema/field_spec_spec.rb +44 -2
- data/spec/lib/declare_schema/generator_spec.rb +5 -5
- data/spec/lib/declare_schema/interactive_primary_key_spec.rb +117 -28
- data/spec/lib/declare_schema/migration_generator_spec.rb +1990 -843
- data/spec/lib/declare_schema/model/column_spec.rb +49 -23
- data/spec/lib/declare_schema/model/foreign_key_definition_spec.rb +158 -57
- data/spec/lib/declare_schema/model/habtm_model_shim_spec.rb +0 -2
- data/spec/lib/declare_schema/model/index_definition_spec.rb +189 -78
- data/spec/lib/declare_schema/model/table_options_definition_spec.rb +75 -11
- data/spec/lib/declare_schema/schema_change/base_spec.rb +75 -0
- data/spec/lib/declare_schema/schema_change/column_add_spec.rb +30 -0
- data/spec/lib/declare_schema/schema_change/column_change_spec.rb +33 -0
- data/spec/lib/declare_schema/schema_change/column_remove_spec.rb +30 -0
- data/spec/lib/declare_schema/schema_change/column_rename_spec.rb +28 -0
- data/spec/lib/declare_schema/schema_change/foreign_key_add_spec.rb +29 -0
- data/spec/lib/declare_schema/schema_change/foreign_key_remove_spec.rb +29 -0
- data/spec/lib/declare_schema/schema_change/index_add_spec.rb +56 -0
- data/spec/lib/declare_schema/schema_change/index_remove_spec.rb +29 -0
- data/spec/lib/declare_schema/schema_change/primary_key_change_spec.rb +69 -0
- data/spec/lib/declare_schema/schema_change/table_add_spec.rb +50 -0
- data/spec/lib/declare_schema/schema_change/table_change_spec.rb +30 -0
- data/spec/lib/declare_schema/schema_change/table_remove_spec.rb +27 -0
- data/spec/lib/declare_schema/schema_change/table_rename_spec.rb +27 -0
- data/spec/lib/declare_schema_spec.rb +101 -0
- data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +71 -13
- data/spec/spec_helper.rb +1 -1
- data/spec/support/acceptance_spec_helpers.rb +2 -2
- metadata +36 -6
- data/test_responses.txt +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 516d119d979224ddbe841b2dd5117fc5891b5c79e09261443c7df6f3c2335910
|
4
|
+
data.tar.gz: 32621c93b56814f9a8dd30b2d622d2c6a4c536cdc40b2145d078134252e14401
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 912c885f9fea463bae49937b6cb85ba6f7ab8cd4739327e6c662eb7b3cece701e1d42be9f251b97ba4a7b544eddd2a854a99ad8992d4a1fa5563acfd81cff989
|
7
|
+
data.tar.gz: 7429b27a56a0cfc27f8af16216528ee7137e5f9d0f20301f5c6177decc816c09b663bf78e5ea22d955b03ed58a7a103663980ec08ab14bb2d27f6d6ccb581787
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,39 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
4
4
|
|
5
5
|
Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [0.11.0] - 2021-03-22
|
8
|
+
### Removed
|
9
|
+
- Removed `g|m|c` prompt entirely, since it was confusing. Instead, the migration is
|
10
|
+
always generated; the user may press ^C at the filename prompt to cancel.
|
11
|
+
The migration will be run if `--migrate` is passed; otherwise, the migrate command will be displayed to be run later.
|
12
|
+
### Added
|
13
|
+
- Added the new configuration option `DeclareSchema.@db_migrate_command =`.
|
14
|
+
### Fixed
|
15
|
+
- Fixed bug where foreign key constraint names are not globally unique
|
16
|
+
|
17
|
+
## [0.10.1] - 2021-03-18
|
18
|
+
### Fixed
|
19
|
+
- Migration steps are now generated in a defined dependency order, so that--for example--indexes that depend
|
20
|
+
on columns are deleted first, before the columns themselves are deleted (since the latter implicitly does the former, which would break the migration when run).
|
21
|
+
- Related to the above, down migration steps are now always generated in exactly the reverse order of the up migration steps.
|
22
|
+
|
23
|
+
## [0.10.0] - 2021-03-17
|
24
|
+
### Deprecated
|
25
|
+
- Deprecated the `fields` DSL method in favor of `declare_schema`.
|
26
|
+
|
27
|
+
### Added
|
28
|
+
- Added the `declare_schema` method to replace `fields`. We now expect a column's type to come before the name
|
29
|
+
i.e. `declare schema { string :title }`. Otherwise, there is no difference between `fields` and `declare_schema`.
|
30
|
+
|
31
|
+
## [0.9.0] - 2021-03-01
|
32
|
+
### Added
|
33
|
+
- Added configurable default settings for `default_text_limit`, `default_string_limit`, `default_null`,
|
34
|
+
`default_generate_foreign_keys` and `default_generate_indexing` to allow developers to adhere to project conventions.
|
35
|
+
|
36
|
+
### Changed
|
37
|
+
- Moved and deprecated default settings for `default_charset` and `default_collation` from
|
38
|
+
`Generators::DeclareSchema::Migration::Migrator` to `::DeclareSchema`
|
39
|
+
|
7
40
|
## [0.8.0] - 2021-02-22
|
8
41
|
### Removed
|
9
42
|
- Removed assumption that primary key is named 'id'.
|
@@ -131,6 +164,10 @@ using the appropriate Rails configuration attributes.
|
|
131
164
|
### Added
|
132
165
|
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
|
133
166
|
|
167
|
+
[0.11.0]: https://github.com/Invoca/declare_schema/compare/v0.10.1...v0.11.0
|
168
|
+
[0.10.1]: https://github.com/Invoca/declare_schema/compare/v0.10.0...v0.10.1
|
169
|
+
[0.10.0]: https://github.com/Invoca/declare_schema/compare/v0.9.0...v0.10.0
|
170
|
+
[0.9.0]: https://github.com/Invoca/declare_schema/compare/v0.8.0...v0.9.0
|
134
171
|
[0.8.0]: https://github.com/Invoca/declare_schema/compare/v0.7.1...v0.8.0
|
135
172
|
[0.7.1]: https://github.com/Invoca/declare_schema/compare/v0.7.0...v0.7.1
|
136
173
|
[0.7.0]: https://github.com/Invoca/declare_schema/compare/v0.6.3...v0.7.0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -57,7 +57,7 @@ during the initialization of your Rails application.
|
|
57
57
|
|
58
58
|
### before_generating_migration callback
|
59
59
|
|
60
|
-
During the
|
60
|
+
During the initialization process for generating migrations, `DeclareSchema` will
|
61
61
|
trigger the `eager_load!` on the `Rails` application and all `Rails::Engine`s loaded
|
62
62
|
into scope. If you need to generate migrations for models that aren't automatically loaded by `eager_load!`,
|
63
63
|
load them in the `before_generating_migration` block.
|
@@ -70,18 +70,85 @@ DeclareSchema::Migration::Migrator.before_generating_migration do
|
|
70
70
|
end
|
71
71
|
```
|
72
72
|
|
73
|
-
|
74
|
-
|
73
|
+
### Global Configuration
|
74
|
+
Configurations can be set at the global level to customize default declaration for the following values:
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
tables and columns in the database. With `declare_schema` this can be configured
|
82
|
-
at three separate levels
|
76
|
+
#### Text Limit
|
77
|
+
The default text limit can be set using the `DeclareSchema.default_text_limit=` method.
|
78
|
+
Note that a `nil` default means that there is no default-- so every declaration must be explicit.
|
79
|
+
This will `raise` a `limit: must be provided for field :text...` error when the default value is `nil` and there is no explicit
|
80
|
+
declaration.
|
83
81
|
|
84
|
-
|
82
|
+
For example, adding the following to your `config/initializers` directory will
|
83
|
+
set the default `text limit` value to `0xffff`:
|
84
|
+
|
85
|
+
**declare_schema.rb**
|
86
|
+
```ruby
|
87
|
+
# frozen_string_literal: true
|
88
|
+
|
89
|
+
DeclareSchema.default_text_limit = 0xffff
|
90
|
+
```
|
91
|
+
|
92
|
+
#### String Limit
|
93
|
+
The default string limit can be set using the `DeclareSchema.default_string_limit=` method.
|
94
|
+
Note that a `nil` default means that there is no default-- so every declaration must be explicit.
|
95
|
+
This will `raise` a `limit: must be provided for field :string...` error when the default value is `nil` and there is no explicit
|
96
|
+
declaration.
|
97
|
+
|
98
|
+
For example, adding the following to your `config/initializers` directory will
|
99
|
+
set the default `string limit` value to `255`:
|
100
|
+
|
101
|
+
**declare_schema.rb**
|
102
|
+
```ruby
|
103
|
+
# frozen_string_literal: true
|
104
|
+
|
105
|
+
DeclareSchema.default_string_limit = 255
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Null
|
109
|
+
The default null value can be set using the `DeclareSchema.default_null=` method.
|
110
|
+
Note that a `nil` default means that there is no default-- so every declaration must be explicit.
|
111
|
+
This will `raise` a `null: must be provided for field...` error when the default value is `nil` and there is no explicit
|
112
|
+
declaration.
|
113
|
+
|
114
|
+
For example, adding the following to your `config/initializers` directory will
|
115
|
+
set the default `null` value to `true`:
|
116
|
+
|
117
|
+
**declare_schema.rb**
|
118
|
+
```ruby
|
119
|
+
# frozen_string_literal: true
|
120
|
+
|
121
|
+
DeclareSchema.default_null = true
|
122
|
+
```
|
123
|
+
|
124
|
+
#### Generate Foreign Keys
|
125
|
+
The default value for generate foreign keys can be set using the `DeclareSchema.default_generate_foreign_keys=` method.
|
126
|
+
This value defaults to `true` and can only be set at the global level.
|
127
|
+
|
128
|
+
For example, adding the following to your `config/initializers` directory will set
|
129
|
+
the default `generate foreign keys` value to `false`:
|
130
|
+
|
131
|
+
**declare_schema.rb**
|
132
|
+
```ruby
|
133
|
+
# frozen_string_literal: true
|
134
|
+
|
135
|
+
DeclareSchema.default_generate_foreign_keys = false
|
136
|
+
```
|
137
|
+
|
138
|
+
#### Generate Indexing
|
139
|
+
The default value for generate indexing can be set using the `DeclareSchema.default_generate_indexing=` method.
|
140
|
+
This value defaults to `true` and can only be set at the global level.
|
141
|
+
|
142
|
+
For example, adding the following to your `config/initializers` directory will
|
143
|
+
set the default `generate indexing` value to `false`:
|
144
|
+
|
145
|
+
**declare_schema.rb**
|
146
|
+
```ruby
|
147
|
+
# frozen_string_literal: true
|
148
|
+
|
149
|
+
DeclareSchema.default_generate_indexing = false
|
150
|
+
```
|
151
|
+
#### Character Set and Collation
|
85
152
|
The character set and collation for all tables and fields can be set at the global level
|
86
153
|
using the `Generators::DeclareSchema::Migrator.default_charset=` and
|
87
154
|
`Generators::DeclareSchema::Migrator.default_collation=` configuration methods.
|
@@ -93,9 +160,30 @@ turn all tables into `utf8mb4` supporting tables:
|
|
93
160
|
```ruby
|
94
161
|
# frozen_string_literal: true
|
95
162
|
|
96
|
-
|
97
|
-
|
163
|
+
DeclareSchema.default_charset = "utf8mb4"
|
164
|
+
DeclareSchema.default_collation = "utf8mb4_bin"
|
98
165
|
```
|
166
|
+
#### db:migrate Command
|
167
|
+
`declare_schema` can run the migration once it is generated, if the `--migrate` option is passed.
|
168
|
+
If not, it will display the command to run later. By default this command is
|
169
|
+
```
|
170
|
+
bundle exec rails db:migrate
|
171
|
+
```
|
172
|
+
If your repo has a different command to run for migrations, you can configure it like this:
|
173
|
+
```ruby
|
174
|
+
`DeclareSchema.db_migrate_command = "bundle exec rails db:migrate_immediate"`
|
175
|
+
```
|
176
|
+
|
177
|
+
## Declaring Character Set and Collation
|
178
|
+
_Note: This feature currently only works for MySQL database configurations._
|
179
|
+
|
180
|
+
MySQL originally supported UTF-8 in the range of 1-3 bytes (`mb3` or "multi-byte 3")
|
181
|
+
which covered the full set of Unicode code points at the time: U+0000 - U+FFFF.
|
182
|
+
But later, Unicode was extended beyond U+FFFF to make room for emojis, and with that
|
183
|
+
UTF-8 require 1-4 bytes (`mb4` or "multi-byte 4"). With this addition, there has
|
184
|
+
come a need to dynamically define the character set and collation for individual
|
185
|
+
tables and columns in the database. With `declare_schema` this can be configured
|
186
|
+
at three separate levels
|
99
187
|
|
100
188
|
### Table Configuration
|
101
189
|
In order to configure a table's default character set and collation, the `charset` and
|
@@ -150,5 +238,5 @@ or add it to your `bundler` Gemfile:
|
|
150
238
|
To run tests:
|
151
239
|
```
|
152
240
|
rake test:prepare_testapp[force]
|
153
|
-
rake test:all
|
241
|
+
rake test:all
|
154
242
|
```
|
data/lib/declare_schema.rb
CHANGED
@@ -21,7 +21,24 @@ module DeclareSchema
|
|
21
21
|
text: String
|
22
22
|
}.freeze
|
23
23
|
|
24
|
+
@default_charset = "utf8mb4"
|
25
|
+
@default_collation = "utf8mb4_bin"
|
26
|
+
@default_text_limit = 0xffff_ffff
|
27
|
+
@default_string_limit = nil
|
28
|
+
@default_null = false
|
29
|
+
@default_generate_foreign_keys = true
|
30
|
+
@default_generate_indexing = true
|
31
|
+
@db_migrate_command =
|
32
|
+
if ActiveSupport::VERSION::MAJOR < 5
|
33
|
+
"bundle exec rake db:migrate"
|
34
|
+
else
|
35
|
+
"bundle exec rails db:migrate"
|
36
|
+
end
|
37
|
+
|
24
38
|
class << self
|
39
|
+
attr_reader :default_charset, :default_collation, :default_text_limit, :default_string_limit, :default_null,
|
40
|
+
:default_generate_foreign_keys, :default_generate_indexing, :db_migrate_command
|
41
|
+
|
25
42
|
def to_class(type)
|
26
43
|
case type
|
27
44
|
when Class
|
@@ -32,6 +49,46 @@ module DeclareSchema
|
|
32
49
|
raise ArgumentError, "expected Class or Symbol or String: got #{type.inspect}"
|
33
50
|
end
|
34
51
|
end
|
52
|
+
|
53
|
+
def default_charset=(charset)
|
54
|
+
charset.is_a?(String) or raise ArgumentError, "charset must be a string (got #{charset.inspect})"
|
55
|
+
@default_charset = charset
|
56
|
+
end
|
57
|
+
|
58
|
+
def default_collation=(collation)
|
59
|
+
collation.is_a?(String) or raise ArgumentError, "collation must be a string (got #{collation.inspect})"
|
60
|
+
@default_collation = collation
|
61
|
+
end
|
62
|
+
|
63
|
+
def default_text_limit=(text_limit)
|
64
|
+
text_limit.nil? or text_limit.is_a?(Integer) or raise ArgumentError, "text limit must be an integer or nil (got #{text_limit.inspect})"
|
65
|
+
@default_text_limit = text_limit
|
66
|
+
end
|
67
|
+
|
68
|
+
def default_string_limit=(string_limit)
|
69
|
+
string_limit.nil? or string_limit.is_a?(Integer) or raise ArgumentError, "string limit must be an integer or nil (got #{string_limit.inspect})"
|
70
|
+
@default_string_limit = string_limit
|
71
|
+
end
|
72
|
+
|
73
|
+
def default_null=(null)
|
74
|
+
null.in?([true, false, nil]) or raise ArgumentError, "null must be either true, false, or nil (got #{null.inspect})"
|
75
|
+
@default_null = null
|
76
|
+
end
|
77
|
+
|
78
|
+
def default_generate_foreign_keys=(generate_foreign_keys)
|
79
|
+
generate_foreign_keys.in?([true, false]) or raise ArgumentError, "generate_foreign_keys must be either true or false (got #{generate_foreign_keys.inspect})"
|
80
|
+
@default_generate_foreign_keys = generate_foreign_keys
|
81
|
+
end
|
82
|
+
|
83
|
+
def default_generate_indexing=(generate_indexing)
|
84
|
+
generate_indexing.in?([true, false]) or raise ArgumentError, "generate_indexing must be either true or false (got #{generate_indexing.inspect})"
|
85
|
+
@default_generate_indexing = generate_indexing
|
86
|
+
end
|
87
|
+
|
88
|
+
def db_migrate_command=(db_migrate_command)
|
89
|
+
db_migrate_command.is_a?(String) or raise ArgumentError, "db_migrate_command must be a string (got #{db_migrate_command.inspect})"
|
90
|
+
@db_migrate_command = db_migrate_command
|
91
|
+
end
|
35
92
|
end
|
36
93
|
end
|
37
94
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/proxy_object'
|
4
|
+
|
5
|
+
module DeclareSchema
|
6
|
+
class Dsl < BasicObject # avoid Object because that gets extended by lots of gems
|
7
|
+
include ::Kernel # but we need the basic class methods
|
8
|
+
|
9
|
+
instance_methods.each do |m|
|
10
|
+
unless m.to_s.starts_with?('__') || m.in?([:object_id, :instance_eval])
|
11
|
+
undef_method(m)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(model, options = {})
|
16
|
+
@model = model
|
17
|
+
@options = options
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :model
|
21
|
+
|
22
|
+
def timestamps
|
23
|
+
field(:created_at, :datetime, null: true)
|
24
|
+
field(:updated_at, :datetime, null: true)
|
25
|
+
end
|
26
|
+
|
27
|
+
def optimistic_lock
|
28
|
+
field(:lock_version, :integer, default: 1, null: false)
|
29
|
+
end
|
30
|
+
|
31
|
+
def field(name, type, *args, **options)
|
32
|
+
@model.declare_field(name, type, *(args + [@options.merge(options)]))
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(type, name, *args)
|
36
|
+
field(name, type, *args)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,21 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'active_record'
|
4
|
+
require 'declare_schema/dsl'
|
4
5
|
require 'declare_schema/model'
|
5
6
|
require 'declare_schema/field_declaration_dsl'
|
6
7
|
|
7
8
|
module DeclareSchema
|
8
|
-
module
|
9
|
+
module Macros
|
9
10
|
def fields(table_options = {}, &block)
|
10
11
|
# Any model that calls 'fields' gets DeclareSchema::Model behavior
|
11
12
|
DeclareSchema::Model.mix_in(self)
|
12
13
|
|
13
|
-
# @include_in_migration = false #||= options.fetch(:include_in_migration, true); options.delete(:include_in_migration)
|
14
14
|
@include_in_migration = true
|
15
15
|
@table_options = table_options
|
16
16
|
|
17
17
|
if block
|
18
|
-
dsl = DeclareSchema::FieldDeclarationDsl.new(self
|
18
|
+
dsl = DeclareSchema::FieldDeclarationDsl.new(self)
|
19
|
+
if block.arity == 1
|
20
|
+
yield dsl
|
21
|
+
else
|
22
|
+
dsl.instance_eval(&block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
deprecate :fields, deprecator: ActiveSupport::Deprecation.new('1.0', 'DeclareSchema')
|
27
|
+
|
28
|
+
def declare_schema(table_options = {}, &block)
|
29
|
+
# Any model that calls 'fields' gets DeclareSchema::Model behavior
|
30
|
+
DeclareSchema::Model.mix_in(self)
|
31
|
+
|
32
|
+
# @include_in_migration = false #||= options.fetch(:include_in_migration, true); options.delete(:include_in_migration)
|
33
|
+
@include_in_migration = true # TODO: Add back or delete the include_in_migration feature
|
34
|
+
@table_options = table_options
|
35
|
+
|
36
|
+
if block
|
37
|
+
dsl = DeclareSchema::Dsl.new(self, null: false)
|
19
38
|
if block.arity == 1
|
20
39
|
yield dsl
|
21
40
|
else
|
@@ -26,4 +45,4 @@ module DeclareSchema
|
|
26
45
|
end
|
27
46
|
end
|
28
47
|
|
29
|
-
ActiveRecord::Base.singleton_class.prepend DeclareSchema::
|
48
|
+
ActiveRecord::Base.singleton_class.prepend DeclareSchema::Macros
|
data/lib/declare_schema/model.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'rails'
|
4
|
-
|
5
3
|
require 'declare_schema/extensions/module'
|
6
4
|
|
7
5
|
module DeclareSchema
|
@@ -41,7 +39,7 @@ module DeclareSchema
|
|
41
39
|
eval <<~EOS
|
42
40
|
def self.inherited(klass)
|
43
41
|
unless klass.field_specs.has_key?(inheritance_column)
|
44
|
-
|
42
|
+
declare_schema do |f|
|
45
43
|
f.field(inheritance_column, :string, limit: 255, null: true)
|
46
44
|
end
|
47
45
|
index(inheritance_column)
|
@@ -130,7 +128,7 @@ module DeclareSchema
|
|
130
128
|
|
131
129
|
fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
|
132
130
|
|
133
|
-
if
|
131
|
+
if ActiveSupport::VERSION::MAJOR >= 5
|
134
132
|
super
|
135
133
|
else
|
136
134
|
super(name, scope, options.except(:optional))
|
@@ -149,7 +147,7 @@ module DeclareSchema
|
|
149
147
|
end
|
150
148
|
end
|
151
149
|
|
152
|
-
if ::
|
150
|
+
if ::ActiveSupport::VERSION::MAJOR < 5
|
153
151
|
def primary_key
|
154
152
|
super || 'id'
|
155
153
|
end
|
@@ -227,7 +225,7 @@ module DeclareSchema
|
|
227
225
|
ActiveRecord::Coders::JSON
|
228
226
|
elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
|
229
227
|
class_name_or_coder
|
230
|
-
elsif
|
228
|
+
elsif ActiveSupport::VERSION::MAJOR >= 5
|
231
229
|
ActiveRecord::Coders::YAMLColumn.new(attr_name, class_name_or_coder)
|
232
230
|
else
|
233
231
|
ActiveRecord::Coders::YAMLColumn.new(class_name_or_coder)
|
@@ -53,7 +53,7 @@ module DeclareSchema
|
|
53
53
|
def deserialize_default_value(column, type, default_value)
|
54
54
|
type or raise ArgumentError, "must pass type; got #{type.inspect}"
|
55
55
|
|
56
|
-
case
|
56
|
+
case ActiveSupport::VERSION::MAJOR
|
57
57
|
when 4
|
58
58
|
# TODO: Delete this Rails 4 support ASAP! This could be wrong, since it's using the type of the old column...which
|
59
59
|
# might be getting migrated to a new type. We should be using just type as below. -Colin
|
@@ -58,18 +58,21 @@ module DeclareSchema
|
|
58
58
|
@position = position
|
59
59
|
@options = options.dup
|
60
60
|
|
61
|
-
@options.has_key?(:null) or @options[:null] =
|
61
|
+
@options.has_key?(:null) or @options[:null] = ::DeclareSchema.default_null
|
62
|
+
@options[:null].nil? and raise "null: must be provided for field #{model}##{@name}: #{@options.inspect} since ::DeclareSchema#default_null is set to 'nil'; do you want `null: false`?"
|
62
63
|
|
63
64
|
case @type
|
64
65
|
when :text
|
65
66
|
if self.class.mysql_text_limits?
|
66
67
|
@options[:default].nil? or raise MysqlTextMayNotHaveDefault, "when using MySQL, non-nil default may not be given for :text field #{model}##{@name}"
|
67
|
-
@options[:limit]
|
68
|
+
@options[:limit] ||= ::DeclareSchema.default_text_limit or
|
69
|
+
raise("limit: must be provided for :text field #{model}##{@name}: #{@options.inspect} since ::DeclareSchema#default_text_limit is set to 'nil'; do you want `limit: 0xffff_ffff`?")
|
70
|
+
@options[:limit] = self.class.round_up_mysql_text_limit(@options[:limit])
|
68
71
|
else
|
69
72
|
@options.delete(:limit)
|
70
73
|
end
|
71
74
|
when :string
|
72
|
-
@options[:limit] or raise "limit: must be
|
75
|
+
@options[:limit] ||= ::DeclareSchema.default_string_limit or raise "limit: must be provided for :string field #{model}##{@name}: #{@options.inspect} since ::DeclareSchema#default_string_limit is set to 'nil'; do you want `limit: 255`?"
|
73
76
|
when :bigint
|
74
77
|
@type = :integer
|
75
78
|
@options[:limit] = 8
|
@@ -96,15 +99,15 @@ module DeclareSchema
|
|
96
99
|
|
97
100
|
if @type.in?([:text, :string])
|
98
101
|
if ActiveRecord::Base.connection.class.name.match?(/mysql/i)
|
99
|
-
@options[:charset] ||= model.table_options[:charset] ||
|
100
|
-
@options[:collation] ||= model.table_options[:collation] ||
|
102
|
+
@options[:charset] ||= model.table_options[:charset] || ::DeclareSchema.default_charset
|
103
|
+
@options[:collation] ||= model.table_options[:collation] || ::DeclareSchema.default_collation
|
101
104
|
else
|
102
105
|
@options.delete(:charset)
|
103
106
|
@options.delete(:collation)
|
104
107
|
end
|
105
108
|
else
|
106
109
|
@options[:charset] and warn("charset may only given for :string and :text fields for SQL type #{@type} in field #{model}##{@name}")
|
107
|
-
@options[:collation] and
|
110
|
+
@options[:collation] and warn("collation may only given for :string and :text fields for SQL type #{@type} in field #{model}##{@name}")
|
108
111
|
end
|
109
112
|
|
110
113
|
@options = Hash[@options.sort_by { |k, _v| OPTION_INDEXES[k] || 9999 }]
|