Skip to content

Commit

Permalink
create_table if_not_exists option should prevent drop table
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonphillips committed Feb 13, 2024
1 parent 36c1591 commit 48fbdfa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Expand Up @@ -296,7 +296,7 @@ def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **o
td = build_create_table_definition(table_name, id: id, primary_key: primary_key, force: force, **options, &block)

if force
drop_table(table_name, force: force, if_exists: true)
drop_table(table_name, force: force, if_exists: true) unless td.if_not_exists
else
schema_cache.clear_data_source_cache!(table_name.to_s)
end
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -193,6 +193,15 @@ def test_create_table_with_if_not_exists_true
t.string :foo
end
end

# Do not drop table if existence guard option is present
assert_nothing_raised do
record = Person.create(foo: 'Hello, World')
connection.create_table :testings, force: true, if_not_exists: true do |t|
t.string :foo
end
record.reload
end
ensure
connection.drop_table :testings, if_exists: true
end
Expand Down

0 comments on commit 48fbdfa

Please sign in to comment.