1
0
Fork 0
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:
Yves Senn 2015-08-28 11:35:12 +02:00
parent 9e2b13ca35
commit dd118dcc45
4 changed files with 30 additions and 13 deletions

View file

@ -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. * Only try to nullify has_one target association if the record is persisted.
Fixes #21223. Fixes #21223.

View file

@ -214,8 +214,8 @@ module ActiveRecord
end end
# Drops the schema for the given schema name. # Drops the schema for the given schema name.
def drop_schema schema_name def drop_schema(schema_name, options = {})
execute "DROP SCHEMA #{schema_name} CASCADE" execute "DROP SCHEMA#{' IF EXISTS' if options[:if_exists]} #{schema_name} CASCADE"
end end
# Sets the schema search path to a string of comma-separated schema names. # Sets the schema search path to a string of comma-separated schema names.

View file

@ -31,7 +31,7 @@ class SchemaAuthorizationTest < ActiveRecord::PostgreSQLTestCase
set_session_auth set_session_auth
@connection.execute "RESET search_path" @connection.execute "RESET search_path"
USERS.each do |u| USERS.each do |u|
@connection.execute "DROP SCHEMA #{u} CASCADE" @connection.drop_schema u
@connection.execute "DROP USER #{u}" @connection.execute "DROP USER #{u}"
end end
end end

View file

@ -84,8 +84,8 @@ class SchemaTest < ActiveRecord::PostgreSQLTestCase
end end
teardown do teardown do
@connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE" @connection.drop_schema SCHEMA2_NAME, if_exists: true
@connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE" @connection.drop_schema SCHEMA_NAME, if_exists: true
end end
def test_schema_names def test_schema_names
@ -121,10 +121,17 @@ class SchemaTest < ActiveRecord::PostgreSQLTestCase
assert !@connection.schema_names.include?("test_schema3") assert !@connection.schema_names.include?("test_schema3")
end 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 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 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.albums (id serial primary key);
CREATE TABLE music.songs (id serial primary key); CREATE TABLE music.songs (id serial primary key);
CREATE TABLE music.albums_songs (album_id integer, song_id integer); CREATE TABLE music.albums_songs (album_id integer, song_id integer);
@ -134,12 +141,16 @@ class SchemaTest < ActiveRecord::PostgreSQLTestCase
Album.create Album.create
assert_equal song, Song.includes(:albums).references(:albums).first assert_equal song, Song.includes(:albums).references(:albums).first
ensure ensure
ActiveRecord::Base.connection.execute "DROP SCHEMA music CASCADE;" ActiveRecord::Base.connection.drop_schema "music", if_exists: true
end end
def test_raise_drop_schema_with_nonexisting_schema def test_drop_schema_with_nonexisting_schema
assert_raises(ActiveRecord::StatementInvalid) do 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
end end
@ -462,14 +473,14 @@ class SchemaForeignKeyTest < ActiveRecord::PostgreSQLTestCase
ensure ensure
@connection.drop_table "wagons", if_exists: true @connection.drop_table "wagons", if_exists: true
@connection.drop_table "my_schema.trains", 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
end end
class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCase class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCase
setup do setup do
@connection = ActiveRecord::Base.connection @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 SCHEMA schema_1"
@connection.execute "CREATE DOMAIN schema_1.text AS text" @connection.execute "CREATE DOMAIN schema_1.text AS text"
@connection.execute "CREATE DOMAIN schema_1.varchar AS varchar" @connection.execute "CREATE DOMAIN schema_1.varchar AS varchar"
@ -487,7 +498,7 @@ class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCa
teardown do teardown do
@connection.schema_search_path = @old_search_path @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 Default.reset_column_information
end end