activerecord-postgresql-extensions 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -120,6 +120,18 @@ module ActiveRecord
120
120
  end
121
121
  end
122
122
 
123
+ alias :original_change_table :change_table
124
+ def change_table(table_name, options = {})
125
+ table = PostgreSQLTable.new(table_name, self)
126
+ yield table
127
+
128
+ unless table.post_processing.blank?
129
+ table.post_processing.each do |pp|
130
+ execute pp.to_s
131
+ end
132
+ end
133
+ end
134
+
123
135
  alias :original_drop_table :drop_table
124
136
  # Drops a table. This method is expanded beyond the standard
125
137
  # ActiveRecord drop_table method to allow for a couple of
@@ -387,5 +399,17 @@ module ActiveRecord
387
399
  end
388
400
  end
389
401
  end
402
+
403
+ class PostgreSQLTable < Table
404
+ # Add statements to execute to after a table has been created.
405
+ def post_processing
406
+ @post_processing ||= []
407
+ end
408
+
409
+ private
410
+ def table_constraints
411
+ @table_constraints ||= []
412
+ end
413
+ end
390
414
  end
391
415
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  module ActiveRecord
3
3
  module PostgreSQLExtensions
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.0"
5
5
  end
6
6
  end
7
7
 
@@ -48,6 +48,33 @@ class ConstraintTests < PostgreSQLExtensionsTestCase
48
48
  SQL
49
49
  end
50
50
 
51
+ def test_add_constraint
52
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLCheckConstraint.new(ARBC, "x = 10"))
53
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLUniqueConstraint.new(ARBC, [ :bar_id, :baz_id ]))
54
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLForeignKeyConstraint.new(ARBC, :bar_id, "bars"))
55
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLExcludeConstraint.new(ARBC, :bars, { :element => :bar_id, :with => '=' }))
56
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLPrimaryKeyConstraint.new(ARBC, [ :bar_id, :baz_id ]))
57
+
58
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLCheckConstraint.new(ARBC, "x = 10", :name => "foo_check"))
59
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLUniqueConstraint.new(ARBC, [ :bar_id, :baz_id ], :name => "foo_uniq"))
60
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLForeignKeyConstraint.new(ARBC, :bar_id, "bars", :name => "foo_fk"))
61
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLExcludeConstraint.new(ARBC, :bars, { :element => :bar_id, :with => '=' }, { :name => "foo_exclude" }))
62
+ Mig.add_constraint(:foo, ActiveRecord::ConnectionAdapters::PostgreSQLPrimaryKeyConstraint.new(ARBC, [ :bar_id, :baz_id ], :name => "foo_pk"))
63
+
64
+ assert_equal([
65
+ %{ALTER TABLE "foo" ADD CHECK (x = 10);},
66
+ %{ALTER TABLE "foo" ADD UNIQUE ("bar_id", "baz_id");},
67
+ %{ALTER TABLE "foo" ADD FOREIGN KEY ("bar_id") REFERENCES "bars";},
68
+ %{ALTER TABLE "foo" ADD EXCLUDE (bar_id WITH =);},
69
+ %{ALTER TABLE "foo" ADD PRIMARY KEY ("bar_id", "baz_id");},
70
+ %{ALTER TABLE "foo" ADD CONSTRAINT "foo_check" CHECK (x = 10);},
71
+ %{ALTER TABLE "foo" ADD CONSTRAINT "foo_uniq" UNIQUE ("bar_id", "baz_id");},
72
+ %{ALTER TABLE "foo" ADD CONSTRAINT "foo_fk" FOREIGN KEY ("bar_id") REFERENCES "bars";},
73
+ %{ALTER TABLE "foo" ADD CONSTRAINT "foo_exclude" EXCLUDE (bar_id WITH =);},
74
+ %{ALTER TABLE "foo" ADD CONSTRAINT "foo_pk" PRIMARY KEY ("bar_id", "baz_id");}
75
+ ], statements)
76
+ end
77
+
51
78
  def test_add_unique_constraint
52
79
  Mig.add_unique_constraint(:foo, :bar_id)
53
80
  Mig.add_unique_constraint(
@@ -26,379 +26,7 @@ class GeometryTests < PostgreSQLExtensionsTestCase
26
26
  end
27
27
 
28
28
  if (ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] rescue '') >= '2.0'
