Skip to content

Commit

Permalink
Merge pull request #156 from Shopify/bb/change-column-defaults
Browse files Browse the repository at this point in the history
Support drop/set default in change_colum operations
  • Loading branch information
coding-chimp authored Mar 4, 2024
2 parents 4fd8436 + 71fea58 commit 5b92392
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
ruby: ["3.0", "3.1", "3.2", "head"]
mysql: ["5.7", "8.0"]
adapter: ["mysql2", "trilogy"]
exclude:
- activerecord: 6.1
ruby: head
- activerecord: 7.0
ruby: head

env:
BUNDLE_GEMFILE: "${{ github.workspace }}/gemfiles/activerecord_${{ matrix.activerecord }}.gemfile"
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

# 4.2.0 (Mar, 2024)
* Support `DROP DEFAULT` & `SET DEFAULT` in `change_column` operations.

# 4.1.1 (Nov, 2023)
* Fix check for warnings against PK in MySQL 8+

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lhm-shopify (4.1.1)
lhm-shopify (4.2.0)
retriable (>= 3.0.0)

GEM
Expand Down Expand Up @@ -38,7 +38,7 @@ GEM
ruby2_keywords
i18n (1.14.1)
concurrent-ruby (~> 1.0)
minitest (5.20.0)
minitest (5.22.2)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
mutex_m (0.1.2)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/activerecord_6.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
lhm-shopify (4.1.1)
lhm-shopify (4.2.0)
retriable (>= 3.0.0)

GEM
Expand Down Expand Up @@ -31,7 +31,7 @@ GEM
docile (1.4.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
minitest (5.20.0)
minitest (5.22.2)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
mysql2 (0.5.5)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/activerecord_7.0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
lhm-shopify (4.1.1)
lhm-shopify (4.2.0)
retriable (>= 3.0.0)

GEM
Expand Down Expand Up @@ -30,7 +30,7 @@ GEM
docile (1.4.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
minitest (5.20.0)
minitest (5.22.2)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
mysql2 (0.5.5)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/activerecord_7.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
lhm-shopify (4.1.1)
lhm-shopify (4.2.0)
retriable (>= 3.0.0)

GEM
Expand Down Expand Up @@ -38,7 +38,7 @@ GEM
ruby2_keywords
i18n (1.14.1)
concurrent-ruby (~> 1.0)
minitest (5.20.0)
minitest (5.22.2)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
mutex_m (0.1.2)
Expand Down
6 changes: 5 additions & 1 deletion lib/lhm/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def add_column(name, definition)
# @param [String] name Name of the column to change
# @param [String] definition Valid SQL column definition
def change_column(name, definition)
ddl('alter table `%s` modify column `%s` %s' % [@name, name, definition])
if definition.match?(/^(DROP|SET) DEFAULT/i)
ddl('alter table `%s` alter column `%s` %s' % [@name, name, definition])
else
ddl('alter table `%s` modify column `%s` %s' % [@name, name, definition])
end
end

# Rename an existing column.
Expand Down
2 changes: 1 addition & 1 deletion lib/lhm/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Schmidt

module Lhm
VERSION = '4.1.1'
VERSION = '4.2.0'
end
16 changes: 16 additions & 0 deletions spec/unit/migrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@
'alter table `lhmn_alt` modify column `logins` INT(11)'
])
end

it "should drop a default" do
@creator.change_column('foo', 'DROP DEFAULT')

value(@creator.statements).must_equal([
'alter table `lhmn_alt` alter column `foo` DROP DEFAULT'
])
end

it "should set a default" do
@creator.change_column('foo', "SET DEFAULT 'bar'")

value(@creator.statements).must_equal([
"alter table `lhmn_alt` alter column `foo` SET DEFAULT 'bar'"
])
end
end

describe 'direct changes' do
Expand Down

0 comments on commit 5b92392

Please sign in to comment.