mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
PostgreSQL, add :if_exists
to #drop_schema
.
This commit is contained in:
parent
9e2b13ca35
commit
dd118dcc45
4 changed files with 30 additions and 13 deletions
|
@ -1,3 +1,9 @@
|
|||
* PostgreSQL, add `:if_exists` option to `#drop_schema`. This makes it
|
||||
possible to drop a schema that might exist without raising an exception if
|
||||
it doesn't.
|
||||
|
||||
*Yves Senn*
|
||||
|
||||
* Only try to nullify has_one target association if the record is persisted.
|
||||
|
||||
Fixes #21223.
|
||||
|
|
|
@ -214,8 +214,8 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
# Drops the schema for the given schema name.
|
||||
def drop_schema schema_name
|
||||
execute "DROP SCHEMA #{schema_name} CASCADE"
|
||||
def drop_schema(schema_name, options = {})
|
||||
execute "DROP SCHEMA#{' IF EXISTS' if options[:if_exists]} #{schema_name} CASCADE"
|
||||
end
|
||||
|
||||
# Sets the schema search path to a string of comma-separated schema names.
|
||||
|
|
|
@ -31,7 +31,7 @@ class SchemaAuthorizationTest < ActiveRecord::PostgreSQLTestCase
|
|||
set_session_auth
|
||||
@connection.execute "RESET search_path"
|
||||
USERS.each do |u|
|
||||
@connection.execute "DROP SCHEMA #{u} CASCADE"
|
||||
@connection.drop_schema u
|
||||
@connection.execute "DROP USER #{u}"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -84,8 +84,8 @@ class SchemaTest < ActiveRecord::PostgreSQLTestCase
|
|||
end
|
||||
|
||||
teardown do
|
||||
@connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
|
||||
@connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
|
||||
@connection.drop_schema SCHEMA2_NAME, if_exists: true
|
||||
@connection.drop_schema SCHEMA_NAME, if_exists: true
|
||||
end
|
||||
|
||||
def test_schema_names
|
||||
|
@ -121,10 +121,17 @@ class SchemaTest < ActiveRecord::PostgreSQLTestCase
|
|||
assert !@connection.schema_names.include?("test_schema3")
|
||||
end
|
||||
|
||||
def test_drop_schema_if_exists
|
||||
@connection.create_schema "some_schema"
|
||||
assert_includes @connection.schema_names, "some_schema"
|
||||
@connection.drop_schema "some_schema", if_exists: true
|
||||
assert_not_includes @connection.schema_names, "some_schema"
|
||||
end
|
||||
|
||||
def test_habtm_table_name_with_schema
|
||||
ActiveRecord::Base.connection.drop_schema "music", if_exists: true
|
||||
ActiveRecord::Base.connection.create_schema "music"
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
DROP SCHEMA IF EXISTS music CASCADE;
|
||||
CREATE SCHEMA music;
|
||||
CREATE TABLE music.albums (id serial primary key);
|
||||
CREATE TABLE music.songs (id serial primary key);
|
||||
CREATE TABLE music.albums_songs (album_id integer, song_id integer);
|
||||
|
@ -134,12 +141,16 @@ class SchemaTest < ActiveRecord::PostgreSQLTestCase
|
|||
Album.create
|
||||
assert_equal song, Song.includes(:albums).references(:albums).first
|
||||
ensure
|
||||
ActiveRecord::Base.connection.execute "DROP SCHEMA music CASCADE;"
|
||||
ActiveRecord::Base.connection.drop_schema "music", if_exists: true
|
||||
end
|
||||
|
||||
def test_raise_drop_schema_with_nonexisting_schema
|
||||
def test_drop_schema_with_nonexisting_schema
|
||||
assert_raises(ActiveRecord::StatementInvalid) do
|
||||
@connection.drop_schema "test_schema3"
|
||||
@connection.drop_schema "idontexist"
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
@connection.drop_schema "idontexist", if_exists: true
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -462,14 +473,14 @@ class SchemaForeignKeyTest < ActiveRecord::PostgreSQLTestCase
|
|||
ensure
|
||||
@connection.drop_table "wagons", if_exists: true
|
||||
@connection.drop_table "my_schema.trains", if_exists: true
|
||||
@connection.execute "DROP SCHEMA IF EXISTS my_schema"
|
||||
@connection.drop_schema "my_schema", if_exists: true
|
||||
end
|
||||
end
|
||||
|
||||
class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCase
|
||||
setup do
|
||||
@connection = ActiveRecord::Base.connection
|
||||
@connection.execute "DROP SCHEMA IF EXISTS schema_1 CASCADE"
|
||||
@connection.drop_schema "schema_1", if_exists: true
|
||||
@connection.execute "CREATE SCHEMA schema_1"
|
||||
@connection.execute "CREATE DOMAIN schema_1.text AS text"
|
||||
@connection.execute "CREATE DOMAIN schema_1.varchar AS varchar"
|
||||
|
@ -487,7 +498,7 @@ class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCa
|
|||
|
||||
teardown do
|
||||
@connection.schema_search_path = @old_search_path
|
||||
@connection.execute "DROP SCHEMA IF EXISTS schema_1 CASCADE"
|
||||
@connection.drop_schema "schema_1", if_exists: true
|
||||
Default.reset_column_information
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue