Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add if-exists clause to drop table #1043

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading