-
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 create_sequence and drop_sequence migration helpers. Fixes #910
- Loading branch information
Showing
6 changed files
with
104 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,14 @@ | ||
require "../../spec_helper" | ||
|
||
describe Avram::Migrator::CreateSequenceStatement do | ||
it "generates correct CREATE SEQUENCE sql" do | ||
statement = Avram::Migrator::CreateSequenceStatement.new(:accounts_number).build | ||
statement.should eq "CREATE SEQUENCE accounts_number_seq OWNED BY NONE;" | ||
|
||
statement = Avram::Migrator::CreateSequenceStatement.new(:accounts_number, if_not_exists: true).build | ||
statement.should eq "CREATE SEQUENCE IF NOT EXISTS accounts_number_seq OWNED BY NONE;" | ||
|
||
statement = Avram::Migrator::CreateSequenceStatement.new(:accounts_number, owned_by: "accounts.number").build | ||
statement.should eq "CREATE SEQUENCE accounts_number_seq OWNED BY accounts.number;" | ||
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::DropSequenceStatement do | ||
it "generates correct DROP SEQUENCE sql" do | ||
statement = Avram::Migrator::DropSequenceStatement.new(:accounts_number).build | ||
statement.should eq "DROP SEQUENCE IF EXISTS accounts_number_seq;" | ||
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
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,26 @@ | ||
# Builds an SQL statement for creating a sequence using the given name appending "_seq". | ||
# Additional options may be provided for extra customization. | ||
# | ||
# ### Usage | ||
# | ||
# ``` | ||
# CreateSequenceStatement.new(:accounts_number, if_not_exists: true, owned_by: "accounts.number").build | ||
# # => "CREATE SEQUENCE accounts_number_seq;" | ||
# ``` | ||
class Avram::Migrator::CreateSequenceStatement | ||
private getter? if_not_exists : Bool = false | ||
|
||
def initialize(@name : String | Symbol, *, @if_not_exists : Bool = false, @owned_by : String = "NONE") | ||
end | ||
|
||
def build | ||
name = "#{@name}_seq" | ||
|
||
String.build do |io| | ||
io << "CREATE SEQUENCE " | ||
io << "IF NOT EXISTS " if if_not_exists? | ||
io << name | ||
io << " OWNED BY #{@owned_by};" | ||
end | ||
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,22 @@ | ||
# Builds an SQL statement for dropping a sequence using the given name. | ||
# | ||
# ### Usage | ||
# | ||
# ``` | ||
# DropSequenceStatement.new(:accounts_number, if_not_exists: true, owned_by: "accounts.number").build | ||
# # => "CREATE SEQUENCE accounts_number_seq;" | ||
# ``` | ||
class Avram::Migrator::DropSequenceStatement | ||
def initialize(@name : String | Symbol) | ||
end | ||
|
||
def build | ||
name = "#{@name}_seq" | ||
|
||
String.build do |io| | ||
io << "DROP SEQUENCE IF EXISTS " | ||
io << name | ||
io << ';' | ||
end | ||
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