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

Adding in extension support #356

Merged
merged 4 commits into from
Apr 27, 2020
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
13 changes: 13 additions & 0 deletions spec/migrator/alter_extension_statement_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "../spec_helper"

describe Avram::Migrator::AlterExtensionStatement do
it "builds the proper SQL to update an extension" do
statement = Avram::Migrator::AlterExtensionStatement.new("uuid-ossp")
statement.build.should eq %{ALTER EXTENSION "uuid-ossp" UPDATE;}
end

it "updates to a specific version" do
statement = Avram::Migrator::AlterExtensionStatement.new("hstore", to: "2.0")
statement.build.should eq %{ALTER EXTENSION "hstore" UPDATE TO '2.0';}
jwoertink marked this conversation as resolved.
Show resolved Hide resolved
end
end
8 changes: 8 additions & 0 deletions spec/migrator/create_extension_statement_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "../spec_helper"

describe Avram::Migrator::CreateExtensionStatement do
it "builds the proper SQL for creating an extension" do
statement = Avram::Migrator::CreateExtensionStatement.new("uuid-ossp")
statement.build.should eq %{CREATE EXTENSION IF NOT EXISTS "uuid-ossp";}
end
end
8 changes: 8 additions & 0 deletions spec/migrator/drop_extension_statement_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "../spec_helper"

describe Avram::Migrator::DropExtensionStatement do
it "builds the proper SQL for dropping an extension" do
statement = Avram::Migrator::DropExtensionStatement.new("uuid-ossp")
statement.build.should eq %{DROP EXTENSION IF EXISTS "uuid-ossp" CASCADE;}
end
end
16 changes: 16 additions & 0 deletions src/avram/migrator/alter_extension_statement.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Avram::Migrator::AlterExtensionStatement
def initialize(@name : String, @to : String? = nil)
end

def build
String.build do |sql|
sql << %{ALTER EXTENSION "#{@name}" UPDATE}
sql << to_version if @to
sql << ";"
end
end

def to_version
" TO '#{@to}'"
end
end
10 changes: 10 additions & 0 deletions src/avram/migrator/create_extension_statement.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Avram::Migrator::CreateExtensionStatement
def initialize(@name : String)
end

def build
<<-SQL
CREATE EXTENSION IF NOT EXISTS "#{@name}";
SQL
end
end
10 changes: 10 additions & 0 deletions src/avram/migrator/drop_extension_statement.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Avram::Migrator::DropExtensionStatement
def initialize(@name : String)
end

def build
<<-SQL
DROP EXTENSION IF EXISTS "#{@name}" CASCADE;
SQL
end
end
12 changes: 12 additions & 0 deletions src/avram/migrator/statement_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ module Avram::Migrator::StatementHelpers
def make_optional(table : Symbol, column : Symbol)
prepared_statements << Avram::Migrator::ChangeNullStatement.new(table, column, required: false).build
end

def enable_extension(name : String)
prepared_statements << Avram::Migrator::CreateExtensionStatement.new(name).build
end

def disable_extension(name : String)
prepared_statements << Avram::Migrator::DropExtensionStatement.new(name).build
end

def update_extension(name : String, to : String? = nil)
prepared_statements << Avram::Migrator::AlterExtensionStatement.new(name, to: to).build
end
end