Skip to content

Commit

Permalink
PostgreSQL, add :if_exists to #drop_schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Aug 28, 2015
1 parent 9e2b13c commit dd118dc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def create_schema schema_name
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setup
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
Expand Down
31 changes: 21 additions & 10 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def setup
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
Expand Down Expand Up @@ -121,10 +121,17 @@ def test_drop_schema
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);
Expand All @@ -134,12 +141,16 @@ def test_habtm_table_name_with_schema
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

Expand Down Expand Up @@ -462,14 +473,14 @@ def test_dump_foreign_key_targeting_different_schema
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"
Expand All @@ -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

Expand Down

0 comments on commit dd118dc

Please sign in to comment.