Skip to content

Commit

Permalink
Allow drop_table to accept an array of table names
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Sep 23, 2024
1 parent 3194885 commit 55622a6
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ def create_table(table_name, **options)
res
end

def drop_table(table_name, **options)
# Mimic CASCADE option as best we can.
if options[:force] == :cascade
execute_procedure(:sp_fkeys, pktable_name: table_name).each do |fkdata|
fktable = fkdata["FKTABLE_NAME"]
fkcolmn = fkdata["FKCOLUMN_NAME"]
pktable = fkdata["PKTABLE_NAME"]
pkcolmn = fkdata["PKCOLUMN_NAME"]
remove_foreign_key fktable, name: fkdata["FK_NAME"]
execute "DELETE FROM #{quote_table_name(fktable)} WHERE #{quote_column_name(fkcolmn)} IN ( SELECT #{quote_column_name(pkcolmn)} FROM #{quote_table_name(pktable)} )"
def drop_table(*table_names, **options)
table_names.each do |table_name|
# Mimic CASCADE option as best we can.
if options[:force] == :cascade
execute_procedure(:sp_fkeys, pktable_name: table_name).each do |fkdata|
fktable = fkdata["FKTABLE_NAME"]
fkcolmn = fkdata["FKCOLUMN_NAME"]
pktable = fkdata["PKTABLE_NAME"]
pkcolmn = fkdata["PKCOLUMN_NAME"]
remove_foreign_key fktable, name: fkdata["FK_NAME"]
execute "DELETE FROM #{quote_table_name(fktable)} WHERE #{quote_column_name(fkcolmn)} IN ( SELECT #{quote_column_name(pkcolmn)} FROM #{quote_table_name(pktable)} )"
end
end
if options[:if_exists] && version_year < 2016
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}", "SCHEMA"
else
super
end
end
if options[:if_exists] && version_year < 2016
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}", "SCHEMA"
else
super
end
end

Expand Down

0 comments on commit 55622a6

Please sign in to comment.