Skip to content

Commit

Permalink
Custom name for index
Browse files Browse the repository at this point in the history
  • Loading branch information
alexneisc committed Jun 10, 2020
1 parent d67dd63 commit 85d0bd5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
7 changes: 7 additions & 0 deletions spec/migrator/create_index_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ describe Avram::Migrator::CreateIndexStatement do
statement = Avram::Migrator::CreateIndexStatement.new(:users, columns: [:email, :username], using: :btree, unique: true).build
statement.should eq "CREATE UNIQUE INDEX users_email_username_index ON users USING btree (email, username);"
end

context "custom index name" do
it "generates correct CREATE INDEX sql with given name" do
statement = Avram::Migrator::CreateIndexStatement.new(:users, :email, name: :custom_index_name).build
statement.should eq "CREATE INDEX custom_index_name ON users USING btree (email);"
end
end
end
16 changes: 16 additions & 0 deletions spec/migrator/drop_index_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,20 @@ describe Avram::Migrator::DropIndexStatement do
statement = Avram::Migrator::DropIndexStatement.new(:users, [:email, :username], on_delete: :cascade, if_exists: true).build
statement.should eq "DROP INDEX IF EXISTS users_email_username_index CASCADE;"
end

context "custom index name" do
it "generates correct sql with given name" do
statement = Avram::Migrator::DropIndexStatement.new(:users, name: :custom_index_name).build
statement.should eq "DROP INDEX custom_index_name;"
end
end

context "without name and columns" do
it "raises Exception" do
message = Regex.new("No name or columns specified for drop_index")
expect_raises(Exception, message) do
statement = Avram::Migrator::DropIndexStatement.new(:users).build
end
end
end
end
7 changes: 5 additions & 2 deletions src/avram/migrator/create_index_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ class Avram::Migrator::CreateIndexStatement

ALLOWED_INDEX_TYPES = %w[btree]

def initialize(@table : Symbol, @columns : Columns, @using : Symbol = :btree, @unique = false)
def initialize(@table : Symbol, @columns : Columns, @using : Symbol = :btree, @unique = false, @name : String? | Symbol? = nil)
raise "index type '#{using}' not supported" unless ALLOWED_INDEX_TYPES.includes?(using.to_s)
end

def build
index_name = @name
index_name ||= "#{@table}_#{columns.join("_")}_index"

String.build do |index|
index << "CREATE"
index << " UNIQUE" if @unique
index << " INDEX #{@table}_#{columns.join("_")}_index"
index << " INDEX #{index_name}"
index << " ON #{@table}"
index << " USING #{@using}"
index << " (#{columns.join(", ")});"
Expand Down
14 changes: 12 additions & 2 deletions src/avram/migrator/drop_index_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class Avram::Migrator::DropIndexStatement

ALLOWED_ON_DELETE_STRATEGIES = %i[cascade restrict]

def initialize(@table : Symbol, @columns : Columns, @if_exists = false, @on_delete = :do_nothing)
def initialize(@table : Symbol, @columns : Columns | Nil = nil, @if_exists = false, @on_delete = :do_nothing, @name : String? | Symbol? = nil)
end

def build
String.build do |index|
index << "DROP INDEX"
index << " IF EXISTS" if @if_exists
index << " #{@table}_#{columns.join("_")}_index"
index << " #{index_name}"
index << on_delete_strategy(@on_delete)
end
end
Expand All @@ -53,4 +53,14 @@ class Avram::Migrator::DropIndexStatement
return [columns]
end
end

private def index_name
if @name
@name
elsif @columns
"#{@table}_#{columns.join("_")}_index"
else
raise "No name or columns specified for drop_index"
end
end
end
8 changes: 4 additions & 4 deletions src/avram/migrator/statement_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ module Avram::Migrator::StatementHelpers
prepared_statements << CreateForeignKeyStatement.new(from, to, on_delete, column, primary_key).build
end

def create_index(table_name : Symbol, columns : Columns, unique = false, using = :btree)
prepared_statements << CreateIndexStatement.new(table_name, columns, using, unique).build
def create_index(table_name : Symbol, columns : Columns, unique = false, using = :btree, name : String? | Symbol? = nil)
prepared_statements << CreateIndexStatement.new(table_name, columns, using, unique, name).build
end

def drop_index(table_name : Symbol, columns : Columns, if_exists = false, on_delete = :do_nothing)
prepared_statements << Avram::Migrator::DropIndexStatement.new(table_name, columns, if_exists, on_delete).build
def drop_index(table_name : Symbol, columns : Columns? = nil, if_exists = false, on_delete = :do_nothing, name : String? | Symbol? = nil)
prepared_statements << Avram::Migrator::DropIndexStatement.new(table_name, columns, if_exists, on_delete, name).build
end

def make_required(table : Symbol, column : Symbol)
Expand Down

0 comments on commit 85d0bd5

Please sign in to comment.