Skip to content

Commit

Permalink
Merge 151763a into 080e78a
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Jun 30, 2018
2 parents 080e78a + 151763a commit d253349
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
* `>= 0.7.3`
* Add `--mysql-change-table-comment option` ([pull#166](https://github.com/winebarrel/ridgepole/pull/166))
* Refactoring with RuboCop
* Support primary key dropping ([issue#246](https://github.com/winebarrel/ridgepole/issues/246))
* Support primary key adding/dropping ([issue#246](https://github.com/winebarrel/ridgepole/issues/246))

## Installation

Expand Down
10 changes: 8 additions & 2 deletions lib/ridgepole/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ def scan_options_change(table_name, from, to, table_delta)
pk_attrs = build_primary_key_attrs_if_changed(from, to, table_name)
if pk_attrs
if @options[:allow_pk_change]
delta_type = pk_attrs[:options][:id] == false ? :delete : :change
if from[:id] == false
delta_type = :add
pk_attrs[:options][:primary_key] = true
else
delta_type = pk_attrs[:options][:id] == false ? :delete : :change
end

table_delta[:primary_key_definition] = { delta_type => { id: pk_attrs } }
else
@logger.warn(<<-MSG)
Expand Down Expand Up @@ -170,7 +176,7 @@ def convert_to_primary_key_attrs(column_options)
Ridgepole::DSLParser::TableDefinition::DEFAULT_PRIMARY_KEY_TYPE
end

if %i[integer bigint].include?(type) && !options.key?(:default)
if %i[integer bigint].include?(type) && !options.key?(:default) && !Ridgepole::ConnectionAdapters.postgresql?
options[:auto_increment] = true
end

Expand Down
71 changes: 71 additions & 0 deletions spec/postgresql/migrate/migrate_primary_key2_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
describe 'Ridgepole::Client#diff -> migrate', condition: '>= 5.1.0' do
let(:actual_dsl) do
erbh(<<-ERB)
create_table "employees", id: false, force: :cascade do |t|
t.string "name"
end
ERB
end

before do
subject.diff('').migrate
subject.diff(actual_dsl).migrate
end
subject { client(allow_pk_change: allow_pk_change) }

context 'when allow_pk_change option is false' do
let(:allow_pk_change) { false }

context 'with difference' do
let(:expected_dsl) do
erbh(<<-ERB)
create_table "employees", id: :bigint, force: :cascade do |t|
t.string "name"
end
ERB
end

it {
expect(Ridgepole::Logger.instance).to receive(:warn).with(<<-MSG)
[WARNING] Primary key definition of `employees` differ but `allow_pk_change` option is false
from: {:id=>false}
to: {:id=>:bigint}
MSG

delta = subject.diff(expected_dsl)
expect(delta.differ?).to be_falsey
delta.migrate
expect(subject.dump).to match_ruby actual_dsl
}
end

context 'with no difference' do
let(:expected_dsl) { actual_dsl }

it {
expect(Ridgepole::Logger.instance).to_not receive(:warn)

delta = subject.diff(expected_dsl)
expect(delta.differ?).to be_falsey
}
end
end

context 'when allow_pk_change option is true' do
let(:allow_pk_change) { true }
let(:expected_dsl) do
erbh(<<-ERB)
create_table "employees", id: :serial, force: :cascade do |t|
t.string "name"
end
ERB
end

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

0 comments on commit d253349

Please sign in to comment.