activerecord-postgresql-extensions 0.0.12 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +3 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +7 -2
  5. data/Rakefile +4 -17
  6. data/activerecord-postgresql-extensions.gemspec +13 -57
  7. data/lib/{postgresql_extensions/postgresql_adapter_extensions.rb → active_record/postgresql_extensions/adapter_extensions.rb} +44 -46
  8. data/lib/{postgresql_extensions/postgresql_constraints.rb → active_record/postgresql_extensions/constraints.rb} +121 -10
  9. data/lib/{postgresql_extensions/postgresql_extensions.rb → active_record/postgresql_extensions/extensions.rb} +1 -1
  10. data/lib/{postgresql_extensions → active_record/postgresql_extensions}/foreign_key_associations.rb +9 -1
  11. data/lib/{postgresql_extensions/postgresql_functions.rb → active_record/postgresql_extensions/functions.rb} +9 -3
  12. data/lib/{postgresql_extensions/postgresql_geometry.rb → active_record/postgresql_extensions/geometry.rb} +111 -35
  13. data/lib/{postgresql_extensions/postgresql_indexes.rb → active_record/postgresql_extensions/indexes.rb} +4 -2
  14. data/lib/{postgresql_extensions/postgresql_languages.rb → active_record/postgresql_extensions/languages.rb} +1 -1
  15. data/lib/{postgresql_extensions/postgresql_permissions.rb → active_record/postgresql_extensions/permissions.rb} +3 -3
  16. data/lib/active_record/postgresql_extensions/postgis.rb +53 -0
  17. data/lib/{postgresql_extensions/postgresql_roles.rb → active_record/postgresql_extensions/roles.rb} +1 -1
  18. data/lib/{postgresql_extensions/postgresql_rules.rb → active_record/postgresql_extensions/rules.rb} +3 -3
  19. data/lib/{postgresql_extensions/postgresql_schemas.rb → active_record/postgresql_extensions/schemas.rb} +1 -1
  20. data/lib/{postgresql_extensions/postgresql_sequences.rb → active_record/postgresql_extensions/sequences.rb} +2 -2
  21. data/lib/{postgresql_extensions/postgresql_tables.rb → active_record/postgresql_extensions/tables.rb} +18 -4
  22. data/lib/{postgresql_extensions/postgresql_tablespaces.rb → active_record/postgresql_extensions/tablespaces.rb} +1 -1
  23. data/lib/{postgresql_extensions/postgresql_text_search.rb → active_record/postgresql_extensions/text_search.rb} +3 -3
  24. data/lib/{postgresql_extensions/postgresql_triggers.rb → active_record/postgresql_extensions/triggers.rb} +1 -1
  25. data/lib/{postgresql_extensions/postgresql_types.rb → active_record/postgresql_extensions/types.rb} +1 -1
  26. data/lib/active_record/postgresql_extensions/utils.rb +23 -0
  27. data/lib/active_record/postgresql_extensions/version.rb +7 -0
  28. data/lib/{postgresql_extensions/postgresql_views.rb → active_record/postgresql_extensions/views.rb} +2 -2
  29. data/lib/activerecord-postgresql-extensions.rb +23 -22
  30. data/test/adapter_tests.rb +9 -9
  31. data/test/constraints_tests.rb +155 -0
  32. data/test/database.yml +17 -0
  33. data/test/geometry_tests.rb +224 -52
  34. data/test/index_tests.rb +16 -1
  35. data/test/rules_tests.rb +4 -4
  36. data/test/sequences_tests.rb +0 -22
  37. data/test/tables_tests.rb +28 -31
  38. data/test/test_helper.rb +70 -23
  39. data/test/trigger_tests.rb +5 -5
  40. metadata +112 -25
  41. data/VERSION +0 -1
