Skip to content

Commit

Permalink
feat: support complex statements
Browse files Browse the repository at this point in the history
Co-authored-by: Vlad Faust <[email protected]>
  • Loading branch information
lucasintel and vladfaust committed Nov 23, 2018
1 parent f315a8e commit bfd8380
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ CREATE TABLE baz (
-- Indexes
CREATE UNIQUE INDEX baz_content_index ON baz (content);
-- Statements which might contain semicolons
-- +migrate start
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- +migrate end
-- +migrate down
DROP TABLE baz;
```
Expand Down
25 changes: 24 additions & 1 deletion spec/migrate/migration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ describe Migrate::Migration do
-- Indexes
CREATE UNIQUE INDEX foo_content_index ON foo (content);
-- Statements that might contain semicolons
-- +migrate start
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- +migrate end
-- +migrate down
DROP TABLE foo;
Expand All @@ -30,8 +42,19 @@ describe Migrate::Migration do
CREATE UNIQUE INDEX foo_content_index ON foo (content);
SQL

q3 = <<-SQL
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
SQL

it do
migration.queries_up.should eq [q1, q2]
migration.queries_up.should eq [q1, q2, q3]
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/migrations/10_create_baz.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@ CREATE TABLE baz (
-- Indexes
CREATE UNIQUE INDEX baz_content_index ON baz (content);

-- Statements that might contain semicolons
-- +migrate start
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- +migrate end

-- +migrate down
DROP TABLE baz;
11 changes: 10 additions & 1 deletion src/migrate/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Migrate

def initialize(lines : Iterator)
direction : Direction? = nil
complex = false
complex_end = false

buffer = String.new

Expand All @@ -22,6 +24,10 @@ module Migrate
direction = Direction::Up
when "down"
direction = Direction::Down
when "start"
complex = true
when "end"
complex_end = true
when "error"
message = /^#{Regex.escape(CMD_PREFIX)} error (?<message>.+)$/.match(line).try &.["message"]

Expand All @@ -37,7 +43,10 @@ module Migrate
buffer += (line + "\n") if direction
end

if !line.starts_with?("--") && line.strip.ends_with?(";")
if (line.strip.ends_with?(";") && !complex) || complex_end
complex_end = false
complex = false

case direction
when Direction::Up
@queries_up.push(buffer.dup.strip)
Expand Down

0 comments on commit bfd8380

Please sign in to comment.