-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding helpers for creating pg functions * Make it only String to save on some guess work. The function name may or may not contain args * adding helpers for dropping pg functions * forgot to pass the function return type down from statement helpers
- Loading branch information
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "../spec_helper" | ||
|
||
describe Avram::Migrator::CreateFunctionStatement do | ||
it "builds the proper SQL for creating a function" do | ||
sql = <<-SQL | ||
IF NEW.updated_at IS NULL OR NEW.updated_at = OLD.updated_at THEN | ||
NEW.updated_at := now(); | ||
END IF; | ||
RETURN NEW; | ||
SQL | ||
statement = Avram::Migrator::CreateFunctionStatement.new("set_updated_at", sql) | ||
|
||
full_statement = statement.build | ||
full_statement.should contain "CREATE OR REPLACE FUNCTION set_updated_at()" | ||
full_statement.should contain "RETURNS trigger" | ||
full_statement.should contain "IF NEW.updated_at IS NULL OR NEW.updated_at = OLD.updated_at THEN" | ||
end | ||
|
||
it "builds more complex functions" do | ||
sql = "RETURN i + 1;" | ||
statement = Avram::Migrator::CreateFunctionStatement.new("increment(i integer)", sql, returns: "integer") | ||
|
||
full_statement = statement.build | ||
full_statement.should contain "CREATE OR REPLACE FUNCTION increment(i integer)" | ||
full_statement.should contain "RETURNS integer" | ||
full_statement.should contain "RETURN i + 1;" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require "../spec_helper" | ||
|
||
describe Avram::Migrator::DropFunctionStatement do | ||
it "builds the proper SQL for dropping a function" do | ||
statement = Avram::Migrator::DropFunctionStatement.new("set_updated_at") | ||
statement.build.should eq %{DROP FUNCTION IF EXISTS "set_updated_at" CASCADE;} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Avram::Migrator::CreateFunctionStatement | ||
def initialize(@name : String, @body : String, @returns : String = "trigger") | ||
end | ||
|
||
def function_name | ||
if @name.ends_with?(')') | ||
@name | ||
else | ||
"#{@name}()" | ||
end | ||
end | ||
|
||
def build | ||
<<-SQL | ||
CREATE OR REPLACE FUNCTION #{function_name} | ||
RETURNS #{@returns} AS $$ | ||
BEGIN | ||
#{@body} | ||
END | ||
$$ LANGUAGE 'plpgsql'; | ||
SQL | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Avram::Migrator::DropFunctionStatement | ||
def initialize(@name : String) | ||
end | ||
|
||
def build | ||
<<-SQL | ||
DROP FUNCTION IF EXISTS "#{@name}" CASCADE; | ||
SQL | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters