Skip to content

Commit

Permalink
Merge da5803a into 2adcb4d
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Jan 4, 2021
2 parents 2adcb4d + da5803a commit 9e6443b
Show file tree
Hide file tree
Showing 101 changed files with 1,016 additions and 1,081 deletions.
2 changes: 0 additions & 2 deletions bin/ridgepole
Expand Up @@ -147,8 +147,6 @@ ARGV.options do |opt|
opt.on('', '--create-table-with-index') { options[:create_table_with_index] = true }

opt.on('', '--mysql-dump-auto-increment') do
raise OptionParser::InvalidOption, '`mysql-dump-auto-increment` is not available in `activerecord < 5.1`' if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('5.1')

options[:mysql_dump_auto_increment] = true
end

Expand Down
18 changes: 7 additions & 11 deletions lib/ridgepole/diff.rb
Expand Up @@ -159,7 +159,7 @@ def scan_options_change(table_name, from, to, table_delta)
to.delete(:collation)
end

pk_attrs = build_primary_key_attrs_if_changed(from, to, table_name)
pk_attrs = build_primary_key_attrs_if_changed(from, to)
if pk_attrs
if @options[:allow_pk_change]
if from[:id] == false
Expand Down Expand Up @@ -207,20 +207,20 @@ def convert_to_primary_key_attrs(column_options)
{ type: type, options: options }
end

def build_attrs_if_changed(to_attrs, from_attrs, table_name, primary_key: false)
def build_attrs_if_changed(to_attrs, from_attrs, primary_key: false)
normalize_column_options!(from_attrs, primary_key)
normalize_column_options!(to_attrs, primary_key)

new_to_attrs = fix_change_column_options(table_name, from_attrs, to_attrs) unless compare_column_attrs(from_attrs, to_attrs)
new_to_attrs = fix_change_column_options(from_attrs, to_attrs) unless compare_column_attrs(from_attrs, to_attrs)
new_to_attrs
end

def build_primary_key_attrs_if_changed(from, to, table_name)
def build_primary_key_attrs_if_changed(from, to)
from_column_attrs = convert_to_primary_key_attrs(from.slice(*PRIMARY_KEY_OPTIONS))
to_column_attrs = convert_to_primary_key_attrs(to.slice(*PRIMARY_KEY_OPTIONS))
return if from_column_attrs == to_column_attrs

build_attrs_if_changed(to_column_attrs, from_column_attrs, table_name, primary_key: true)
build_attrs_if_changed(to_column_attrs, from_column_attrs, primary_key: true)
end

