Skip to content

Commit

Permalink
Adding in extension support (#356)
Browse files Browse the repository at this point in the history
* Adding in extension support.

* Update src/avram/migrator/alter_extension_statement.cr

Co-Authored-By: Paul Smith <paulcsmith@users.noreply.github.com>

Co-authored-by: Paul Smith <paulcsmith@users.noreply.github.com>
  • Loading branch information
jwoertink and paulcsmith authored Apr 27, 2020
1 parent 27edded commit c160ddd
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
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';}
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

0 comments on commit c160ddd

Please sign in to comment.