data/test/database.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ # To modify the test database parameters, create a new file called
3
+ # local_database.yml and go nuts with settings. The "jdbc" settings
4
+ # are merged into the "arunit" settings as JDBC works over a TCP
5
+ # socket and those sorts of connections generally require some user
6
+ # credentials.
7
+
8
+ arunit:
9
+ adapter: "postgresql"
10
+ database: "postgresql_extensions_unit_tests"
11
+ min_messages: "warning"
12
+ schema_search_path: "public"
13
+
14
+ jdbc:
15
+ host: "localhost"
16
+ adapter: "jdbcpostgresql"
17
+
@@ -2,79 +2,251 @@
2
2
  $: << File.dirname(__FILE__)
3
3
  require 'test_helper'
4
4
 
5
- class GeometryTests < Test::Unit::TestCase
6
- include PostgreSQLExtensionsTestHelper
5
+ if ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION
6
+ if ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] >= '2.0'
7
+ class GeometryTests < Test::Unit::TestCase
8
+ include PostgreSQLExtensionsTestHelper
7
9
 
8
- def test_create_geometry
9
- Mig.create_table(:foo) do |t|
10
- t.geometry :the_geom, :srid => 4326
10
+ def test_create_geometry
11
+ Mig.create_table(:foo) do |t|
12
+ t.geometry :the_geom, :srid => 4326
13
+ end
14
+
15
+ assert_equal([
16
+ %{CREATE TABLE "foo" (
17
+ "id" serial primary key,
18
+ "the_geom" geometry(GEOMETRY, 4326)
19
+ );},
20
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
21
+ ], statements)
22
+ end
23
+
24
+ def test_create_geometry_with_spatial
25
+ Mig.create_table(:foo) do |t|
26
+ t.spatial :the_geom, :srid => 4326
27
+ end
28
+
29
+ assert_equal([
30
+ %{CREATE TABLE "foo" (
31
+ "id" serial primary key,
32
+ "the_geom" geometry(GEOMETRY, 4326)
33
+ );},
34
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
35
+ ], statements)
36
+ end
37
+
38
+ def test_create_geometry_with_spatial_and_spatial_column_type
39
+ Mig.create_table(:foo) do |t|
40
+ t.spatial :the_geom, :srid => 4326, :spatial_column_type => :geography
41
+ end
42
+
43
+ assert_equal([
44
+ %{CREATE TABLE "foo" (
45
+ "id" serial primary key,
46
+ "the_geom" geography(GEOMETRY, 4326)
47
+ );},
48
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
49
+ ], statements)
50
+ end
51
+
52
+ def test_create_geography
53
+ Mig.create_table(:foo) do |t|
54
+ t.geography :the_geom, :srid => 4326
55
+ end
56
+
57
+ assert_equal([
58
+ %{CREATE TABLE "foo" (
59
+ "id" serial primary key,
60
+ "the_geom" geography(GEOMETRY, 4326)
61
+ );},
62
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
63
+ ], statements)
64
+ end
65
+
66
+ def test_create_geometry_with_force_constraints
67
+ Mig.create_table(:foo) do |t|
68
+ t.geometry :the_geom, :srid => 4326, :force_constraints => true
69
+ end
70
+
71
+ assert_equal([
72
+ %{CREATE TABLE "foo" (
73
+ "id" serial primary key,
74
+ "the_geom" geometry(GEOMETRY, 4326),
75
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
76
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
77
+ );},
78
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
79
+ ], statements)
80
+ end
81
+
82
+ def test_create_geometry_with_schema
83
+ Mig.create_table('shabba.foo') do |t|
84
+ t.geometry :the_geom, :srid => 4326
85
+ end
86
+
87
+ assert_equal([
88
+ %{CREATE TABLE "shabba"."foo" (
89
+ "id" serial primary key,
90
+ "the_geom" geometry(GEOMETRY, 4326)
91
+ );},
92
+ %{CREATE INDEX "foo_the_geom_gist_index" ON "shabba"."foo" USING "gist"("the_geom");}
93
+ ], statements)
94
+ end
95
+
96
+ def test_create_geometry_with_not_null
97
+ Mig.create_table(:foo) do |t|
98
+ t.geometry :the_geom, :srid => 4326, :null => false
99
+ end
100
+
101
+ assert_equal([
102
+ %{CREATE TABLE "foo" (
103
+ "id" serial primary key,
104
+ "the_geom" geometry(GEOMETRY, 4326) NOT NULL
105
+ );},
106
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
107
+ ], statements)
108
+ end
109
+
110
+ def test_create_geometry_with_null_and_type
111
+ Mig.create_table(:foo) do |t|
112
+ t.geometry :the_geom, :srid => 4326, :geometry_type => :polygon
113
+ end
114
+
115
+ assert_equal([
116
+ %{CREATE TABLE "foo" (
117
+ "id" serial primary key,
118
+ "the_geom" geometry(POLYGON, 4326)
119
+ );},
120
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
121
+ ], statements)
122
+ end
11
123
  end
