activerecord-postgresql-extensions 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/activerecord-postgresql-extensions.gemspec +20 -17
- data/lib/activerecord-postgresql-extensions.rb +2 -0
- data/lib/postgresql_extensions/postgresql_adapter_extensions.rb +50 -53
- data/lib/postgresql_extensions/postgresql_constraints.rb +142 -153
- data/lib/postgresql_extensions/postgresql_extensions.rb +246 -0
- data/lib/postgresql_extensions/postgresql_functions.rb +31 -33
- data/lib/postgresql_extensions/postgresql_geometry.rb +2 -2
- data/lib/postgresql_extensions/postgresql_indexes.rb +13 -14
- data/lib/postgresql_extensions/postgresql_languages.rb +4 -4
- data/lib/postgresql_extensions/postgresql_permissions.rb +12 -14
- data/lib/postgresql_extensions/postgresql_roles.rb +2 -2
- data/lib/postgresql_extensions/postgresql_rules.rb +11 -10
- data/lib/postgresql_extensions/postgresql_schemas.rb +4 -4
- data/lib/postgresql_extensions/postgresql_sequences.rb +15 -16
- data/lib/postgresql_extensions/postgresql_tables.rb +20 -21
- data/lib/postgresql_extensions/postgresql_text_search.rb +313 -0
- data/lib/postgresql_extensions/postgresql_triggers.rb +13 -14
- data/lib/postgresql_extensions/postgresql_types.rb +1 -1
- data/lib/postgresql_extensions/postgresql_views.rb +13 -14
- data/test/{adapter_test.rb → adapter_tests.rb} +6 -6
- data/test/{constraints_test.rb → constraints_tests.rb} +13 -13
- data/test/extensions_tests.rb +275 -0
- data/test/{functions_test.rb → functions_tests.rb} +10 -10
- data/test/{geometry_test.rb → geometry_tests.rb} +16 -16
- data/test/{index_test.rb → index_tests.rb} +11 -11
- data/test/{languages_test.rb → languages_tests.rb} +6 -6
- data/test/{permissions_test.rb → permissions_tests.rb} +36 -36
- data/test/{roles_test.rb → roles_tests.rb} +6 -6
- data/test/{rules_test.rb → rules_tests.rb} +3 -3
- data/test/{schemas_test.rb → schemas_tests.rb} +6 -6
- data/test/{sequences_test.rb → sequences_tests.rb} +10 -10
- data/test/{tables_test.rb → tables_tests.rb} +2 -2
- data/test/text_search_tests.rb +263 -0
- metadata +19 -16
- data/postgresql-extensions.gemspec +0 -50
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
require 'rubygems'
|
5
5
|
require 'rubygems/package_task'
|
6
6
|
require 'rake/testtask'
|
7
|
-
require '
|
7
|
+
require 'rdoc/task'
|
8
8
|
|
9
9
|
if RUBY_VERSION >= '1.9'
|
10
10
|
begin
|
@@ -35,7 +35,7 @@ end
|
|
35
35
|
|
36
36
|
desc 'Test PostgreSQL extensions'
|
37
37
|
Rake::TestTask.new(:test) do |t|
|
38
|
-
t.
|
38
|
+
t.test_files = FileList['test/**/*_tests.rb']
|
39
39
|
t.verbose = false
|
40
40
|
end
|
41
41
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.11
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "activerecord-postgresql-extensions"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.11"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["J Smith"]
|
12
|
-
s.date = "2011-
|
12
|
+
s.date = "2011-11-09"
|
13
13
|
s.description = "A whole bunch of extensions the ActiveRecord PostgreSQL adapter."
|
14
14
|
s.email = "code@zoocasa.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"lib/postgresql_extensions/foreign_key_associations.rb",
|
26
26
|
"lib/postgresql_extensions/postgresql_adapter_extensions.rb",
|
27
27
|
"lib/postgresql_extensions/postgresql_constraints.rb",
|
28
|
+
"lib/postgresql_extensions/postgresql_extensions.rb",
|
28
29
|
"lib/postgresql_extensions/postgresql_functions.rb",
|
29
30
|
"lib/postgresql_extensions/postgresql_geometry.rb",
|
30
31
|
"lib/postgresql_extensions/postgresql_indexes.rb",
|
@@ -35,27 +36,29 @@ Gem::Specification.new do |s|
|
|
35
36
|
"lib/postgresql_extensions/postgresql_schemas.rb",
|
36
37
|
"lib/postgresql_extensions/postgresql_sequences.rb",
|
37
38
|
"lib/postgresql_extensions/postgresql_tables.rb",
|
39
|
+
"lib/postgresql_extensions/postgresql_text_search.rb",
|
38
40
|
"lib/postgresql_extensions/postgresql_triggers.rb",
|
39
41
|
"lib/postgresql_extensions/postgresql_types.rb",
|
40
42
|
"lib/postgresql_extensions/postgresql_views.rb",
|
41
|
-
"
|
42
|
-
"test/
|
43
|
-
"test/
|
44
|
-
"test/
|
45
|
-
"test/
|
46
|
-
"test/
|
47
|
-
"test/
|
48
|
-
"test/
|
49
|
-
"test/
|
50
|
-
"test/
|
51
|
-
"test/
|
52
|
-
"test/
|
53
|
-
"test/
|
54
|
-
"test/test_helper.rb"
|
43
|
+
"test/adapter_tests.rb",
|
44
|
+
"test/constraints_tests.rb",
|
45
|
+
"test/extensions_tests.rb",
|
46
|
+
"test/functions_tests.rb",
|
47
|
+
"test/geometry_tests.rb",
|
48
|
+
"test/index_tests.rb",
|
49
|
+
"test/languages_tests.rb",
|
50
|
+
"test/permissions_tests.rb",
|
51
|
+
"test/roles_tests.rb",
|
52
|
+
"test/rules_tests.rb",
|
53
|
+
"test/schemas_tests.rb",
|
54
|
+
"test/sequences_tests.rb",
|
55
|
+
"test/tables_tests.rb",
|
56
|
+
"test/test_helper.rb",
|
57
|
+
"test/text_search_tests.rb"
|
55
58
|
]
|
56
59
|
s.homepage = "http://github.com/zoocasa/activerecord-postgresql-extensions"
|
57
60
|
s.require_paths = ["lib"]
|
58
|
-
s.rubygems_version = "1.8.
|
61
|
+
s.rubygems_version = "1.8.11"
|
59
62
|
s.summary = "A whole bunch of extensions the ActiveRecord PostgreSQL adapter."
|
60
63
|
|
61
64
|
if s.respond_to? :specification_version then
|
@@ -22,6 +22,8 @@ dirname = File.join(File.dirname(__FILE__), 'postgresql_extensions')
|
|
22
22
|
postgresql_geometry
|
23
23
|
postgresql_types
|
24
24
|
postgresql_roles
|
25
|
+
postgresql_text_search
|
26
|
+
postgresql_extensions
|
25
27
|
foreign_key_associations
|
26
28
|
}.each do |file|
|
27
29
|
require File.join(dirname, file)
|
@@ -57,16 +57,15 @@ module ActiveRecord
|
|
57
57
|
#
|
58
58
|
# Examples:
|
59
59
|
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
# end
|
60
|
+
# # should produce '"geospatial"."my_tables"'
|
61
|
+
# with_schema :geospatial do
|
62
|
+
# quote_table_name('my_table')
|
63
|
+
# end
|
65
64
|
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
65
|
+
# # should produce 'SELECT * FROM "geospatial"."models"'
|
66
|
+
# with_schema :geospatial do
|
67
|
+
# Model.find(:all)
|
68
|
+
# end
|
70
69
|
def with_schema schema
|
71
70
|
scoped_schemas << schema
|
72
71
|
begin
|
@@ -81,30 +80,28 @@ module ActiveRecord
|
|
81
80
|
#
|
82
81
|
# Example:
|
83
82
|
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
# end
|
83
|
+
# with_schema :geospatial do
|
84
|
+
# create_table(:test) do |t|
|
85
|
+
# ignore_schema do
|
86
|
+
# t.integer(
|
87
|
+
# :ref_id,
|
88
|
+
# :references => {
|
89
|
+
# :table => :refs,
|
90
|
+
# :column => :id,
|
91
|
+
# :deferrable => true
|
92
|
+
# }
|
93
|
+
# )
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
# end
|
99
97
|
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
# )
|
98
|
+
# # Produces:
|
99
|
+
# #
|
100
|
+
# # CREATE TABLE "geospatial"."test" (
|
101
|
+
# # "id" serial primary key,
|
102
|
+
# # "ref_id" integer DEFAULT NULL NULL,
|
103
|
+
# # FOREIGN KEY ("ref_id") REFERENCES "refs" ("id")
|
104
|
+
# # )
|
108
105
|
#
|
109
106
|
# Here we see that we used the geospatial schema when naming the
|
110
107
|
# test table and dropped back to not specifying a schema when
|
@@ -112,8 +109,7 @@ module ActiveRecord
|
|
112
109
|
# used ignore_schema, the foreign key would have been defined
|
113
110
|
# thusly:
|
114
111
|
#
|
115
|
-
#
|
116
|
-
# FOREIGN KEY ("ref_id") REFERENCES "geospatial"."refs" ("id")
|
112
|
+
# FOREIGN KEY ("ref_id") REFERENCES "geospatial"."refs" ("id")
|
117
113
|
def ignore_schema
|
118
114
|
with_schema nil do
|
119
115
|
yield
|
@@ -209,22 +205,21 @@ module ActiveRecord
|
|
209
205
|
#
|
210
206
|
# Example of using a Hash as a table name:
|
211
207
|
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
# # => "geospatial"."epois"
|
208
|
+
# quote_table_name(:geospatial => :epois) # => "geospatial"."epois"
|
209
|
+
# # => "geospatial"."epois"
|
215
210
|
#
|
216
|
-
#
|
217
|
-
#
|
211
|
+
# quote_table_name(:epois)
|
212
|
+
# # => "epois"
|
218
213
|
#
|
219
|
-
#
|
220
|
-
#
|
214
|
+
# with_schema(:geospatial) { quote_table_name(:epois) }
|
215
|
+
# # => "geospatial"."epois"
|
221
216
|
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
#
|
217
|
+
# with_schema(:geospatial) do
|
218
|
+
# ignore_schema do
|
219
|
+
# quote_table_name(:epois)
|
220
|
+
# end
|
221
|
+
# end
|
222
|
+
# # => "epois"
|
228
223
|
def quote_table_name_with_schemas(name)
|
229
224
|
if current_schema || name.is_a?(Hash)
|
230
225
|
quote_generic_with_schema(name)
|
@@ -450,16 +445,16 @@ module ActiveRecord
|
|
450
445
|
|
451
446
|
sql = 'SET '
|
452
447
|
sql << "#{duration} " if duration
|
453
|
-
sql << "ROLE #{quote_role(role)}"
|
448
|
+
sql << "ROLE #{quote_role(role)};"
|
454
449
|
execute(sql, "Setting current role")
|
455
450
|
end
|
456
451
|
|
457
452
|
def reset_role
|
458
|
-
execute('RESET ROLE')
|
453
|
+
execute('RESET ROLE;')
|
459
454
|
end
|
460
455
|
|
461
456
|
def current_role
|
462
|
-
execute('SELECT current_role')
|
457
|
+
execute('SELECT current_role;')
|
463
458
|
end
|
464
459
|
alias :current_user :current_role
|
465
460
|
|
@@ -468,7 +463,7 @@ module ActiveRecord
|
|
468
463
|
query(<<-SQL, name).map { |row| row[0] }
|
469
464
|
SELECT tablename
|
470
465
|
FROM pg_tables
|
471
|
-
WHERE schemaname IN ('pg_catalog')
|
466
|
+
WHERE schemaname IN ('pg_catalog');
|
472
467
|
SQL
|
473
468
|
end
|
474
469
|
|
@@ -483,7 +478,7 @@ module ActiveRecord
|
|
483
478
|
alias_method_chain :schema_search_path=, :csv_fix
|
484
479
|
|
485
480
|
def schema_search_path_with_csv_fix #:nodoc:
|
486
|
-
@schema_search_path ||= query('SHOW search_path')[0][0].gsub(/, /, ',')
|
481
|
+
@schema_search_path ||= query('SHOW search_path;')[0][0].gsub(/, /, ',')
|
487
482
|
end
|
488
483
|
alias_method_chain :schema_search_path, :csv_fix
|
489
484
|
|
@@ -543,6 +538,7 @@ module ActiveRecord
|
|
543
538
|
a.attnum = conkey
|
544
539
|
AND
|
545
540
|
a.attrelid = conrelid
|
541
|
+
;
|
546
542
|
SQL
|
547
543
|
|
548
544
|
query(sql, name).inject([]) do |memo, (tbl, column, referenced_column)|
|
@@ -603,6 +599,7 @@ module ActiveRecord
|
|
603
599
|
a.attnum = conkey
|
604
600
|
AND
|
605
601
|
a.attrelid = conrelid
|
602
|
+
;
|
606
603
|
SQL
|
607
604
|
|
608
605
|
query(sql, name).inject([]) do |memo, (tbl, column, referenced_column)|
|
@@ -630,7 +627,7 @@ module ActiveRecord
|
|
630
627
|
|
631
628
|
def change_column_default_with_expression(table_name, column_name, default) #:nodoc:
|
632
629
|
if default.is_a?(Hash) && default.has_key?(:expression)
|
633
|
-
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{default[:expression]}"
|
630
|
+
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{default[:expression]};"
|
634
631
|
else
|
635
632
|
change_column_default_without_expression(table_name, column_name, default)
|
636
633
|
end
|
@@ -31,7 +31,7 @@ module ActiveRecord
|
|
31
31
|
def add_check_constraint(table, expression, options = {})
|
32
32
|
sql = "ALTER TABLE #{quote_table_name(table)} ADD "
|
33
33
|
sql << PostgreSQLCheckConstraint.new(self, expression, options).to_s
|
34
|
-
execute
|
34
|
+
execute("#{sql};")
|
35
35
|
end
|
36
36
|
|
37
37
|
# Adds a UNIQUE constraint to the table. See
|
@@ -39,7 +39,7 @@ module ActiveRecord
|
|
39
39
|
def add_unique_constraint(table, columns, options = {})
|
40
40
|
sql = "ALTER TABLE #{quote_table_name(table)} ADD "
|
41
41
|
sql << PostgreSQLUniqueConstraint.new(self, columns, options).to_s
|
42
|
-
execute
|
42
|
+
execute("#{sql};")
|
43
43
|
end
|
44
44
|
|
45
45
|
# Adds a FOREIGN KEY constraint to the table. See
|
@@ -47,7 +47,7 @@ module ActiveRecord
|
|
47
47
|
def add_foreign_key(table, columns, ref_table, *args)
|
48
48
|
sql = "ALTER TABLE #{quote_table_name(table)} ADD "
|
49
49
|
sql << PostgreSQLForeignKeyConstraint.new(self, columns, ref_table, *args).to_s
|
50
|
-
execute
|
50
|
+
execute("#{sql};")
|
51
51
|
end
|
52
52
|
|
53
53
|
# Drops a constraint from the table. Use this to drop CHECK,
|
@@ -60,7 +60,7 @@ module ActiveRecord
|
|
60
60
|
def drop_constraint(table, name, options = {})
|
61
61
|
sql = "ALTER TABLE #{quote_table_name(table)} DROP CONSTRAINT #{quote_generic(name)}"
|
62
62
|
sql << ' CASCADE' if options[:cascade]
|
63
|
-
execute
|
63
|
+
execute("#{sql};")
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -119,18 +119,17 @@ module ActiveRecord
|
|
119
119
|
#
|
120
120
|
# ==== Example
|
121
121
|
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
# end
|
122
|
+
# create_table(:foo) do |t|
|
123
|
+
# t.integer :fancy_id, :check => "fancy_id != 10"
|
124
|
+
# end
|
126
125
|
#
|
127
|
-
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
126
|
+
# # Produces:
|
127
|
+
# #
|
128
|
+
# # CREATE TABLE "foo" (
|
129
|
+
# # "id" serial primary key,
|
130
|
+
# # "fancy_id" integer DEFAULT NULL NULL,
|
131
|
+
# # CHECK (fancy_id != 10)
|
132
|
+
# # );
|
134
133
|
#
|
135
134
|
# You can also provide an Array to <tt>:check</tt> with multiple CHECK
|
136
135
|
# constraints. Each CHECK constraint can be either a String containing
|
@@ -140,10 +139,9 @@ module ActiveRecord
|
|
140
139
|
# automatically. Thus, the following is equivalent to the example
|
141
140
|
# above:
|
142
141
|
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
# end
|
142
|
+
# create_table(:foo) do |t|
|
143
|
+
# t.integer :fancy_id, :check => [ { :expression => "fancy_id != 10" } ]
|
144
|
+
# end
|
147
145
|
#
|
148
146
|
# See below for additional options.
|
149
147
|
#
|
@@ -154,36 +152,35 @@ module ActiveRecord
|
|
154
152
|
#
|
155
153
|
# ==== Examples
|
156
154
|
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
# # );
|
155
|
+
# create_table(:foo) do |t|
|
156
|
+
# t.integer :fancy_id
|
157
|
+
# t.integer :another_fancy_id
|
158
|
+
# t.check_constraint 'fancy_id != another_fancy_id'
|
159
|
+
# end
|
160
|
+
#
|
161
|
+
# # Produces:
|
162
|
+
# #
|
163
|
+
# # CREATE TABLE "foo" (
|
164
|
+
# # "id" serial primary key,
|
165
|
+
# # "fancy_id" integer DEFAULT NULL NULL,
|
166
|
+
# # "another_fancy_id" integer DEFAULT NULL NULL,
|
167
|
+
# # CHECK (fancy_id != another_fancy_id)
|
168
|
+
# # );
|
169
|
+
#
|
170
|
+
# create_table(:foo) do |t|
|
171
|
+
# t.integer :fancy_id
|
172
|
+
# t.integer :another_fancy_id
|
173
|
+
# t.check_constraint 'fancy_id != another_fancy_id', :name => 'my_constraint'
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
# # Produces:
|
177
|
+
# #
|
178
|
+
# # CREATE TABLE "foo" (
|
179
|
+
# # "id" serial primary key,
|
180
|
+
# # "fancy_id" integer DEFAULT NULL NULL,
|
181
|
+
# # "another_fancy_id" integer DEFAULT NULL NULL,
|
182
|
+
# # CONSTRAINT "my_constraint" CHECK (fancy_id != another_fancy_id)
|
183
|
+
# # );
|
187
184
|
#
|
188
185
|
# See below for additional options.
|
189
186
|
#
|
@@ -194,12 +191,11 @@ module ActiveRecord
|
|
194
191
|
#
|
195
192
|
# ==== Example
|
196
193
|
#
|
197
|
-
#
|
198
|
-
# add_check_constraint(:foo, 'fancy_id != 10')
|
194
|
+
# add_check_constraint(:foo, 'fancy_id != 10')
|
199
195
|
#
|
200
|
-
#
|
201
|
-
#
|
202
|
-
#
|
196
|
+
# # Produces:
|
197
|
+
# #
|
198
|
+
# # ALTER TABLE "foo" ADD CHECK (fancy_id != 10);
|
203
199
|
#
|
204
200
|
# See below for additional options.
|
205
201
|
#
|
@@ -254,18 +250,17 @@ module ActiveRecord
|
|
254
250
|
#
|
255
251
|
# ==== Example:
|
256
252
|
#
|
257
|
-
#
|
258
|
-
#
|
259
|
-
#
|
260
|
-
# end
|
253
|
+
# create_table(:foo) do |t|
|
254
|
+
# t.integer :fancy_id, :unique => true
|
255
|
+
# end
|
261
256
|
#
|
262
|
-
#
|
263
|
-
#
|
264
|
-
#
|
265
|
-
#
|
266
|
-
#
|
267
|
-
#
|
268
|
-
#
|
257
|
+
# # Produces:
|
258
|
+
# #
|
259
|
+
# # CREATE TABLE "foo" (
|
260
|
+
# # "id" serial primary key,
|
261
|
+
# # "fancy_id" integer DEFAULT NULL NULL,
|
262
|
+
# # UNIQUE ("fancy_id")
|
263
|
+
# # );
|
269
264
|
#
|
270
265
|
# You can provide additional options to the UNIQUE constraint by
|
271
266
|
# passing a Hash instead of true. See below for details on these
|
@@ -279,21 +274,20 @@ module ActiveRecord
|
|
279
274
|
#
|
280
275
|
# ==== Example:
|
281
276
|
#
|
282
|
-
#
|
283
|
-
#
|
284
|
-
#
|
285
|
-
#
|
286
|
-
#
|
287
|
-
#
|
288
|
-
#
|
289
|
-
#
|
290
|
-
#
|
291
|
-
#
|
292
|
-
#
|
293
|
-
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
# # );
|
277
|
+
# create_table(:foo) do |t|
|
278
|
+
# t.integer :fancy_id
|
279
|
+
# t.integer :another_fancy_id
|
280
|
+
# t.unique_constraint [ :fancy_id, :another_fancy_id ]
|
281
|
+
# end
|
282
|
+
#
|
283
|
+
# # Produces:
|
284
|
+
# #
|
285
|
+
# # CREATE TABLE "foo" (
|
286
|
+
# # "id" serial primary key,
|
287
|
+
# # "fancy_id" integer DEFAULT NULL NULL,
|
288
|
+
# # "another_fancy_id" integer DEFAULT NULL NULL,
|
289
|
+
# # UNIQUE ("fancy_id", "another_fancy_id")
|
290
|
+
# # );
|
297
291
|
#
|
298
292
|
# See below for additional options.
|
299
293
|
#
|
@@ -312,18 +306,17 @@ module ActiveRecord
|
|
312
306
|
#
|
313
307
|
# ==== Examples:
|
314
308
|
#
|
315
|
-
#
|
316
|
-
#
|
317
|
-
#
|
318
|
-
# # => ALTER TABLE "foo" ADD UNIQUE ("fancy_id", "another_fancy_id");
|
309
|
+
# # using the constraint method:
|
310
|
+
# add_unique_constraint(:foo, [ :fancy_id, :another_fancy_id ])
|
311
|
+
# # => ALTER TABLE "foo" ADD UNIQUE ("fancy_id", "another_fancy_id");
|
319
312
|
#
|
320
|
-
#
|
321
|
-
#
|
322
|
-
#
|
313
|
+
# # using create_index:
|
314
|
+
# create_index('my_index_name', :foo, [ :fancy_id, :another_fancy_id ], :unique => true)
|
315
|
+
# # => CREATE UNIQUE INDEX "my_index_name" ON "foo"("fancy_id", "another_fancy_id");
|
323
316
|
#
|
324
|
-
#
|
325
|
-
#
|
326
|
-
#
|
317
|
+
# # using the standard ActiveRecord add_index:
|
318
|
+
# add_index(:foo, [ :fancy_id, :another_fancy_id ], :unique => true)
|
319
|
+
# # => CREATE UNIQUE INDEX "index_foo_on_fancy_id_and_another_fancy_id" ON "foo" ("fancy_id", "another_fancy_id");
|
327
320
|
#
|
328
321
|
# You'll notice that in create_index we must manually supply a name
|
329
322
|
# while add_index can generate one for us automatically. See the
|
@@ -395,18 +388,17 @@ module ActiveRecord
|
|
395
388
|
#
|
396
389
|
# ==== Example:
|
397
390
|
#
|
398
|
-
#
|
399
|
-
#
|
400
|
-
#
|
401
|
-
# end
|
391
|
+
# create_table(:foo) do |t|
|
392
|
+
# t.integer :bar_id, :references => { :table => :bar, :column => :id }
|
393
|
+
# end
|
402
394
|
#
|
403
|
-
#
|
404
|
-
#
|
405
|
-
#
|
406
|
-
#
|
407
|
-
#
|
408
|
-
#
|
409
|
-
#
|
395
|
+
# # Produces:
|
396
|
+
# #
|
397
|
+
# # CREATE TABLE "foo" (
|
398
|
+
# # "id" serial primary key,
|
399
|
+
# # "bar_id" integer DEFAULT NULL NULL,
|
400
|
+
# # FOREIGN KEY ("bar_id") REFERENCES "bar" ("id")
|
401
|
+
# # );
|
410
402
|
#
|
411
403
|
# You can leave out the :column option if you are following the Rails
|
412
404
|
# standards for foreign key referral, as PostgreSQL automatically
|
@@ -426,54 +418,52 @@ module ActiveRecord
|
|
426
418
|
#
|
427
419
|
# The following example produces the same result as above:
|
428
420
|
#
|
429
|
-
#
|
430
|
-
#
|
431
|
-
#
|
432
|
-
#
|
433
|
-
# end
|
421
|
+
# create_table(:foo) do |t|
|
422
|
+
# t.integer :bar_id
|
423
|
+
# t.foreign_key :bar_id, :bar, :id
|
424
|
+
# end
|
434
425
|
#
|
435
|
-
#
|
436
|
-
#
|
437
|
-
#
|
438
|
-
#
|
439
|
-
#
|
440
|
-
#
|
441
|
-
#
|
426
|
+
# # Produces:
|
427
|
+
# #
|
428
|
+
# # CREATE TABLE "foo" (
|
429
|
+
# # "id" serial primary key,
|
430
|
+
# # "bar_id" integer DEFAULT NULL NULL,
|
431
|
+
# # FOREIGN KEY ("bar_id") REFERENCES "bar" ("id")
|
432
|
+
# # );
|
442
433
|
#
|
443
434
|
# Defining a FOREIGN KEY constraint on the table-level allows you to
|
444
435
|
# create multicolumn foreign keys. You can define these super advanced
|
445
436
|
# foreign keys thusly:
|
446
437
|
#
|
447
|
-
#
|
448
|
-
#
|
449
|
-
#
|
450
|
-
#
|
451
|
-
#
|
452
|
-
#
|
453
|
-
#
|
454
|
-
#
|
455
|
-
#
|
456
|
-
#
|
457
|
-
#
|
458
|
-
#
|
459
|
-
#
|
460
|
-
#
|
461
|
-
#
|
462
|
-
#
|
463
|
-
#
|
464
|
-
#
|
465
|
-
#
|
466
|
-
#
|
467
|
-
#
|
468
|
-
#
|
469
|
-
#
|
470
|
-
#
|
471
|
-
#
|
472
|
-
#
|
473
|
-
#
|
474
|
-
#
|
475
|
-
#
|
476
|
-
# # );
|
438
|
+
# create_table(:foo) {}
|
439
|
+
#
|
440
|
+
# create_table(:bar) do |t|
|
441
|
+
# t.integer :foo_id
|
442
|
+
# t.unique_constraint [ :id, :foo_id ]
|
443
|
+
# end
|
444
|
+
#
|
445
|
+
# create_table(:funk) do |t|
|
446
|
+
# t.integer :bar_id
|
447
|
+
# t.foreign_key [ :id, :bar_id ], :bar, [ :id, :foo_id ]
|
448
|
+
# end
|
449
|
+
#
|
450
|
+
# # Produces:
|
451
|
+
# #
|
452
|
+
# # CREATE TABLE "foo" (
|
453
|
+
# # "id" serial primary key
|
454
|
+
# # );
|
455
|
+
# #
|
456
|
+
# # CREATE TABLE "bar" (
|
457
|
+
# # "id" serial primary key,
|
458
|
+
# # "foo_id" integer DEFAULT NULL NULL,
|
459
|
+
# # UNIQUE ("id", "foo_id")
|
460
|
+
# # );
|
461
|
+
# #
|
462
|
+
# # CREATE TABLE "funk" (
|
463
|
+
# # "id" serial primary key,
|
464
|
+
# # "bar_id" integer DEFAULT NULL NULL,
|
465
|
+
# # FOREIGN KEY ("id", "bar_id") REFERENCES "bar" ("id", "foo_id")
|
466
|
+
# # );
|
477
467
|
#
|
478
468
|
# === Table Manipulation
|
479
469
|
#
|
@@ -482,18 +472,17 @@ module ActiveRecord
|
|
482
472
|
#
|
483
473
|
# ==== Examples:
|
484
474
|
#
|
485
|
-
#
|
486
|
-
#
|
487
|
-
# # => ALTER TABLE "funk" ADD FOREIGN KEY ("bar_id") REFERENCES "bar";
|
475
|
+
# add_foreign_key(:foo, :bar_id, :bar)
|
476
|
+
# # => ALTER TABLE "funk" ADD FOREIGN KEY ("bar_id") REFERENCES "bar";
|
488
477
|
#
|
489
|
-
#
|
490
|
-
#
|
478
|
+
# add_foreign_key(:foo, :bar_id, :bar, :id)
|
479
|
+
# # => ALTER TABLE "funk" ADD FOREIGN KEY ("bar_id") REFERENCES "bar"("id");
|
491
480
|
#
|
492
|
-
#
|
493
|
-
#
|
494
|
-
#
|
495
|
-
#
|
496
|
-
#
|
481
|
+
# add_foreign_key(:foo, [ :bar_id, :blort_id ], :bar, [ :id, :blort_id ],
|
482
|
+
# :name => 'my_fk', :match => :simple
|
483
|
+
# )
|
484
|
+
# # => ALTER TABLE "foo" ADD CONSTRAINT "my_fk" FOREIGN KEY ("id", "blort_id")
|
485
|
+
# # REFERENCES "bar" ("id", "blort_id") MATCH SIMPLE;
|
497
486
|
#
|
498
487
|
# === Options for FOREIGN KEY Constraints
|
499
488
|
#
|