You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* TECH-13204: update README
* TECH-13204: update CHANGELOG
* TECH-13204: add length: support to SchemaChange::IndexAdd
* TECH-13204: deprecate belongs_to index: 'name' and unique: true|false
* TECH-13204: add index support for length; add options; refactor to_key and settings
* TECH-13204: add spec for with_name and drop unused index_name
* TECH-13204: fix bug where indexes on the same set of fields would not be allowed (now they must be equivalent)
* TECH-13204: [] -> Set so that idempotence is built in
* TECH-13204: include .to_a to convert Sets to Arrays
* TECH-13204: raise ArgumentError if index: false contradicts other options
* TECH-13204: update comment to drop mention of prefix (partial) indexes not being supported
* TECH-13204: make #hash public
* TECH-13204: test that parent class is lazy required
* TECH-13204: unabbreviate fkey and refl
* TECH-13204: treat belongs_to index: :name same as "name"
* TECH-13204: parse lengths in for_model
* TECH-13204: fix code to be confident that parent_table_name will be present
* TECH-13204: add utf8mb3 -> utf8mb3_general_ci inference
* TECH-13204: use reflection.class_name so class can be lazy-evaluated
* TECH-13204: plumb connection from model into HabtmModelShim
* TECH-13204: add IndexDefinition.normalize_index_length; use it to normalize user and database config before comparing
* TECH-13204: drop connection: from HabtmModelShim.from_reflection; use klass instead of our own constantize
* TECH-13204: add contract checks for ColumnAdd table_name and column_name
* TECH-13204: exclude Array _declared_primary_key from consideration as a column
* TECH-13204: use ActiveSupport::Deprecation for all warnings; include name.inspect
* TECH-13204: add warnings about unnecessary null: and optional: combinations; qualify with [declare_schema] prefix
* TECH-13204: document belongs_to args and options and which pass through
* TECH-13204: release v1.4.0
The `declare_schema` DSL is yielded to the block as shown with block variable `t` (for table).
65
+
Each field (column) is declared with the syntax `t.<type> :<column_name>, <options>` as shown here for the `string` column `company_name`:
66
+
```ruby
67
+
create_table :companies, id::bigintdo |t|
68
+
t.string :company_name, null:false, limit:100
69
+
...
70
+
end
71
+
```
72
+
### Field (Column) Types
73
+
All of the ActiveRecord field types are supported, as returned by the database driver in use at the time.
74
+
These typically include:
75
+
-`binary` (blob)
76
+
-`text`
77
+
-`integer`
78
+
-`bigint`
79
+
-`float`
80
+
-`decimal`
81
+
-`date`
82
+
-`time`
83
+
-`datetime`
84
+
-`timestamp`
85
+
-`string` (varchar)
86
+
-`boolean` (tinyint 0 or 1)
87
+
-`json`
88
+
-`array`
89
+
-`enum` (if using the `activerecord-mysql-enum` gem) (MySQL enum)
90
+
91
+
### Field (Column) Options
92
+
The following field options are:
93
+
-`limit` (integer) - The maximum length of the field. For `text` and `binary` fields, this is the maximum number of bytes.
94
+
For `string` fields, this is the maximum number of characters, and defaults to `DeclareSchema.default_string_limit`; for `text`, defaults to `DeclareSchema.default_text_limit`.
95
+
For `enum`
96
+
-`null` (boolean) - Whether the field is nullable. Defaults to `DeclareSchema.default_null`.
97
+
-`default` (any) - The default value for the field.
98
+
-`ruby_default` (Proc) - A callable Ruby Proc that returns the default value for the field. This is useful for default values that require Ruby computation.
99
+
(Provided by the `attr_default` gem.)
100
+
-`index` (boolean [deprecated] or hash) - Whether to create an index for the field. If `true`, defaults to `{ unique: false }`[deprecated]. See below for supported `index` options.
101
+
-`unique`[deprecated] (boolean) - Whether to create a unique index for the field. Defaults to `false`. Deprecated in favor of `index: { unique: <boolean> }`.
102
+
-`charset` (string) - The character set for the field. Defaults to `default_charset` (see below).
103
+
-`collation` (string) - The collation for the field. Defaults to `default_collation` (see below).
104
+
-`precision` (integer) - The precision for the numeric field.
105
+
-`scale` (integer) - The scale for the numeric field.
106
+
107
+
### Index Options
108
+
The following `index` options are supported:
109
+
-`name` (string) - The name of the index. Defaults the longest format that will fit within `DeclareSchema.max_index_and_constraint_name_length`. They are tried in this order:
110
+
1.`index_<table>_on_<col1>[_and_<col2>...]>`.
111
+
2.`__<col1>[_<col2>...]>`
112
+
3.`<table_prefix><sha256_of_columns_prefix>`
113
+
-`unique` (boolean) - Whether the index is unique. Defaults to `false`.
114
+
-`order` (synbol or hash) - The index order. If `:asc` or `:desc` is provided, it is used as the order for all columns. If hash is provided, it is used to specify the order of individual columns, where the column names are given as `Symbol` hash keys with values of `:asc` or `:desc` indicating the sort order of that column.
115
+
-`length` (integer or hash) - The partial index length(s). If an integer is provided, it is used as the length for all columns. If a hash is provided, it is used to specify the length for individual columns, where the column names are given as `Symbol` hash keys.
116
+
-`where` (string) - The subset index predicate.
117
+
63
118
## Usage without Rails
64
119
65
120
When using `DeclareSchema` without Rails, you can use the `declare_schema/rake` task to generate the migration file.
@@ -115,13 +170,13 @@ end
115
170
```
116
171
117
172
### clear_default_schema
118
-
This method clears out any previously declared `default_schema`.
173
+
This method clears out any previously declared `default_schema`. This can be useful for tests.
119
174
```ruby
120
175
DeclareSchema.clear_default_schema
121
176
```
122
177
123
178
### Global Configuration
124
-
Configurations can be set at the global level to customize default declaration for the following values:
179
+
Configurations can be set globally to customize default declaration for the following values:
125
180
126
181
#### Text Limit
127
182
The default text limit can be set using the `DeclareSchema.default_text_limit=` method.
@@ -134,8 +189,6 @@ set the default `text limit` value to `0xffff`:
134
189
135
190
**declare_schema.rb**
136
191
```ruby
137
-
# frozen_string_literal: true
138
-
139
192
DeclareSchema.default_text_limit =0xffff
140
193
```
141
194
@@ -150,8 +203,6 @@ set the default `string limit` value to `255`:
150
203
151
204
**declare_schema.rb**
152
205
```ruby
153
-
# frozen_string_literal: true
154
-
155
206
DeclareSchema.default_string_limit =255
156
207
```
157
208
@@ -166,40 +217,34 @@ set the default `null` value to `true`:
166
217
167
218
**declare_schema.rb**
168
219
```ruby
169
-
# frozen_string_literal: true
170
-
171
220
DeclareSchema.default_null =true
172
221
```
173
222
174
223
#### Generate Foreign Keys
175
-
The default value for generate foreign keys can be set using the `DeclareSchema.default_generate_foreign_keys=` method.
176
-
This value defaults to `true` and can only be set at the global level.
224
+
You can choose whether to generate foreign keys by using the `DeclareSchema.default_generate_foreign_keys=` method.
225
+
This defaults to `true` and can only be set globally.
177
226
178
-
For example, adding the following to your `config/initializers` directory will set
179
-
the default `generate foreign keys` value to `false`:
227
+
For example, adding the following to your `config/initializers` directory will cause
0 commit comments