diff --git a/spec/avram/migrator/drop_table_statement_spec.cr b/spec/avram/migrator/drop_table_statement_spec.cr index 8fd21d7a5..5b49d7934 100644 --- a/spec/avram/migrator/drop_table_statement_spec.cr +++ b/spec/avram/migrator/drop_table_statement_spec.cr @@ -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 diff --git a/src/avram/migrator/drop_table_statement.cr b/src/avram/migrator/drop_table_statement.cr index e6f1b5d1b..0bd54835b 100644 --- a/src/avram/migrator/drop_table_statement.cr +++ b/src/avram/migrator/drop_table_statement.cr @@ -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 diff --git a/src/avram/migrator/statement_helpers.cr b/src/avram/migrator/statement_helpers.cr index 605520e4c..ba1887387 100644 --- a/src/avram/migrator/statement_helpers.cr +++ b/src/avram/migrator/statement_helpers.cr @@ -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)