File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed
lib/active_record/connection_adapters/postgresql
test/cases/adapters/postgresql Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change
1
+ * Add support for ` :if_not_exists ` and ` :force ` options to ` create_schema `
2
+
3
+ * fatkodima*
4
+
1
5
* Fix ` index_errors ` having incorrect index in association validation errors.
2
6
3
7
* lulalala*
Original file line number Diff line number Diff line change @@ -209,8 +209,16 @@ def schema_names
209
209
end
210
210
211
211
# Creates a schema for the given schema name.
212
- def create_schema ( schema_name )
213
- execute "CREATE SCHEMA #{ quote_schema_name ( schema_name ) } "
212
+ def create_schema ( schema_name , force : nil , if_not_exists : nil )
213
+ if force && if_not_exists
214
+ raise ArgumentError , "Options `:force` and `:if_not_exists` cannot be used simultaneously."
215
+ end
216
+
217
+ if force
218
+ drop_schema ( schema_name , if_exists : true )
219
+ end
220
+
221
+ execute ( "CREATE SCHEMA#{ ' IF NOT EXISTS' if if_not_exists } #{ quote_schema_name ( schema_name ) } " )
214
222
end
215
223
216
224
# Drops the schema for the given schema name.
Original file line number Diff line number Diff line change @@ -131,6 +131,32 @@ def test_raise_create_schema_with_existing_schema
131
131
@connection . drop_schema "test_schema3"
132
132
end
133
133
134
+ def test_force_create_schema
135
+ @connection . create_schema "test_schema3"
136
+ assert_queries_match ( /DROP SCHEMA IF EXISTS "test_schema3"/ ) do
137
+ @connection . create_schema "test_schema3" , force : true
138
+ end
139
+ assert @connection . schema_names . include? ( "test_schema3" )
140
+ ensure
141
+ @connection . drop_schema "test_schema3"
142
+ end
143
+
144
+ def test_create_schema_if_not_exists
145
+ @connection . create_schema "test_schema3"
146
+ assert_queries_match ( 'CREATE SCHEMA IF NOT EXISTS "test_schema3"' ) do
147
+ @connection . create_schema "test_schema3" , if_not_exists : true
148
+ end
149
+ assert @connection . schema_names . include? ( "test_schema3" )
150
+ ensure
151
+ @connection . drop_schema "test_schema3"
152
+ end
153
+
154
+ def test_create_schema_raises_if_both_force_and_if_not_exists_provided
155
+ assert_raises ( ArgumentError , match : "Options `:force` and `:if_not_exists` cannot be used simultaneously." ) do
156
+ @connection . create_schema "test_schema3" , force : true , if_not_exists : true
157
+ end
158
+ end
159
+
134
160
def test_drop_schema
135
161
begin
136
162
@connection . create_schema "test_schema3"
You can’t perform that action at this time.
0 commit comments