def scan_definition_change(from, to, from_indices, table_name, table_options, table_delta)
Expand All @@ -242,7 +242,7 @@ def scan_definition_change(from, to, from_indices, table_name, table_options, ta
next if ignore_column

if from_attrs
to_attrs = build_attrs_if_changed(to_attrs, from_attrs, table_name)
to_attrs = build_attrs_if_changed(to_attrs, from_attrs)
if to_attrs
definition_delta[:change] ||= {}
definition_delta[:change][column_name] = to_attrs
Expand Down Expand Up @@ -471,7 +471,7 @@ def scan_foreign_keys_change(from, to, table_delta, options)
# XXX: MySQL only?
# https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L760
# https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb#L102
def fix_change_column_options(table_name, from_attrs, to_attrs)
def fix_change_column_options(from_attrs, to_attrs)
# default: 0, null: false -> default: nil, null: false | default: nil
# default: 0, null: false -> null: false | default: nil
# default: 0, null: false -> default: nil, null: true | default: nil, null: true
Expand All @@ -486,10 +486,6 @@ def fix_change_column_options(table_name, from_attrs, to_attrs)
to_attrs[:options].delete(:null)
end

if Ridgepole::ConnectionAdapters.mysql? && ActiveRecord::VERSION::STRING.start_with?('5.0.')
Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}`: `default: nil` is ignored when `null: false`. Please apply twice") if to_attrs[:options][:default].nil? && (to_attrs[:options][:null] == false)
end

to_attrs
end

Expand Down
47 changes: 0 additions & 47 deletions spec/mysql/bigint_pk/bigint_pk_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/mysql/bigint_pk/int_pk_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe 'Ridgepole::Client (with integer pk)', condition: '>= 5.1.0' do
describe 'Ridgepole::Client (with integer pk)' do
context 'when with id:integer' do
let(:dsl) do
<<-RUBY
Expand Down
28 changes: 14 additions & 14 deletions spec/mysql/collation/collation_spec.rb
Expand Up @@ -4,22 +4,22 @@
context 'when change column (add collation)' do
let(:actual_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false
t.text "text", null: false
end
ERB
end

let(:expected_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, collation: "utf8mb4_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
ERB
end
Expand All @@ -39,22 +39,22 @@
context 'when change column (delete collation)' do
let(:actual_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, collation: "utf8mb4_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
ERB
end

let(:expected_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false
t.text "text", null: false
end
ERB
end
Expand All @@ -74,22 +74,22 @@
context 'when change column (change collation)' do
let(:actual_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, collation: "utf8mb4_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
ERB
end

let(:expected_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "utf8mb4_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, collation: "ascii_bin"
t.text "text", null: false, collation: "ascii_bin"
end
ERB
end
Expand All @@ -109,11 +109,11 @@
context 'when change column (no change collation)' do
let(:actual_dsl) do
erbh(<<-ERB)
create_table "employee_clubs", <%= i cond({ ">= 5.1, < 6.1" => { id: :bigint, unsigned: true }, ">= 6.1" => { id: { type: :bigint, unsigned: true } } }, { unsigned: true }) %>, force: :cascade do |t|
create_table "employee_clubs", <%= i cond("< 6.1", { id: :bigint, unsigned: true }, { id: { type: :bigint, unsigned: true } }) %>, force: :cascade do |t|
t.integer "emp_no", null: false
t.integer "club_id", null: false, unsigned: true
t.string "string", null: false, collation: "ascii_bin"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, collation: "utf8mb4_bin"
t.text "text", null: false, collation: "utf8mb4_bin"
end
ERB
end
Expand Down
18 changes: 9 additions & 9 deletions spec/mysql/comment/comment_spec.rb
Expand Up @@ -8,7 +8,7 @@
t.integer "emp_no", null: false
t.integer "club_id", null: false
t.string "string", null: false
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false
t.text "text", null: false
end
ERB
end
Expand All @@ -19,7 +19,7 @@
t.integer "emp_no", null: false, comment: "any comment"
t.integer "club_id", null: false, comment: "any comment2"
t.string "string", null: false, comment: "any comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "any comment4"
t.text "text", null: false, comment: "any comment4"
end
ERB
end
Expand All @@ -43,7 +43,7 @@
t.integer "emp_no", null: false, comment: "any comment"
t.integer "club_id", null: false, comment: "any comment2"
t.string "string", null: false, comment: "any comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "any comment4"
t.text "text", null: false, comment: "any comment4"
end
ERB
end
Expand All @@ -54,7 +54,7 @@
t.integer "emp_no", null: false
t.integer "club_id", null: false
t.string "string", null: false
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false
t.text "text", null: false
end
ERB
end
Expand All @@ -78,7 +78,7 @@
t.integer "emp_no", null: false, comment: "any comment"
t.integer "club_id", null: false, comment: "any comment2"
t.string "string", null: false, comment: "any comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "any comment4"
t.text "text", null: false, comment: "any comment4"
end
ERB
end
Expand All @@ -89,7 +89,7 @@
t.integer "emp_no", null: false, comment: "other comment"
t.integer "club_id", null: false, comment: "other comment2"
t.string "string", null: false, comment: "other comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "other comment4"
t.text "text", null: false, comment: "other comment4"
end
ERB
end
Expand All @@ -113,7 +113,7 @@
t.integer "emp_no", null: false, comment: "any comment"
t.integer "club_id", null: false, comment: "any comment2"
t.string "string", null: false, comment: "any comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "any comment4"
t.text "text", null: false, comment: "any comment4"
end
ERB
end
Expand All @@ -137,7 +137,7 @@
t.integer "emp_no", null: false, comment: "other comment"
t.integer "club_id", null: false, comment: "other comment2"
t.string "string", null: false, comment: "other comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "other comment4"
t.text "text", null: false, comment: "other comment4"
end
ERB
end
Expand All @@ -160,7 +160,7 @@
t.integer "emp_no", null: false, comment: "other comment"
t.integer "club_id", null: false, comment: "other comment2"
t.string "string", null: false, comment: "other comment3"
t.text "text", <%= i cond(5.0, limit: 65535) %>, null: false, comment: "other comment4"
t.text "text", null: false, comment: "other comment4"
end
ERB
end
Expand Down
10 changes: 5 additions & 5 deletions spec/mysql/diff/diff2_spec.rb
Expand Up @@ -174,12 +174,12 @@
it {
delta = subject.diff(actual_dsl, expected_dsl)
expect(delta.differ?).to be_truthy
expect(delta.script).to match_ruby erbh(<<-ERB)
change_column("employee_clubs", "club_id", :integer, **<%= {:unsigned=>false, :null=>true, :default=>nil} + cond('>= 5.1', comment: nil) %>)
expect(delta.script).to match_ruby(<<-RUBY)
change_column("employee_clubs", "club_id", :integer, **{:unsigned=>false, :null=>true, :default=>nil, :comment=>nil})
change_column("employees", "last_name", :string, **<%= {:limit=>20, :default=>"XXX", :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
change_column("employees", "gender", :string, **<%= {:limit=>2, :null=>false, :default=>nil, :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
ERB
change_column("employees", "last_name", :string, **{:limit=>20, :default=>"XXX", :unsigned=>false, :comment=>nil})
change_column("employees", "gender", :string, **{:limit=>2, :null=>false, :default=>nil, :unsigned=>false, :comment=>nil})
RUBY
}

after do
Expand Down
6 changes: 3 additions & 3 deletions spec/mysql/diff/diff_spec.rb
Expand Up @@ -149,10 +149,10 @@
delta = subject.diff(actual_dsl, expected_dsl)
expect(delta.differ?).to be_truthy
expect(delta.script).to match_ruby erbh(<<-ERB)
change_column("employee_clubs", "club_id", :integer, **<%= {:unsigned=>false, :null=>true, :default=>nil} + cond('>= 5.1', comment: nil) %>)
change_column("employee_clubs", "club_id", :integer, **{:unsigned=>false, :null=>true, :default=>nil, :comment=>nil})
change_column("employees", "last_name", :string, **<%= {:limit=>20, :default=>"XXX", :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
change_column("employees", "gender", :string, **<%= {:limit=>2, :null=>false, :default=>nil, :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
change_column("employees", "last_name", :string, **{:limit=>20, :default=>"XXX", :unsigned=>false, :comment=>nil})
change_column("employees", "gender", :string, **{:limit=>2, :null=>false, :default=>nil, :unsigned=>false, :comment=>nil})
ERB
}
end
Expand Down

0 comments on commit 9e6443b

Please sign in to comment.