pg_saurus 2.6.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +141 -98
- data/lib/core_ext/active_record/connection_adapters/postgresql_adapter.rb +4 -2
- data/lib/pg_saurus/connection_adapters/abstract_adapter.rb +0 -2
- data/lib/pg_saurus/connection_adapters/postgresql_adapter/foreign_key_methods.rb +105 -0
- data/lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb +8 -0
- data/lib/pg_saurus/connection_adapters/postgresql_adapter.rb +14 -2
- data/lib/pg_saurus/connection_adapters/table/comment_methods.rb +6 -6
- data/lib/pg_saurus/connection_adapters/table/trigger_methods.rb +2 -2
- data/lib/pg_saurus/connection_adapters/table.rb +0 -6
- data/lib/pg_saurus/connection_adapters.rb +0 -1
- data/lib/pg_saurus/create_index_concurrently.rb +1 -46
- data/lib/pg_saurus/migration/command_recorder.rb +0 -2
- data/lib/pg_saurus/schema_dumper/foreign_key_methods.rb +49 -0
- data/lib/pg_saurus/schema_dumper.rb +4 -3
- data/lib/pg_saurus/version.rb +1 -1
- metadata +10 -78
- data/lib/pg_saurus/connection_adapters/abstract_adapter/foreigner_methods.rb +0 -67
- data/lib/pg_saurus/connection_adapters/postgresql_adapter/foreigner_methods.rb +0 -221
- data/lib/pg_saurus/connection_adapters/table/foreigner_methods.rb +0 -51
- data/lib/pg_saurus/migration/command_recorder/foreigner_methods.rb +0 -31
- data/lib/pg_saurus/schema_dumper/foreigner_methods.rb +0 -63
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dabef7560c6d27d491206e5ba2f067d23d05c8bd
|
4
|
+
data.tar.gz: 8199a937694563d6a5fd411bff53485720280419
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dadad30b8423ca3ac3ac500542e09d3cfceef0e37d77e7d5747e46f193c157cd7340573d4d64c3002e2d24ba3b9b52f859fbe7f0072b04d8dfc5eac083bdae2
|
7
|
+
data.tar.gz: d9906693fe2dc28b459dc18fa263714343355c252eb53c9c63264f8cb077c76c41106c79c1b29e9acdd92adf403cc7e2c599befc44c53380b418f3ab09bbbb5e
|
data/README.markdown
CHANGED
@@ -1,27 +1,43 @@
|
|
1
1
|
# PgSaurus
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/HornsAndHooves/pg_saurus.png)](http://travis-ci.org/HornsAndHooves/pg_saurus)
|
4
|
-
[![Dependency Status](https://gemnasium.com/HornsAndHooves/pg_saurus.png)](https://gemnasium.com/HornsAndHooves/pg_saurus)
|
5
4
|
[![Code Climate](https://codeclimate.com/github/HornsAndHooves/pg_saurus/badges/gpa.svg)](https://codeclimate.com/github/HornsAndHooves/pg_saurus)
|
6
|
-
ActiveRecord extension to get more from PostgreSQL:
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
*
|
11
|
-
* Use
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*
|
6
|
+
An ActiveRecord extension to get more from PostgreSQL:
|
7
|
+
|
8
|
+
* Create/drop [schemas](#schemas).
|
9
|
+
* Use existing functionality in the context of [schemas](#schemas).
|
10
|
+
* Set/remove [comments on columns and tables](#table-and-column-comments).
|
11
|
+
* [Enhancements to the Rails 4.2 foreign key support](#foreign-keys).
|
12
|
+
* Use [partial indexes](#partial-indexes).
|
13
|
+
* Use [indexes on expressions](#indexes-on-expressions).
|
14
|
+
* [Run index creation concurrently](#concurrent-index-creation).
|
15
|
+
* Create/drop [views](#views).
|
16
|
+
* Create/drop [functions](#functions).
|
17
|
+
* Create/drop [triggers](#triggers).
|
18
|
+
* [Load/unload postgres extensions](#loadingunloading-postgresql-extension-modules).
|
19
|
+
* [Select a specific DB role to conduct a migration](#roles).
|
20
|
+
* Use yet more [tools](#tools).
|
15
21
|
|
16
22
|
PgSaurus is a fork of PgPower.
|
17
23
|
|
24
|
+
### More information
|
25
|
+
|
26
|
+
* [Environment notes](#environment-notes)
|
27
|
+
* [Running tests](#running-tests)
|
28
|
+
* [TODO](#todo)
|
29
|
+
* [Credits](#credits)
|
30
|
+
* [Copyright and License](#copyright-and-license)
|
31
|
+
* [Contributing](#contributing)
|
32
|
+
|
18
33
|
## Environment notes
|
19
34
|
|
20
|
-
PgSaurus was tested with Rails 4.
|
35
|
+
PgSaurus v3 was tested with Rails 4.2, Ruby 2.2.4. For Rails 4.1, use PgSaurus v2.5+.
|
36
|
+
|
37
|
+
NOTE: JRuby is not supported. The current ActiveRecord JDBC adapter has its own Rails4-compatible
|
38
|
+
method named "create_schema" which conflicts with this gem.
|
21
39
|
|
22
|
-
NOTE:
|
23
|
-
adapter has its own Rails4-compatible method named "create_schema" which
|
24
|
-
conflicts with this gem.
|
40
|
+
NOTE: PgSaurus does not support Rails 3.
|
25
41
|
|
26
42
|
## Schemas
|
27
43
|
|
@@ -45,7 +61,7 @@ end
|
|
45
61
|
Use schema `:schema` option to specify schema name:
|
46
62
|
|
47
63
|
```ruby
|
48
|
-
create_table "countries", :
|
64
|
+
create_table "countries", schema: "demography" do |t|
|
49
65
|
# columns goes here
|
50
66
|
end
|
51
67
|
```
|
@@ -93,15 +109,15 @@ remove_column_comment :phone_numbers, :npa
|
|
93
109
|
Set comments on multiple columns in the table.
|
94
110
|
|
95
111
|
```ruby
|
96
|
-
set_column_comments :phone_numbers, :
|
97
|
-
:
|
112
|
+
set_column_comments :phone_numbers, npa: 'Numbering Plan Area Code - Allowed ranges: [2-9] for first digit, [0-9] for second and third digit.',
|
113
|
+
nxx: 'Central Office Number'
|
98
114
|
```
|
99
115
|
Remove comments from multiple columns in the table.
|
100
116
|
|
101
117
|
```ruby
|
102
118
|
remove_column_comments :phone_numbers, :npa, :nxx
|
103
119
|
```
|
104
|
-
PgSaurus also adds extra methods to change_table.
|
120
|
+
PgSaurus also adds extra table methods to the `change_table` block.
|
105
121
|
|
106
122
|
Set comments:
|
107
123
|
|
@@ -112,8 +128,8 @@ change_table :phone_numbers do |t|
|
|
112
128
|
end
|
113
129
|
|
114
130
|
change_table :phone_numbers do |t|
|
115
|
-
t.set_column_comments :
|
116
|
-
:
|
131
|
+
t.set_column_comments npa: 'Numbering Plan Area Code - Allowed ranges: [2-9] for first digit, [0-9] for second and third digit.',
|
132
|
+
nxx: 'Central Office Number'
|
117
133
|
end
|
118
134
|
```
|
119
135
|
Remove comments:
|
@@ -130,46 +146,63 @@ end
|
|
130
146
|
```
|
131
147
|
## Foreign keys
|
132
148
|
|
133
|
-
|
134
|
-
gem and patched it to be schema-aware. Support was also added for index auto-generation.
|
135
|
-
|
136
|
-
You should disable `foreigner` in your Gemfile if you want to use `pg_saurus`.
|
137
|
-
|
138
|
-
If you do not want to generate an index, pass the :exclude_index => true option.
|
149
|
+
PgSaurus v3 augments Rails 4.2's foreign key methods with:
|
139
150
|
|
140
|
-
|
151
|
+
* schema support
|
152
|
+
* index auto-generation
|
141
153
|
|
154
|
+
When you create a foreign key PgSaurus automatically creates an index.
|
155
|
+
If you do not want to generate an index, pass the `exclude_index: true` option.
|
156
|
+
The syntax is compatible with Rails 4.2's foreign key handling methods.
|
142
157
|
|
143
|
-
|
158
|
+
It works with schemas as expected:
|
144
159
|
|
145
160
|
```ruby
|
146
|
-
add_foreign_key(
|
161
|
+
add_foreign_key('blog.comments', 'blog.posts')
|
147
162
|
```
|
148
|
-
|
163
|
+
Adds the index `'index_comments_on_post_id'`:
|
149
164
|
|
150
165
|
```ruby
|
151
|
-
add_foreign_key(:comments, :posts
|
166
|
+
add_foreign_key(:comments, :posts)
|
152
167
|
```
|
153
|
-
|
168
|
+
Does not add an index:
|
154
169
|
|
155
170
|
```ruby
|
156
|
-
add_foreign_key(:comments, :posts, :
|
171
|
+
add_foreign_key(:comments, :posts, exclude_index: true)
|
157
172
|
```
|
158
|
-
|
173
|
+
|
174
|
+
Note that removing a foreign key does not drop the index of the foreign key column.
|
175
|
+
If you want to remove the index, pass in the `remove_index: true` option.
|
159
176
|
|
160
177
|
```ruby
|
161
|
-
|
178
|
+
remove_foreign_key(:comments, column: :post_id, remove_index: true)
|
162
179
|
```
|
163
|
-
|
180
|
+
|
181
|
+
|
182
|
+
### Migration notes - upgrading from Rails 4.1
|
183
|
+
|
184
|
+
PgSaurus v3+ now uses the Rails 4.2 semantics for `add_foreign_key` and `remove_foreign_key`. See http://api.rubyonrails.org/v4.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html
|
185
|
+
|
186
|
+
A few things have changed. The most breaking change is that the syntax `remove_foreign_key :from_table, :to_table, options` no longer works.
|
164
187
|
|
165
188
|
```ruby
|
166
|
-
|
189
|
+
#THIS FAILS
|
190
|
+
remove_foreign_key :comments, :posts, remove_index: true
|
191
|
+
|
192
|
+
#DO THIS INSTEAD
|
193
|
+
remove_foreign_key :comments, column: :post_id, remove_index: true
|
167
194
|
```
|
168
|
-
|
195
|
+
|
196
|
+
For adding foreign keys, the `:dependent` option is replaced with `:on_delete` and `:on_update`. The `:delete` value is replaced with `:cascade`.
|
169
197
|
|
170
198
|
```ruby
|
171
|
-
|
199
|
+
#OLD STYLE - NO LONGER WORKS
|
200
|
+
add_foreign_key :comments, :posts, dependent: :delete
|
201
|
+
|
202
|
+
#NEW STYLE - DO THIS INSTEAD
|
203
|
+
add_foreign_key :comments, :posts, on_delete: :cascade
|
172
204
|
```
|
205
|
+
|
173
206
|
## Partial Indexes
|
174
207
|
|
175
208
|
Rails 4.x [pull request](https://github.com/rails/rails/pull/4956) was used as a
|
@@ -177,15 +210,15 @@ starting point to patch it to be schema-aware.
|
|
177
210
|
|
178
211
|
### Examples
|
179
212
|
|
180
|
-
Add a partial index to a table
|
213
|
+
Add a partial index to a table:
|
181
214
|
|
182
215
|
```ruby
|
183
|
-
add_index(:comments, [:country_id, :user_id], :
|
216
|
+
add_index(:comments, [:country_id, :user_id], where: 'active')
|
184
217
|
```
|
185
|
-
Add a partial index to a schema table
|
218
|
+
Add a partial index to a schema table:
|
186
219
|
|
187
220
|
```ruby
|
188
|
-
add_index('blog.comments', :user_id, :
|
221
|
+
add_index('blog.comments', :user_id, where: 'active')
|
189
222
|
```
|
190
223
|
## Indexes on Expressions
|
191
224
|
|
@@ -194,67 +227,67 @@ expressions are supported.
|
|
194
227
|
|
195
228
|
### Examples
|
196
229
|
|
197
|
-
Add an index to a column with a function
|
230
|
+
Add an index to a column with a function:
|
198
231
|
|
199
232
|
```ruby
|
200
233
|
add_index(:comments, "lower(text)")
|
201
234
|
```
|
202
235
|
|
203
|
-
You can also specify index access method
|
236
|
+
You can also specify the index access method:
|
204
237
|
|
205
238
|
```ruby
|
206
239
|
create_extension 'btree_gist'
|
207
240
|
create_extension 'fuzzystrmatch'
|
208
|
-
add_index(:comments, 'dmetaphone(author)', :
|
241
|
+
add_index(:comments, 'dmetaphone(author)', using: 'gist')
|
209
242
|
```
|
210
243
|
|
211
244
|
## Concurrent index creation
|
212
245
|
|
213
|
-
PostgreSQL supports
|
214
|
-
DSL on index and foreign
|
246
|
+
PostgreSQL supports concurrent index creation. PgSaurus supports that feature by adding support
|
247
|
+
to the migration DSL on index and foreign key creation.
|
215
248
|
|
216
249
|
### Examples
|
217
250
|
|
218
|
-
Add an index concurrently to a table
|
251
|
+
Add an index concurrently to a table:
|
219
252
|
|
220
253
|
```ruby
|
221
|
-
add_index :table, :column_id, :
|
254
|
+
add_index :table, :column_id, concurrently: true
|
222
255
|
```
|
223
256
|
|
224
|
-
Add an index concurrently along with foreign key
|
257
|
+
Add an index concurrently along with foreign key:
|
225
258
|
|
226
259
|
```ruby
|
227
|
-
add_foreign_key :table1, :table2, :
|
260
|
+
add_foreign_key :table1, :table2, column: :column_id, concurrent_index: true
|
228
261
|
```
|
229
262
|
|
230
263
|
## Loading/Unloading postgresql extension modules
|
231
264
|
|
232
|
-
|
265
|
+
PostgreSQL ships with a number of [extension modules](http://www.postgresql.org/docs/9.4/static/contrib.html).
|
233
266
|
PgSaurus provides some tools
|
234
|
-
to [load](http://www.postgresql.org/docs/9.
|
235
|
-
|
267
|
+
to [load](http://www.postgresql.org/docs/9.4/static/sql-createextension.html) and
|
268
|
+
[unload](http://www.postgresql.org/docs/9.4/static/sql-dropextension.html)
|
269
|
+
such modules using migrations.
|
236
270
|
|
237
|
-
Please note
|
238
|
-
So this functionality
|
271
|
+
Please note: `CREATE`/`DROP EXTENSION` command was introduced in PostgreSQL 9.1.
|
272
|
+
So this functionality is not available in previous versions.
|
239
273
|
|
240
274
|
### Examples
|
241
275
|
|
242
|
-
Load [fuzzystrmatch](http://www.postgresql.org/docs/9.
|
243
|
-
and create its objects in schema *public*:
|
276
|
+
Load [fuzzystrmatch](http://www.postgresql.org/docs/9.4/static/fuzzystrmatch.html)
|
277
|
+
extension module; and create its objects in schema *public*:
|
244
278
|
|
245
279
|
```ruby
|
246
280
|
create_extension "fuzzystrmatch"
|
247
281
|
```
|
248
282
|
|
249
|
-
|
250
|
-
|
251
|
-
and create its objects in schema *demography*.
|
283
|
+
Load version *1.0* of the [btree_gist](http://www.postgresql.org/docs/9.4/static/btree-gist.html)
|
284
|
+
extension module; and create its objects in schema *demography*.
|
252
285
|
|
253
286
|
```ruby
|
254
|
-
create_extension "btree_gist", :
|
287
|
+
create_extension "btree_gist", schema_name: "demography", version: "1.0"
|
255
288
|
```
|
256
289
|
|
257
|
-
Unload extension module:
|
290
|
+
Unload an extension module:
|
258
291
|
|
259
292
|
```ruby
|
260
293
|
drop_extension "fuzzystrmatch"
|
@@ -262,13 +295,15 @@ drop_extension "fuzzystrmatch"
|
|
262
295
|
|
263
296
|
## Views
|
264
297
|
|
265
|
-
|
298
|
+
PgSaurus v1.6.0 introduced experimental support for creating views. This API should only be used
|
266
299
|
with the understanding that it is preliminary 'alpha' at best.
|
267
300
|
|
268
|
-
###
|
301
|
+
### Examples
|
269
302
|
|
270
303
|
```ruby
|
271
304
|
create_view "demography.citizens_view", "select * from demography.citizens"
|
305
|
+
|
306
|
+
drop_view "demography.citizens_view"
|
272
307
|
```
|
273
308
|
|
274
309
|
## Roles
|
@@ -303,10 +338,10 @@ end
|
|
303
338
|
```
|
304
339
|
|
305
340
|
You may force all migrations to have `set_role`, for this, configure PgSaurus with
|
306
|
-
`ensure_role_set=true`:
|
341
|
+
`ensure_role_set = true`:
|
307
342
|
|
308
343
|
```ruby
|
309
|
-
PgSaurus.
|
344
|
+
PgSaurus.configure do |config|
|
310
345
|
config.ensure_role_set = true
|
311
346
|
end
|
312
347
|
```
|
@@ -317,8 +352,9 @@ You can create, list, and drop functions.
|
|
317
352
|
|
318
353
|
### Examples
|
319
354
|
|
355
|
+
Create a function:
|
356
|
+
|
320
357
|
```ruby
|
321
|
-
# Create a function
|
322
358
|
pets_not_empty_function = <<-SQL
|
323
359
|
BEGIN
|
324
360
|
IF (SELECT COUNT(*) FROM pets) > 0
|
@@ -329,13 +365,18 @@ BEGIN
|
|
329
365
|
END IF;
|
330
366
|
END;
|
331
367
|
SQL
|
368
|
+
|
332
369
|
# Arguments are: function_name, return_type, function_definition, options (currently, only :schema)
|
333
370
|
create_function 'pets_not_empty()', :boolean, pets_not_empty_function, schema: 'public'
|
371
|
+
```
|
372
|
+
Drop a function:
|
334
373
|
|
335
|
-
|
374
|
+
```ruby
|
336
375
|
drop_function 'pets_not_empty()'
|
376
|
+
```
|
377
|
+
Get a list of defined functions:
|
337
378
|
|
338
|
-
|
379
|
+
```ruby
|
339
380
|
ActiveRecord::Base.connection.functions
|
340
381
|
```
|
341
382
|
|
@@ -345,28 +386,34 @@ You can create and remove triggers on tables and views.
|
|
345
386
|
|
346
387
|
### Examples
|
347
388
|
|
389
|
+
Create a trigger:
|
390
|
+
|
348
391
|
```ruby
|
349
|
-
# Create a trigger
|
350
392
|
create_trigger :pets, # Table or view name
|
351
393
|
:pets_not_empty_trigger_proc, # Procedure name. Parentheses are optional if you have no arguments.
|
352
394
|
'AFTER INSERT', # Trigger event
|
353
|
-
for_each: 'ROW', # Can be row or statement. Default is row.
|
395
|
+
for_each: 'ROW', # Can be a row or a statement. Default is row.
|
354
396
|
schema: 'public', # Optional schema name
|
355
|
-
constraint: true, # Sets
|
356
|
-
deferrable: true, # Sets
|
357
|
-
initially_deferred: true, # Sets
|
397
|
+
constraint: true, # Sets whether the trigger is a constraint. Default is false.
|
398
|
+
deferrable: true, # Sets whether the trigger is immediate or deferrable. Default is immediate.
|
399
|
+
initially_deferred: true, # Sets whether the trigger is initially deferred. Default is immediate.
|
400
|
+
# Only relevant if the trigger is deferrable.
|
358
401
|
condition: "new.name = 'fluffy'" # Optional when condition. Default is none.
|
402
|
+
```
|
403
|
+
Drop a trigger:
|
359
404
|
|
360
|
-
|
405
|
+
```ruby
|
361
406
|
remove_trigger :pets, :pets_not_empty_trigger_proc
|
407
|
+
```
|
408
|
+
Get a list of defined triggers on a table or view:
|
362
409
|
|
363
|
-
|
410
|
+
```ruby
|
364
411
|
ActiveRecord::Base.connection.triggers
|
365
412
|
```
|
366
413
|
|
367
414
|
## Tools
|
368
415
|
|
369
|
-
PgSaurus::Tools provides a number of useful methods
|
416
|
+
PgSaurus::Tools provides a number of useful methods for managing schemas, etc.:
|
370
417
|
|
371
418
|
```ruby
|
372
419
|
PgSaurus::Tools.create_schema "services" # => create new PG schema "services"
|
@@ -378,51 +425,47 @@ PgSaurus::Tools.schemas # => ["public", "inform
|
|
378
425
|
PgSaurus::Tools.index_exists?(table, columns, options) # => returns true if an index exists for the given params
|
379
426
|
```
|
380
427
|
|
381
|
-
##
|
382
|
-
|
383
|
-
PgSaurus does not support Rails 3.
|
384
|
-
|
385
|
-
## Running tests:
|
428
|
+
## Running tests
|
386
429
|
|
387
|
-
* Ensure your postgresql has postgres-contrib (Ubuntu) package installed. Tests depend on btree_gist and fuzzystrmatch extensions
|
388
|
-
* If on Mac, see below for installing contrib packages
|
430
|
+
* Ensure your `postgresql` has `postgres-contrib` (if you're on Ubuntu) package installed. Tests depend on the `btree_gist` and `fuzzystrmatch` extensions
|
431
|
+
* If you're on a Mac, see below for installing contrib packages
|
389
432
|
* Configure `spec/dummy/config/database.yml` for development and test environments.
|
390
433
|
* Run `rake spec`.
|
391
434
|
* Make sure migrations don't raise exceptions and all specs pass.
|
392
435
|
|
393
436
|
### Installing contrib packages on Mac OS X:
|
394
|
-
* This assumes you are using MacPorts to install
|
395
|
-
* Assuming you installed with default options (including auto-clean), you will need to rebuild the postgresql port and keep the build files
|
396
|
-
* `sudo port -k -s build
|
437
|
+
* This assumes you are using [MacPorts](https://www.macports.org) to install `postgresql`. If you're using [Homebrew](http://brew.sh) or the Postgres App, you will need to adjust the instructions accordingly (please add to this README when you do)
|
438
|
+
* Assuming you installed with default options (including auto-clean), you will need to rebuild the `postgresql` port and keep the build files
|
439
|
+
* `sudo port -k -s build postgresql94`
|
397
440
|
* (adjust the version number above appropriately)
|
398
|
-
* Now you can make and install the btree_gist and any other contrib modules
|
399
|
-
* `cd
|
441
|
+
* Now you can make and install the `btree_gist` and any other contrib modules
|
442
|
+
* `cd $(port work postgresql94)/postgresql-9.4.7/contrib/btree_gist`
|
400
443
|
* (again, you may need to adjust the version number to your specific version)
|
401
444
|
* `sudo make all`
|
402
445
|
* `sudo make install`
|
403
446
|
* Done!
|
404
447
|
|
405
|
-
## TODO
|
448
|
+
## TODO
|
406
449
|
|
407
|
-
Support for Rails
|
450
|
+
Support for Rails 5+
|
408
451
|
|
409
|
-
*
|
452
|
+
* Rails 5 introduces its own schema support. PgSaurus v4+ will have to drop any conflicting support and modify its other features to accommodate Rails 5's schema support.
|
410
453
|
|
411
|
-
|
454
|
+
Possible support for JRuby:
|
412
455
|
|
413
456
|
* Jdbc driver provides its own `create_schema(schema, user)` method - solve conflicts.
|
414
457
|
|
415
458
|
## Credits
|
416
459
|
|
417
|
-
* [Potapov Sergey](https://github.com/greyblake) - schema support
|
418
|
-
* [Arthur Shagall](https://github.com/albertosaurus) - function support - and thanks for [pg_comment](https://github.com/albertosaurus/pg_comment)
|
419
|
-
* [
|
460
|
+
* [Potapov Sergey](https://github.com/greyblake) - schema support, role support
|
461
|
+
* [Arthur Shagall](https://github.com/albertosaurus) - function and trigger support, Rails 4.2 support - and thanks for [pg_comment](https://github.com/albertosaurus/pg_comment)
|
462
|
+
* [Mitya Lyubarskyy](https://github.com/MityaLiu) - view support, Rails 4.2 support
|
420
463
|
* [Artem Ignatyev](https://github.com/cryo28) - extension modules load/unload support
|
421
464
|
* [Marcelo Silveira](https://github.com/mhfs) - thanks for rails partial index support that was backported into this gem
|
422
465
|
|
423
466
|
## Copyright and License
|
424
467
|
|
425
|
-
* Copyright (c)
|
468
|
+
* Copyright (c) 2016 HornsAndHooves.
|
426
469
|
* Initial foreign key code taken from foreigner, Copyright (c) 2009 Matthew Higgins
|
427
470
|
* pg_comment Copyright (c) 2011 Arthur Shagall
|
428
471
|
* Partial index Copyright (c) 2012 Marcelo Silveira
|
@@ -30,7 +30,8 @@ module ActiveRecord # :nodoc:
|
|
30
30
|
# Search using provided schema if table_name includes schema name.
|
31
31
|
#
|
32
32
|
def index_name_exists?(table_name, index_name, default)
|
33
|
-
|
33
|
+
postgre_sql_name = PostgreSQL::Utils.extract_schema_qualified_name(table_name)
|
34
|
+
schema, table = postgre_sql_name.schema, postgre_sql_name.identifier
|
34
35
|
schemas = schema ? "ARRAY['#{schema}']" : 'current_schemas(false)'
|
35
36
|
|
36
37
|
exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
|
@@ -65,7 +66,8 @@ module ActiveRecord # :nodoc:
|
|
65
66
|
# the custom {PgSaurus::ConnectionAdapters::IndexDefinition}
|
66
67
|
#
|
67
68
|
def indexes(table_name, name = nil)
|
68
|
-
|
69
|
+
postgre_sql_name = PostgreSQL::Utils.extract_schema_qualified_name(table_name)
|
70
|
+
schema, table = postgre_sql_name.schema, postgre_sql_name.identifier
|
69
71
|
schemas = schema ? "ARRAY['#{schema}']" : 'current_schemas(false)'
|
70
72
|
|
71
73
|
result = query(<<-SQL, name)
|
@@ -4,14 +4,12 @@ module PgSaurus::ConnectionAdapters::AbstractAdapter
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
autoload :CommentMethods
|
7
|
-
autoload :ForeignerMethods
|
8
7
|
autoload :SchemaMethods
|
9
8
|
autoload :IndexMethods
|
10
9
|
autoload :FunctionMethods
|
11
10
|
autoload :TriggerMethods
|
12
11
|
|
13
12
|
include CommentMethods
|
14
|
-
include ForeignerMethods
|
15
13
|
include SchemaMethods
|
16
14
|
include IndexMethods
|
17
15
|
include FunctionMethods
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module PgSaurus # :nodoc:
|
2
|
+
# Provides methods to extend {ActiveRecord::ConnectionAdapters::PostgreSQLAdapter}
|
3
|
+
# to support foreign keys feature.
|
4
|
+
module ConnectionAdapters::PostgreSQLAdapter::ForeignKeyMethods
|
5
|
+
|
6
|
+
# Drop table and optionally disable triggers.
|
7
|
+
# Changes adapted from https://github.com/matthuhiggins/foreigner/blob/e72ab9c454c156056d3f037d55e3359cd972af32/lib/foreigner/connection_adapters/sql2003.rb
|
8
|
+
# NOTE: Disabling referential integrity requires superuser access in postgres.
|
9
|
+
# Default AR behavior is just to drop_table.
|
10
|
+
#
|
11
|
+
# == Options:
|
12
|
+
# * :force - force disabling of referential integrity
|
13
|
+
#
|
14
|
+
# Note: I don't know a good way to test this -mike 20120420
|
15
|
+
def drop_table(*args)
|
16
|
+
options = args.clone.extract_options!
|
17
|
+
if options[:force]
|
18
|
+
disable_referential_integrity { super }
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# See activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
|
25
|
+
# Creates index on the FK column by default. Pass in the option :exclude_index => true
|
26
|
+
# to disable this.
|
27
|
+
def add_foreign_key_with_index(from_table, to_table, options = {})
|
28
|
+
exclude_index = (options.has_key?(:exclude_index) ? options.delete(:exclude_index) : false)
|
29
|
+
column = options[:column] || foreign_key_column_for(to_table)
|
30
|
+
|
31
|
+
if index_exists?(from_table, column) && !exclude_index
|
32
|
+
raise PgSaurus::IndexExistsError,
|
33
|
+
"The index, #{index_name(from_table, column)}, already exists." \
|
34
|
+
" Use :exclude_index => true when adding the foreign key."
|
35
|
+
end
|
36
|
+
|
37
|
+
add_foreign_key_without_index from_table, to_table, options
|
38
|
+
|
39
|
+
unless exclude_index
|
40
|
+
add_index from_table, column
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# See activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
|
45
|
+
def remove_foreign_key_with_index(from_table, options_or_to_table = {})
|
46
|
+
if options_or_to_table.is_a?(Hash) && options_or_to_table[:remove_index]
|
47
|
+
column = options_or_to_table[:column]
|
48
|
+
remove_index from_table, column
|
49
|
+
end
|
50
|
+
|
51
|
+
remove_foreign_key_without_index(from_table, options_or_to_table)
|
52
|
+
end
|
53
|
+
|
54
|
+
# See: activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
|
55
|
+
def foreign_key_column_for_with_schema(table_name)
|
56
|
+
table = table_name.to_s.split('.').last
|
57
|
+
|
58
|
+
foreign_key_column_for_without_schema table
|
59
|
+
end
|
60
|
+
|
61
|
+
# see activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
|
62
|
+
def foreign_keys_with_schema(table_name)
|
63
|
+
namespace = table_name.to_s.split('.').first
|
64
|
+
table_name = table_name.to_s.split('.').last
|
65
|
+
|
66
|
+
namespace = if namespace == table_name
|
67
|
+
"ANY (current_schemas(false))"
|
68
|
+
else
|
69
|
+
quote(namespace)
|
70
|
+
end
|
71
|
+
|
72
|
+
sql = <<-SQL.strip_heredoc
|
73
|
+
SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, t3.nspname AS from_schema
|
74
|
+
FROM pg_constraint c
|
75
|
+
JOIN pg_class t1 ON c.conrelid = t1.oid
|
76
|
+
JOIN pg_class t2 ON c.confrelid = t2.oid
|
77
|
+
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
|
78
|
+
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
|
79
|
+
JOIN pg_namespace t3 ON c.connamespace = t3.oid
|
80
|
+
WHERE c.contype = 'f'
|
81
|
+
AND t1.relname = #{quote(table_name)}
|
82
|
+
AND t3.nspname = #{namespace}
|
83
|
+
ORDER BY c.conname
|
84
|
+
SQL
|
85
|
+
|
86
|
+
fk_info = select_all(sql)
|
87
|
+
|
88
|
+
fk_info.map do |row|
|
89
|
+
options = {
|
90
|
+
column: row['column'],
|
91
|
+
name: row['name'],
|
92
|
+
primary_key: row['primary_key'],
|
93
|
+
from_schema: row['from_schema']
|
94
|
+
}
|
95
|
+
|
96
|
+
options[:on_delete] = extract_foreign_key_action(row['on_delete'])
|
97
|
+
options[:on_update] = extract_foreign_key_action(row['on_update'])
|
98
|
+
|
99
|
+
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(table_name, row['to_table'], options)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -34,6 +34,14 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::SchemaMethods
|
|
34
34
|
::PgSaurus::Tools.drop_schema_if_exists(schema_name)
|
35
35
|
end
|
36
36
|
|
37
|
+
# Provide :schema option to +drop_table+ method.
|
38
|
+
def drop_table_with_schema_option(table_name, options = {})
|
39
|
+
options = options.dup
|
40
|
+
schema_name = options.delete(:schema)
|
41
|
+
table_name = "#{schema_name}.#{table_name}" if schema_name
|
42
|
+
drop_table_without_schema_option(table_name, options)
|
43
|
+
end
|
44
|
+
|
37
45
|
# Make method +tables+ return tables not only from public schema.
|
38
46
|
#
|
39
47
|
# @note
|
@@ -8,7 +8,7 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter
|
|
8
8
|
autoload :ExtensionMethods, 'pg_saurus/connection_adapters/postgresql_adapter/extension_methods'
|
9
9
|
autoload :SchemaMethods, 'pg_saurus/connection_adapters/postgresql_adapter/schema_methods'
|
10
10
|
autoload :CommentMethods, 'pg_saurus/connection_adapters/postgresql_adapter/comment_methods'
|
11
|
-
autoload :
|
11
|
+
autoload :ForeignKeyMethods, 'pg_saurus/connection_adapters/postgresql_adapter/foreign_key_methods'
|
12
12
|
autoload :IndexMethods, 'pg_saurus/connection_adapters/postgresql_adapter/index_methods'
|
13
13
|
autoload :TranslateException, 'pg_saurus/connection_adapters/postgresql_adapter/translate_exception'
|
14
14
|
autoload :ViewMethods, 'pg_saurus/connection_adapters/postgresql_adapter/view_methods'
|
@@ -18,7 +18,7 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter
|
|
18
18
|
include ExtensionMethods
|
19
19
|
include SchemaMethods
|
20
20
|
include CommentMethods
|
21
|
-
include
|
21
|
+
include ForeignKeyMethods
|
22
22
|
include IndexMethods
|
23
23
|
include TranslateException
|
24
24
|
include ViewMethods
|
@@ -28,6 +28,18 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter
|
|
28
28
|
included do
|
29
29
|
alias_method_chain :tables, :non_public_schema_tables
|
30
30
|
alias_method_chain :add_index, :concurrently
|
31
|
+
alias_method_chain :drop_table, :schema_option
|
31
32
|
alias_method_chain :rename_table, :schema_option
|
33
|
+
|
34
|
+
alias_method_chain :add_foreign_key, :index
|
35
|
+
alias_method_chain :remove_foreign_key, :index
|
36
|
+
alias_method_chain :foreign_key_column_for, :schema
|
37
|
+
alias_method_chain :foreign_keys, :schema
|
38
|
+
|
39
|
+
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.module_eval do
|
40
|
+
def from_schema
|
41
|
+
options[:from_schema] || 'public'
|
42
|
+
end
|
43
|
+
end
|
32
44
|
end
|
33
45
|
end
|