Skip to content

Commit

Permalink
Merge pull request #354 from pocari/fix-mysql-convert-integer-by-limit
Browse files Browse the repository at this point in the history
Fixed conversion process from t.integer to t.bigint in mysql
  • Loading branch information
winebarrel committed Apr 12, 2021
2 parents 1b825b1 + 6b95f03 commit cdb18a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/ridgepole/diff.rb
Expand Up @@ -399,9 +399,13 @@ def normalize_column_options!(attrs, primary_key = false)
if Ridgepole::ConnectionAdapters.mysql?
opts[:unsigned] = false unless opts.key?(:unsigned)

if (attrs[:type] == :integer) && (opts[:limit] == Ridgepole::DefaultsLimit.default_limit(:bigint, @options))
attrs[:type] = :bigint
opts.delete(:limit)
if attrs[:type] == :integer && opts[:limit]
min = Ridgepole::DefaultsLimit.default_limit(:integer, @options)
max = Ridgepole::DefaultsLimit.default_limit(:bigint, @options)
if min < opts[:limit] && opts[:limit] <= max
attrs[:type] = :bigint
opts.delete(:limit)
end
end

if opts[:size] && (attrs[:type] == :text || attrs[:type] == :blob || attrs[:type] == :binary)
Expand Down
13 changes: 11 additions & 2 deletions spec/mysql/migrate/migrate_change_column7_spec.rb
Expand Up @@ -5,7 +5,11 @@
let(:dsl) do
erbh(<<-ERB)
create_table "salaries", id: false, force: :cascade do |t|
t.integer "emp_no", limit: 8, null: false
t.integer "emp_no_int", limit: 4, null: false
t.integer "emp_no_bigint5", limit: 5, null: false
t.integer "emp_no_bigint6", limit: 6, null: false
t.integer "emp_no_bigint7", limit: 7, null: false
t.integer "emp_no_bigint8", limit: 8, null: false
t.float "salary", <%= i cond('< 5.2.0.beta2', limit: 24) %>, null: false
t.date "from_date", null: false
t.date "to_date", null: false
Expand All @@ -17,7 +21,12 @@
subject { client }

it {
expect(subject.dump).to match_ruby dsl.sub(/t.integer "emp_no", limit: 8/, 't.bigint "emp_no"')
expect(subject.dump).to match_ruby dsl
.sub(/t.integer "emp_no_int", limit: 4/, 't.integer "emp_no_int"')
.sub(/t.integer "emp_no_bigint5", limit: 5/, 't.bigint "emp_no_bigint5"')
.sub(/t.integer "emp_no_bigint6", limit: 6/, 't.bigint "emp_no_bigint6"')
.sub(/t.integer "emp_no_bigint7", limit: 7/, 't.bigint "emp_no_bigint7"')
.sub(/t.integer "emp_no_bigint8", limit: 8/, 't.bigint "emp_no_bigint8"')
delta = subject.diff(dsl)
expect(delta.differ?).to be_falsey
}
Expand Down

0 comments on commit cdb18a7

Please sign in to comment.