db_schema 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +62 -15
- data/lib/db_schema/definitions.rb +1 -1
- data/lib/db_schema/dsl.rb +19 -11
- data/lib/db_schema/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8d57c1174558070a12ed7b3f5469b1bdaeb019d
|
4
|
+
data.tar.gz: d389fbca9c57a1a5cbf7356b8cc71c7c4db5423a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7ddcffe01abc26f6ee010e7ed161c53ce60d13e29e2e03be015750d8ba37329e1cbe683d446452f585621d31d55d89ddd9506d44d1ba9bd487d644b2fff6130
|
7
|
+
data.tar.gz: cea37fc0fe70f6e70d4118169677137e4b4c4bda5faed03b26f54484972444cf48113238c3551cb680a7aa23f0e824046ae9b79e7e26305e0090f54810509e34
|
data/README.md
CHANGED
@@ -53,7 +53,7 @@ But you would lose it even with manual migrations.
|
|
53
53
|
Add this line to your application's Gemfile:
|
54
54
|
|
55
55
|
``` ruby
|
56
|
-
gem 'db_schema', '~> 0.2.
|
56
|
+
gem 'db_schema', '~> 0.2.2'
|
57
57
|
```
|
58
58
|
|
59
59
|
And then execute:
|
@@ -78,12 +78,10 @@ DbSchema DSL looks like this:
|
|
78
78
|
DbSchema.describe do |db|
|
79
79
|
db.table :users do |t|
|
80
80
|
t.primary_key :id
|
81
|
-
t.varchar :email, null: false
|
81
|
+
t.varchar :email, null: false, unique: true
|
82
82
|
t.varchar :password_digest, length: 40
|
83
83
|
t.timestamptz :created_at
|
84
84
|
t.timestamptz :updated_at
|
85
|
-
|
86
|
-
t.index :email, unique: true
|
87
85
|
end
|
88
86
|
end
|
89
87
|
```
|
@@ -91,9 +89,18 @@ end
|
|
91
89
|
Before DbSchema connects to the database you need to configure it:
|
92
90
|
|
93
91
|
``` ruby
|
94
|
-
DbSchema.configure(
|
92
|
+
DbSchema.configure(
|
93
|
+
adapter: 'postgresql',
|
94
|
+
database: 'my_database',
|
95
|
+
user: 'bob',
|
96
|
+
password: 'secret'
|
97
|
+
)
|
98
|
+
|
95
99
|
# or in Rails
|
96
|
-
DbSchema.configure_from_yaml(
|
100
|
+
DbSchema.configure_from_yaml(
|
101
|
+
Rails.root.join('config', 'database.yml'),
|
102
|
+
Rails.env
|
103
|
+
)
|
97
104
|
```
|
98
105
|
|
99
106
|
Then you can load your schema definition (it is executable - it instantly applies itself to your database):
|
@@ -110,7 +117,10 @@ Here's an initializer example for a Rails app:
|
|
110
117
|
|
111
118
|
``` ruby
|
112
119
|
# config/initializers/db_schema.rb
|
113
|
-
DbSchema.configure_from_yaml(
|
120
|
+
DbSchema.configure_from_yaml(
|
121
|
+
Rails.root.join('config', 'database.yml'),
|
122
|
+
Rails.env
|
123
|
+
)
|
114
124
|
|
115
125
|
if Rails.env.development? || Rails.env.test?
|
116
126
|
load Rails.root.join('db', 'schema.rb')
|
@@ -133,6 +143,8 @@ end
|
|
133
143
|
|
134
144
|
Then you just call `rake db:schema:apply` from your deploy script before restarting the app.
|
135
145
|
|
146
|
+
If your production setup doesn't include multiple workers starting simultaneously (for example if you run one Puma worker per docker container and restart containers one by one on deploy) you can go the simple way and just `load Rails.root.join('db', 'schema.rb')` in any environment without a separate rake task. The first DbSchema run will apply the schema while the subsequent ones will see there's nothing left to do.
|
147
|
+
|
136
148
|
## DSL
|
137
149
|
|
138
150
|
Database schema is defined with a block passed to `DbSchema.describe` method.
|
@@ -146,27 +158,22 @@ DbSchema.describe do |db|
|
|
146
158
|
|
147
159
|
db.table :users do |t|
|
148
160
|
t.primary_key :id
|
149
|
-
t.varchar :email, null: false
|
161
|
+
t.varchar :email, null: false, unique: true
|
150
162
|
t.varchar :password, null: false
|
151
163
|
t.varchar :name, null: false
|
152
164
|
t.integer :age
|
153
165
|
t.user_status :status, null: false, default: 'registered'
|
154
166
|
t.hstore :tracking, null: false, default: ''
|
155
|
-
|
156
|
-
t.index :email, unique: true
|
157
167
|
end
|
158
168
|
|
159
169
|
db.enum :user_status, [:registered, :confirmed_email, :subscriber]
|
160
170
|
|
161
171
|
db.table :posts do |t|
|
162
172
|
t.primary_key :id
|
163
|
-
t.integer :user_id, null: false
|
173
|
+
t.integer :user_id, null: false, index: true, references: :users
|
164
174
|
t.varchar :title, null: false, length: 50
|
165
175
|
t.text :content
|
166
176
|
t.array :tags, of: :varchar
|
167
|
-
|
168
|
-
t.index :user_id
|
169
|
-
t.foreign_key :user_id, references: :users
|
170
177
|
end
|
171
178
|
end
|
172
179
|
```
|
@@ -304,7 +311,28 @@ Unique indexes are created with `unique: true`:
|
|
304
311
|
t.index :email, unique: true
|
305
312
|
```
|
306
313
|
|
307
|
-
|
314
|
+
Simple one-field indexes can be created with `index: true` and `unique: true` options passed to the field definition method so
|
315
|
+
|
316
|
+
``` ruby
|
317
|
+
db.table :users do |t|
|
318
|
+
t.varchar :name, index: true
|
319
|
+
t.varchar :email, unique: true
|
320
|
+
end
|
321
|
+
```
|
322
|
+
|
323
|
+
is essentially the same as
|
324
|
+
|
325
|
+
``` ruby
|
326
|
+
db.table :users do |t|
|
327
|
+
t.varchar :name
|
328
|
+
t.varchar :email
|
329
|
+
|
330
|
+
t.index :name
|
331
|
+
t.index :email, unique: true
|
332
|
+
end
|
333
|
+
```
|
334
|
+
|
335
|
+
Passing several field names to `#index` makes a multiple index:
|
308
336
|
|
309
337
|
``` ruby
|
310
338
|
db.table :users do |t|
|
@@ -397,6 +425,15 @@ db.table :posts do |t|
|
|
397
425
|
end
|
398
426
|
```
|
399
427
|
|
428
|
+
DbSchema also provides a short syntax for simple one-column foreign keys - just pass the `:references` option to the field definition:
|
429
|
+
|
430
|
+
``` ruby
|
431
|
+
db.table :posts do |t|
|
432
|
+
t.integer :user_id, references: :users
|
433
|
+
t.varchar :username, references: [:users, :name]
|
434
|
+
end
|
435
|
+
```
|
436
|
+
|
400
437
|
As with indexes, you can pass your custom name in the `:name` option; default foreign key name looks like `"#{table_name}_#{fkey_fields.first}_fkey"`.
|
401
438
|
|
402
439
|
You can also define a composite foreign key consisting of (and referencing) multiple columns; just list them all:
|
@@ -435,6 +472,16 @@ db.table :users do |t|
|
|
435
472
|
end
|
436
473
|
```
|
437
474
|
|
475
|
+
As with indexes and foreign keys, DbSchema has a short syntax for simple check constraints - a `:check` option in the method definition:
|
476
|
+
|
477
|
+
``` ruby
|
478
|
+
db.table :products do |t|
|
479
|
+
t.primary_key :id
|
480
|
+
t.text :name, null: false
|
481
|
+
t.numeric :price, check: 'price > 0'
|
482
|
+
end
|
483
|
+
```
|
484
|
+
|
438
485
|
### Enum types
|
439
486
|
|
440
487
|
PostgreSQL allows developers to create custom enum types; value of enum type is one of a fixed set of values stored in the type definition.
|
data/lib/db_schema/dsl.rb
CHANGED
@@ -47,12 +47,12 @@ module DbSchema
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
def
|
51
|
-
|
50
|
+
def method_missing(method_name, name, *args, &block)
|
51
|
+
field(name, method_name, args.first || {})
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
55
|
-
|
54
|
+
def primary_key(name)
|
55
|
+
field(name, :integer, primary_key: true)
|
56
56
|
end
|
57
57
|
|
58
58
|
def index(*columns, name: nil, unique: false, using: :btree, where: nil, **ordered_fields)
|
@@ -132,14 +132,22 @@ module DbSchema
|
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
|
-
def
|
136
|
-
|
135
|
+
def field(name, type, custom: false, unique: false, index: false, references: nil, check: nil, **options)
|
136
|
+
fields << Definitions::Field.build(name, type, options)
|
137
137
|
|
138
|
-
|
139
|
-
name,
|
140
|
-
|
141
|
-
|
142
|
-
|
138
|
+
if unique
|
139
|
+
index(name, unique: true)
|
140
|
+
elsif index
|
141
|
+
index(name)
|
142
|
+
end
|
143
|
+
|
144
|
+
if references
|
145
|
+
foreign_key(name, references: references)
|
146
|
+
end
|
147
|
+
|
148
|
+
if check
|
149
|
+
check("#{table_name}_#{name}_check", check)
|
150
|
+
end
|
143
151
|
end
|
144
152
|
|
145
153
|
def fields
|
data/lib/db_schema/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vsevolod Romashov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|