Skip to content

Commit

Permalink
Add if-exists clause to drop table (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidepaolotua authored May 22, 2024
1 parent c12d506 commit af42b94
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion spec/avram/migrator/drop_table_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ describe Avram::Migrator::DropTableStatement do
it "can drop table" do
statement = Avram::Migrator::DropTableStatement.new(:users).build

statement.should eq "DROP TABLE users"
statement.should eq "DROP TABLE users;"
end

context "IF EXISTS" do
it "adds the option to the table" do
statement = Avram::Migrator::DropTableStatement.new(:users, if_exists: true).build
statement.should eq "DROP TABLE IF EXISTS users;"
end
end
end
4 changes: 2 additions & 2 deletions src/avram/migrator/drop_table_statement.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Avram::Migrator::DropTableStatement
def initialize(@table_name : TableName)
def initialize(@table_name : TableName, @if_exists : Bool = false)
end

def build
"DROP TABLE #{@table_name}"
"DROP TABLE #{@if_exists ? "IF EXISTS " : ""}#{@table_name};"
end
end
4 changes: 2 additions & 2 deletions src/avram/migrator/statement_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module Avram::Migrator::StatementHelpers
end
end

def drop(table_name)
prepared_statements << Avram::Migrator::DropTableStatement.new(table_name).build
def drop(table_name, *, if_exists = false)
prepared_statements << Avram::Migrator::DropTableStatement.new(table_name, if_exists).build
end

macro alter(table_name, *, if_exists = false)
Expand Down

0 comments on commit af42b94

Please sign in to comment.