Skip to content

Commit

Permalink
Merge pull request #353 from yiyenene/treat_index_option
Browse files Browse the repository at this point in the history
Add supports index options when ordinary column definition.
  • Loading branch information
winebarrel committed Mar 13, 2021
2 parents 1670044 + e2c5dcf commit 62ba47d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/ridgepole/dsl_parser/table_definition.rb
Expand Up @@ -13,11 +13,14 @@ def initialize(table_name, base)

def column(name, type, options = {})
name = name.to_s
index_options = options.key?(:index) ? options.delete(:index) : false

@__definition[name] = {
type: type,
options: options,
}

index(name, index_options.is_a?(Hash) ? index_options : {}) if index_options
end

DEFAULT_PRIMARY_KEY_TYPE = :bigint
Expand Down
60 changes: 60 additions & 0 deletions spec/mysql/migrate/migrate_create_index_spec.rb
Expand Up @@ -157,4 +157,64 @@
expect(subject.dump).to match_ruby expected_dsl
}
end

context 'when using index option' do
let(:dsl) do
erbh(<<-ERB)
create_table "clubs", force: :cascade do |t|
t.string "name", default: "", null: false, index: { unique: true }
end
create_table "titles", id: false, force: :cascade do |t|
t.integer "emp_no", null: false, index: { name: "emp_no" }
t.string "title", limit: 50, null: false
t.date "from_date", null: false
t.date "to_date"
end
ERB
end

let(:actual_dsl) do
erbh(<<-ERB)
create_table "clubs", force: :cascade do |t|
t.string "name", default: "", null: false
end
create_table "titles", id: false, force: :cascade do |t|
t.integer "emp_no", null: false
t.string "title", limit: 50, null: false
t.date "from_date", null: false
t.date "to_date"
end
ERB
end

let(:expected_dsl) do
erbh(<<-ERB)
create_table "clubs", force: :cascade do |t|
t.string "name", default: "", null: false
t.index ["name"], name: "index_clubs_on_name", unique: true
end
create_table "titles", id: false, force: :cascade do |t|
t.integer "emp_no", null: false
t.string "title", limit: 50, null: false
t.date "from_date", null: false
t.date "to_date"
t.index ["emp_no"], name: "emp_no"
end
ERB
end

before { subject.diff(actual_dsl).migrate }
subject { client }

it {
delta = subject.diff(dsl)
expect(delta.differ?).to be_truthy
expect(subject.dump).to match_ruby actual_dsl
delta.migrate
expect(subject.dump).to match_ruby expected_dsl
}
end
end

0 comments on commit 62ba47d

Please sign in to comment.