From e2c5dcf5dc236aa440e034b5e6dfa4ce45d23d88 Mon Sep 17 00:00:00 2001 From: yiyenene Date: Tue, 9 Mar 2021 00:51:06 +0900 Subject: [PATCH] add support index option in column method --- lib/ridgepole/dsl_parser/table_definition.rb | 3 + .../migrate/migrate_create_index_spec.rb | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lib/ridgepole/dsl_parser/table_definition.rb b/lib/ridgepole/dsl_parser/table_definition.rb index 55870114..f84531cc 100644 --- a/lib/ridgepole/dsl_parser/table_definition.rb +++ b/lib/ridgepole/dsl_parser/table_definition.rb @@ -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 diff --git a/spec/mysql/migrate/migrate_create_index_spec.rb b/spec/mysql/migrate/migrate_create_index_spec.rb index 187fd516..01edc8f0 100644 --- a/spec/mysql/migrate/migrate_create_index_spec.rb +++ b/spec/mysql/migrate/migrate_create_index_spec.rb @@ -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