Skip to content

Commit

Permalink
Fix index: true on migration to be able to handle index: "gin" or "gist"
Browse files Browse the repository at this point in the history
  • Loading branch information
Yacine Petitprez committed Jul 5, 2018
1 parent f887a4f commit db8672d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 5 additions & 3 deletions spec/migration/migration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module MigrationSpec
t.string :first_name, index: true
t.string :last_name, unique: true

t.string :tags, array: true, index: "gin"

t.index "lower(first_name || ' ' || last_name)", using: :btree

t.timestamps
Expand Down Expand Up @@ -51,18 +53,18 @@ module MigrationSpec
columns.dup.where { column_name == "first_name" }.any?.should eq true
columns.dup.where { column_name == "last_name" }.any?.should eq true

table.indexes.size.should eq 5
table.indexes.size.should eq 6

Migration2.new.apply(Clear::Migration::Direction::UP)
columns = table.columns
columns.dup.where { column_name == "middle_name" }.any?.should eq true
table.indexes.size.should eq 6
table.indexes.size.should eq 7

# Revert the last migration
Migration2.new.apply(Clear::Migration::Direction::DOWN)
columns = table.columns
columns.dup.where { column_name == "middle_name" }.any?.should eq false
table.indexes.size.should eq 5
table.indexes.size.should eq 6

# Revert the table migration
Migration1.new.apply(Clear::Migration::Direction::DOWN)
Expand Down
18 changes: 11 additions & 7 deletions src/clear/migration/operation/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Clear::Migration
# Helper to create or alter table.
struct Table < Operation
record ColumnOperation, column : String, type : String,
null : Bool = false, default : SQL::Any = nil, primary : Bool = false
null : Bool = false, default : SQL::Any = nil, primary : Bool = false,
array : Bool = false

record IndexOperation, field : String, name : String,
using : String? = nil, unique : Bool = false
Expand Down Expand Up @@ -47,14 +48,19 @@ module Clear::Migration
end

# Add/alter a column for this table.
def add_column(column, type, default = nil, null = true, primary = false, index = false, unique = false)
def add_column(column, type, default = nil, null = true, primary = false,
index = false, unique = false, array = false)
self.column_operations << ColumnOperation.new(column: column.to_s, type: type.to_s,
default: default, null: null, primary: primary)
default: default, null: null, primary: primary, array: array)

if unique
add_index(field: column, unique: true)
elsif index
add_index(field: column, unique: false)
if index.is_a?(Bool)
add_index(field: column, unique: false)
else
add_index(field: column, unique: false, using: index)
end
end
end

Expand Down Expand Up @@ -128,7 +134,7 @@ module Clear::Migration
private def print_columns
column_operations.map do |x|
[x.column,
x.type,
x.type + (x.array ? "[]" : ""),
x.null ? nil : "NOT NULL",
!x.default.nil? ? "DEFAULT #{x.default}" : nil,
x.primary ? "PRIMARY KEY" : nil]
Expand All @@ -151,8 +157,6 @@ module Clear::Migration
"bigint"
when "datetime"
"timestamp without time zone"
when "datetimetz"
"timestamp with time zone"
else
type
end
Expand Down

0 comments on commit db8672d

Please sign in to comment.