29
- class GeometryTests < PostgreSQLExtensionsTestCase
30
- def test_create_geometry
31
- Mig.create_table(:foo) do |t|
32
- t.geometry :the_geom, :srid => 4326
33
- end
34
-
35
- expected = []
36
-
37
- expected << strip_heredoc(<<-SQL)
38
- CREATE TABLE "foo" (
39
- "id" serial primary key,
40
- "the_geom" geometry(GEOMETRY, 4326)
41
- );
42
- SQL
43
-
44
- expected << strip_heredoc(<<-SQL)
45
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
46
- SQL
47
-
48
- assert_equal(expected, statements)
49
- end
50
-
51
- def test_create_geometry_with_spatial
52
- Mig.create_table(:foo) do |t|
53
- t.spatial :the_geom, :srid => 4326
54
- end
55
-
56
- expected = []
57
-
58
- expected << strip_heredoc(<<-SQL)
59
- CREATE TABLE "foo" (
60
- "id" serial primary key,
61
- "the_geom" geometry(GEOMETRY, 4326)
62
- );
63
- SQL
64
-
65
- expected << strip_heredoc(<<-SQL)
66
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
67
- SQL
68
-
69
- assert_equal(expected, statements)
70
- end
71
-
72
- def test_create_geometry_with_spatial_and_spatial_column_type
73
- Mig.create_table(:foo) do |t|
74
- t.spatial :the_geom, :srid => 4326, :spatial_column_type => :geography
75
- end
76
-
77
- expected = []
78
-
79
- expected << strip_heredoc(<<-SQL)
80
- CREATE TABLE "foo" (
81
- "id" serial primary key,
82
- "the_geom" geography(GEOMETRY, 4326)
83
- );
84
- SQL
85
-
86
- expected << strip_heredoc(<<-SQL)
87
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
88
- SQL
89
-
90
- assert_equal(expected, statements)
91
- end
92
-
93
- def test_create_geography
94
- Mig.create_table(:foo) do |t|
95
- t.geography :the_geom, :srid => 4326
96
- end
97
-
98
- expected = []
99
-
100
- expected << strip_heredoc(<<-SQL)
101
- CREATE TABLE "foo" (
102
- "id" serial primary key,
103
- "the_geom" geography(GEOMETRY, 4326)
104
- );
105
- SQL
106
-
107
- expected << strip_heredoc(<<-SQL)
108
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
109
- SQL
110
-
111
- assert_equal(expected, statements)
112
- end
113
-
114
- def test_create_geometry_with_force_constraints
115
- Mig.create_table(:foo) do |t|
116
- t.geometry :the_geom, :srid => 4326, :force_constraints => true
117
- end
118
-
119
- expected = []
120
-
121
- expected << strip_heredoc(<<-SQL)
122
- CREATE TABLE "foo" (
123
- "id" serial primary key,
124
- "the_geom" geometry(GEOMETRY, 4326),
125
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
126
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
127
- );
128
- SQL
129
-
130
- expected << strip_heredoc(<<-SQL)
131
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
132
- SQL
133
-
134
- assert_equal(expected, statements)
135
- end
136
-
137
- def test_create_geometry_with_schema
138
- Mig.create_table('shabba.foo') do |t|
139
- t.geometry :the_geom, :srid => 4326
140
- end
141
-
142
- expected = []
143
-
144
- expected << strip_heredoc(<<-SQL)
145
- CREATE TABLE "shabba"."foo" (
146
- "id" serial primary key,
147
- "the_geom" geometry(GEOMETRY, 4326)
148
- );
149
- SQL
150
-
151
- expected << strip_heredoc(<<-SQL)
152
- CREATE INDEX "foo_the_geom_gist_index" ON "shabba"."foo" USING "gist"("the_geom");
153
- SQL
154
-
155
- assert_equal(expected, statements)
156
- end
157
-
158
- def test_create_geometry_with_not_null
159
- Mig.create_table(:foo) do |t|
160
- t.geometry :the_geom, :srid => 4326, :null => false
161
- end
162
-
163
- expected = []
164
-
165
- expected << strip_heredoc(<<-SQL)
166
- CREATE TABLE "foo" (
167
- "id" serial primary key,
168
- "the_geom" geometry(GEOMETRY, 4326) NOT NULL
169
- );
170
- SQL
171
-
172
- expected << strip_heredoc(<<-SQL)
173
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
174
- SQL
175
-
176
- assert_equal(expected, statements)
177
- end
178
-
179
- def test_create_geometry_with_null_and_type
180
- Mig.create_table(:foo) do |t|
181
- t.geometry :the_geom, :srid => 4326, :geometry_type => :polygon
182
- end
183
-
184
- expected = []
185
-
186
- expected << strip_heredoc(<<-SQL)
187
- CREATE TABLE "foo" (
188
- "id" serial primary key,
189
- "the_geom" geometry(POLYGON, 4326)
190
- );
191
- SQL
192
-
193
- expected << strip_heredoc(<<-SQL)
194
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
195
- SQL
196
-
197
- assert_equal(expected, statements)
198
- end
199
- end
29
+ require 'geometry_tests_modern_postgis'
200
30
  else
201
- class GeometryTests < Test::Unit::TestCase
202
- def test_create_geometry
203
- Mig.create_table(:foo) do |t|
204
- t.geometry :the_geom, :srid => 4326
205
- end
206
-
207
- expected = []
208
-
209
- expected << strip_heredoc(<<-SQL)
210
- CREATE TABLE "foo" (
211
- "id" serial primary key,
212
- "the_geom" geometry,
213
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
214
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
215
- );
216
- SQL
217
-
218
- expected << strip_heredoc(<<-SQL)
219
- DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';
220
- SQL
221
-
222
- expected << strip_heredoc(<<-SQL)
223
- INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');
224
- SQL
225
-
226
- expected << strip_heredoc(<<-SQL)
227
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
228
- SQL
229
-
230
- assert_equal(expected, statements)
231
- end
232
-
233
- def test_create_geometry_with_spatial
234
- Mig.create_table(:foo) do |t|
235
- t.spatial :the_geom, :srid => 4326
236
- end
237
-
238
- expected = []
239
-
240
- expected << strip_heredoc(<<-SQL)
241
- CREATE TABLE "foo" (
242
- "id" serial primary key,
243
- "the_geom" geometry,
244
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
245
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
246
- );
247
- SQL
248
-
249
- expected << strip_heredoc(<<-SQL)
250
- DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';
251
- SQL
252
-
253
- expected << strip_heredoc(<<-SQL)
254
- INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');
255
- SQL
256
-
257
- expected << strip_heredoc(<<-SQL)
258
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
259
- SQL
260
-
261
- assert_equal(expected, statements)
262
- end
263
-
264
- def test_create_geometry_with_spatial_and_spatial_column_type
265
- Mig.create_table(:foo) do |t|
266
- t.spatial :the_geom, :srid => 4326, :spatial_column_type => :geography
267
- end
268
-
269
- expected = []
270
-
271
- expected << strip_heredoc(<<-SQL)
272
- CREATE TABLE "foo" (
273
- "id" serial primary key,
274
- "the_geom" geography,
275
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
276
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
277
- );
278
- SQL
279
-
280
- expected << strip_heredoc(<<-SQL)
281
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
282
- SQL
283
-
284
- assert_equal(expected, statements)
285
- end
286
-
287
- def test_create_geography
288
- Mig.create_table(:foo) do |t|
289
- t.geography :the_geom, :srid => 4326
290
- end
291
-
292
- expected = []
293
-
294
- expected << strip_heredoc(<<-SQL)
295
- CREATE TABLE "foo" (
296
- "id" serial primary key,
297
- "the_geom" geography,
298
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
299
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
300
- );
301
- SQL
302
-
303
- expected << strip_heredoc(<<-SQL)
304
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
305
- SQL
306
-
307
- assert_equal(expected, statements)
308
- end
309
-
310
- def test_create_geometry_with_schema
311
- Mig.create_table('shabba.foo') do |t|
312
- t.geometry :the_geom, :srid => 4326
313
- end
314
-
315
- expected = []
316
-
317
- expected << strip_heredoc(<<-SQL)
318
- CREATE TABLE "shabba"."foo" (
319
- "id" serial primary key,
320
- "the_geom" geometry,
321
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
322
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
323
- );
324
- SQL
325
-
326
- expected << strip_heredoc(<<-SQL)
327
- DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'shabba' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';
328
- SQL
329
-
330
- expected << strip_heredoc(<<-SQL)
331
- INSERT INTO "geometry_columns" VALUES ('', 'shabba', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');
332
- SQL
333
-
334
- expected << strip_heredoc(<<-SQL)
335
- CREATE INDEX "foo_the_geom_gist_index" ON "shabba"."foo" USING "gist"("the_geom");
336
- SQL
337
-
338
- assert_equal(expected, statements)
339
- end
340
-
341
- def test_create_geometry_with_not_null
342
- Mig.create_table(:foo) do |t|
343
- t.geometry :the_geom, :srid => 4326, :null => false
344
- end
345
-
346
- expected = []
347
-
348
- expected << strip_heredoc(<<-SQL)
349
- CREATE TABLE "foo" (
350
- "id" serial primary key,
351
- "the_geom" geometry NOT NULL,
352
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
353
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
354
- );
355
- SQL
356
-
357
- expected << strip_heredoc(<<-SQL)
358
- DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';
359
- SQL
360
-
361
- expected << strip_heredoc(<<-SQL)
362
- INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');
363
- SQL
364
-
365
- expected << strip_heredoc(<<-SQL)
366
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
367
- SQL
368
-
369
- assert_equal(expected, statements)
370
- end
371
-
372
- def test_create_geometry_with_null_and_type
373
- Mig.create_table(:foo) do |t|
374
- t.geometry :the_geom, :srid => 4326, :geometry_type => :polygon
375
- end
376
-
377
- expected = []
378
-
379
- expected << strip_heredoc(<<-SQL)
380
- CREATE TABLE "foo" (
381
- "id" serial primary key,
382
- "the_geom" geometry,
383
- CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
384
- CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2),
385
- CONSTRAINT "enforce_geotype_the_geom" CHECK (geometrytype("the_geom") = 'POLYGON'::text OR "the_geom" IS NULL)
386
- );
387
- SQL
388
-
389
- expected << strip_heredoc(<<-SQL)
390
- DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';
391
- SQL
392
-
393
- expected << strip_heredoc(<<-SQL)
394
- INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'POLYGON');
395
- SQL
396
-
397
- expected << strip_heredoc(<<-SQL)
398
- CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");
399
- SQL
400
-
401
- assert_equal(expected, statements)
402
- end
403
- end
31
+ require 'geometry_tests_legacy_postgis'
404
32
  end