124
+ else
125
+ class GeometryTests < Test::Unit::TestCase
126
+ include PostgreSQLExtensionsTestHelper
127
+
128
+ def test_create_geometry
129
+ Mig.create_table(:foo) do |t|
130
+ t.geometry :the_geom, :srid => 4326
131
+ end
12
132
 
13
- assert_equal([
14
- %{CREATE TABLE "foo" (
133
+ assert_equal([
134
+ %{CREATE TABLE "foo" (
15
135
  "id" serial primary key,
16
136
  "the_geom" geometry,
17
- CONSTRAINT "enforce_srid_the_geom" CHECK (srid("the_geom") = (4326)),
18
- CONSTRAINT "enforce_dims_the_geom" CHECK (ndims("the_geom") = 2)
137
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
138
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
19
139
  );},
20
- %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
21
- %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
22
- %{CREATE INDEX "foo_the_geom_gist_index" ON "foo" USING "gist"("the_geom");}
23
- ], statements)
24
- end
140
+ %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
141
+ %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
142
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
143
+ ], statements)
144
+ end
25
145
 
26
- def test_create_geometry_with_schema
27
- Mig.create_table('public.foo') do |t|
28
- t.geometry :the_geom, :srid => 4326
29
- end
146
+ def test_create_geometry_with_spatial
147
+ Mig.create_table(:foo) do |t|
148
+ t.spatial :the_geom, :srid => 4326
149
+ end
30
150
 
31
- assert_equal([
32
- %{CREATE TABLE "public"."foo" (
151
+ assert_equal([
152
+ %{CREATE TABLE "foo" (
33
153
  "id" serial primary key,
34
154
  "the_geom" geometry,
35
- CONSTRAINT "enforce_srid_the_geom" CHECK (srid("the_geom") = (4326)),
36
- CONSTRAINT "enforce_dims_the_geom" CHECK (ndims("the_geom") = 2)
155
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
156
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
37
157
  );},
38
- %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
39
- %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
40
- %{CREATE INDEX "foo_the_geom_gist_index" ON "foo" USING "gist"("the_geom");}
41
- ], statements)
42
- end
158
+ %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
159
+ %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
160
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
161
+ ], statements)
162
+ end
43
163
 
44
- def test_create_geometry_with_not_null
45
- Mig.create_table(:foo) do |t|
46
- t.geometry :the_geom, :srid => 4326, :null => false
47
- end
164
+ def test_create_geometry_with_spatial_and_spatial_column_type
165
+ Mig.create_table(:foo) do |t|
166
+ t.spatial :the_geom, :srid => 4326, :spatial_column_type => :geography
167
+ end
168
+
169
+ assert_equal([
170
+ %{CREATE TABLE "foo" (
171
+ "id" serial primary key,
172
+ "the_geom" geography,
173
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
174
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
175
+ );},
176
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
177
+ ], statements)
178
+ end
48
179
 
49
- assert_equal([
50
- %{CREATE TABLE "foo" (
180
+ def test_create_geography
181
+ Mig.create_table(:foo) do |t|
182
+ t.geography :the_geom, :srid => 4326
183
+ end
184
+
185
+ assert_equal([
186
+ %{CREATE TABLE "foo" (
187
+ "id" serial primary key,
188
+ "the_geom" geography,
189
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
190
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
191
+ );},
192
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
193
+ ], statements)
194
+ end
195
+
196
+ def test_create_geometry_with_schema
197
+ Mig.create_table('shabba.foo') do |t|
198
+ t.geometry :the_geom, :srid => 4326
199
+ end
200
+
201
+ assert_equal([
202
+ %{CREATE TABLE "shabba"."foo" (
203
+ "id" serial primary key,
204
+ "the_geom" geometry,
205
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
206
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
207
+ );},
208
+ %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'shabba' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
209
+ %{INSERT INTO "geometry_columns" VALUES ('', 'shabba', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
210
+ %{CREATE INDEX "foo_the_geom_gist_index" ON "shabba"."foo" USING "gist"("the_geom");}
211
+ ], statements)
212
+ end
213
+
214
+ def test_create_geometry_with_not_null
215
+ Mig.create_table(:foo) do |t|
216
+ t.geometry :the_geom, :srid => 4326, :null => false
217
+ end
218
+
219
+ assert_equal([
220
+ %{CREATE TABLE "foo" (
51
221
  "id" serial primary key,
52
222
  "the_geom" geometry NOT NULL,
53
- CONSTRAINT "enforce_srid_the_geom" CHECK (srid("the_geom") = (4326)),
54
- CONSTRAINT "enforce_dims_the_geom" CHECK (ndims("the_geom") = 2)
223
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
224
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2)
55
225
  );},
56
- %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
57
- %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
58
- %{CREATE INDEX "foo_the_geom_gist_index" ON "foo" USING "gist"("the_geom");}
59
- ], statements)
60
- end
226
+ %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
227
+ %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'GEOMETRY');},
228
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
229
+ ], statements)
230
+ end
61
231
 
62
- def test_create_geometry_with_null_and_type
63
- Mig.create_table(:foo) do |t|
64
- t.geometry :the_geom, :srid => 4326, :geometry_type => :polygon
65
- end
232
+ def test_create_geometry_with_null_and_type
233
+ Mig.create_table(:foo) do |t|
234
+ t.geometry :the_geom, :srid => 4326, :geometry_type => :polygon
235
+ end
66
236
 
67
- assert_equal([
68
- %{CREATE TABLE "foo" (
237
+ assert_equal([
238
+ %{CREATE TABLE "foo" (
69
239
  "id" serial primary key,
70
240
  "the_geom" geometry,
71
- CONSTRAINT "enforce_srid_the_geom" CHECK (srid("the_geom") = (4326)),
72
- CONSTRAINT "enforce_dims_the_geom" CHECK (ndims("the_geom") = 2),
241
+ CONSTRAINT "enforce_srid_the_geom" CHECK (ST_srid("the_geom") = (4326)),
242
+ CONSTRAINT "enforce_dims_the_geom" CHECK (ST_ndims("the_geom") = 2),
73
243
  CONSTRAINT "enforce_geotype_the_geom" CHECK (geometrytype("the_geom") = 'POLYGON'::text OR "the_geom" IS NULL)
74
244
  );},
75
- %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
76
- %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'POLYGON');},
77
- %{CREATE INDEX "foo_the_geom_gist_index" ON "foo" USING "gist"("the_geom");}
78
- ], statements)
245
+ %{DELETE FROM "geometry_columns" WHERE f_table_catalog = '' AND f_table_schema = 'public' AND f_table_name = 'foo' AND f_geometry_column = 'the_geom';},
246
+ %{INSERT INTO "geometry_columns" VALUES ('', 'public', 'foo', 'the_geom', 2, 4326, 'POLYGON');},
247
+ %{CREATE INDEX "foo_the_geom_gist_index" ON PUBLIC."foo" USING "gist"("the_geom");}
248
+ ], statements)
249
+ end
250
+ end
79
251
  end
80
252
  end
data/test/index_tests.rb CHANGED
@@ -28,13 +28,28 @@ class IndexTests < Test::Unit::TestCase
28
28
  :conditions => 'bar_id IS NOT NULL'
29
29
  })
30
30
 
31
+ Mig.create_index(:foo_bar_id_idx, :foo, {
32
+ :column => :bar_id
33
+ }, {
34
+ :conditions => Foo.send(:sanitize_sql, {
35
+ :id => [1, 2, 3, 4]
36
+ })
37
+ })
38
+
39
+ escaped_array = if ActiveRecord::VERSION::STRING >= "3.0"
40
+ "(1, 2, 3, 4)"
41
+ else
42
+ "(1,2,3,4)"
43
+ end
44
+
31
45
  assert_equal([
32
46
  "CREATE INDEX \"foo_names_idx\" ON \"foo\"(\"first_name\", \"last_name\");",
33
47
  "CREATE INDEX \"foo_bar_id_idx\" ON \"foo\"(\"bar_id\");",
34
48
  "CREATE INDEX \"foo_coalesce_bar_id_idx\" ON \"foo\"((COALESCE(bar_id, 0)));",
35
49
  "CREATE INDEX \"foo_search_idx\" ON \"foo\" USING \"gin\"(\"search\");",
36
50
  "CREATE INDEX \"foo_names_idx\" ON \"foo\"(\"name\" \"text_pattern_ops\");",
37
- "CREATE UNIQUE INDEX CONCURRENTLY \"foo_bar_id_idx\" ON \"foo\"(\"bar_id\" ASC NULLS LAST) WITH (FILLFACTOR = 10) TABLESPACE \"fubar\" WHERE bar_id IS NOT NULL;"
51
+ "CREATE UNIQUE INDEX CONCURRENTLY \"foo_bar_id_idx\" ON \"foo\"(\"bar_id\" ASC NULLS LAST) WITH (FILLFACTOR = 10) TABLESPACE \"fubar\" WHERE (bar_id IS NOT NULL);",
52
+ "CREATE INDEX \"foo_bar_id_idx\" ON \"foo\"(\"bar_id\") WHERE (\"foos\".\"id\" IN #{escaped_array});",
38
53
  ], statements)
39
54
  end
40
55
 
data/test/rules_tests.rb CHANGED
@@ -7,18 +7,18 @@ class RulesTests < Test::Unit::TestCase
7
7
 
8
8
  def test_create_rule
9
9
  ARBC.create_rule(
10
- :ignore_root, :update, :users, :instead, :nothing, :conditions => 'user_id = 0'
10
+ :ignore_root, :update, :foos, :instead, :nothing, :conditions => 'user_id = 0'
11
11
  )
12
12
  ARBC.create_rule(
13
- :ignore_root, :update, :users, :instead, 'SELECT * FROM non_admins', {
13
+ :ignore_root, :update, :foos, :instead, 'SELECT * FROM non_admins', {
14
14
  :force => true,
15
15
  :conditions => 'user_id > 0'
16
16
  }
17
17
  )
18
18
 
19
19
  assert_equal([
20
- "CREATE RULE \"ignore_root\" AS ON UPDATE TO \"users\" WHERE user_id = 0 DO INSTEAD NOTHING;",
21
- "CREATE OR REPLACE RULE \"ignore_root\" AS ON UPDATE TO \"users\" WHERE user_id > 0 DO INSTEAD SELECT * FROM non_admins;"
20
+ "CREATE RULE \"ignore_root\" AS ON UPDATE TO \"foos\" WHERE user_id = 0 DO INSTEAD NOTHING;",
21
+ "CREATE OR REPLACE RULE \"ignore_root\" AS ON UPDATE TO \"foos\" WHERE user_id > 0 DO INSTEAD SELECT * FROM non_admins;"
22
22
  ], statements)
23
23
  end
24
24
 
@@ -65,26 +65,4 @@ class SequenceTests < Test::Unit::TestCase
65
65
  "SELECT setval('foo', 42, false);"
66
66
  ], statements)
67
67
  end
68
-
69
- def test_create_sequence
70
- Mig.alter_sequence(
71
- 'what_a_sequence_of_events',
72
- :restart_with => 10
73
- )
74
-
75
- Mig.alter_sequence(
76
- 'what_a_sequence_of_events',
77
- :start => 10,
78
- :increment => 2,
79
- :cache => 2,
80
- :min_value => nil,
81
- :max_value => 10,
82
- :owned_by => [ :foo, :id ]
83
- )
84
-
85
- assert_equal([
86
- "ALTER SEQUENCE \"what_a_sequence_of_events\" RESTART WITH 10;",
87
- "ALTER SEQUENCE \"what_a_sequence_of_events\" INCREMENT BY 2 NO MINVALUE MAXVALUE 10 START WITH 10 CACHE 2 OWNED BY \"foo\".\"id\";"
88
- ], statements)
89
- end
90
68
  end
data/test/tables_tests.rb CHANGED
@@ -5,46 +5,27 @@ require 'test_helper'
5
5
  class TablesTests < Test::Unit::TestCase
6
6
  include PostgreSQLExtensionsTestHelper
7
7
 
8
- def test_foreign_key_in_column_definition
8
+ def test_default_with_expression
9
9
  Mig.create_table('foo') do |t|
10
- t.integer :foo_id, :references => {
11
- :table => :foo,
12
- :on_delete => :set_null,
13
- :on_update => :cascade
14
- }
15
-
16
- t.integer :bar_id, :references => :bar
17
-
18
- t.integer :baz_id, :references => [ :baz ]
19
-
20
- t.foreign_key [ :schabba_id, :doo_id ], :bar, [ :schabba_id, :doo_id ]
10
+ t.integer :foo_id, :default => { :expression => '10 + 20' }
11
+ t.integer :bar_id, :default => '20 + 10'
21
12
  end
22
13
 
23
- assert_equal([
14
+ if ActiveRecord::VERSION::STRING >= "3.2"
15
+ assert_equal([
24
16
  %{CREATE TABLE "foo" (
25
17
  "id" serial primary key,
26
- "foo_id" integer,
27
- "bar_id" integer,
28
- "baz_id" integer,
29
- FOREIGN KEY ("foo_id") REFERENCES "foo" ON DELETE SET NULL ON UPDATE CASCADE,
30
- FOREIGN KEY ("bar_id") REFERENCES "bar",
31
- FOREIGN KEY ("baz_id") REFERENCES "baz",
32
- FOREIGN KEY ("schabba_id", "doo_id") REFERENCES "bar" ("schabba_id", "doo_id")
18
+ "foo_id" integer DEFAULT 10 + 20,
19
+ "bar_id" integer DEFAULT 20
33
20
  );} ], statements)
34
- end
35
-
36
- def test_default_with_expression
37
- Mig.create_table('foo') do |t|
38
- t.integer :foo_id, :default => { :expression => '1 + 1' }
39
- t.integer :bar_id, :default => '1 + 1'
40
- end
41
-
42
- assert_equal([
21
+ else
22
+ assert_equal([
43
23
  %{CREATE TABLE "foo" (
44
24
  "id" serial primary key,
45
- "foo_id" integer DEFAULT 1 + 1,
46
- "bar_id" integer DEFAULT '1 + 1'
25
+ "foo_id" integer DEFAULT 10 + 20,
26
+ "bar_id" integer DEFAULT '20 + 10'
47
27
  );} ], statements)
28
+ end
48
29
  end
49
30
 
50
31
  def test_like
@@ -121,4 +102,20 @@ class TablesTests < Test::Unit::TestCase
121
102
  %{CREATE TABLE "foo" OF "bar" (\n "id" serial primary key\n);}
122
103
  ], statements)
123
104
  end
105
+
106
+ def test_exclude_constraint
107
+ Mig.create_table('foo') do |t|
108
+ t.text :blort
109
+ t.exclude({
110
+ :element => 'length(blort)',
111
+ :with => '='
112
+ }, {
113
+ :name => 'exclude_blort_length'
114
+ })
115
+ end
116
+
117
+ assert_equal([
118
+ %{CREATE TABLE "foo" (\n "id" serial primary key,\n "blort" text,\n CONSTRAINT "exclude_blort_length" EXCLUDE (length(blort) WITH =)\n);}
119
+ ], statements)
120
+ end
124
121
  end