forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add name attribute to Spree::Address model
Add name string field to Spree::Address and backfill it concatenating existing firstname and lastname fileds content. Update address db sample to be consistent.
- Loading branch information
1 parent
6aa7436
commit c2bb23b
Showing
6 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
core/db/migrate/20191121122841_add_name_to_spree_addresses.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddNameToSpreeAddresses < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :spree_addresses, :name, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
class Spree::AddressForMigration < ActiveRecord::Base | ||
self.table_name = 'spree_addresses' | ||
end | ||
|
||
namespace 'solidus:migrations:migrate_address_names' do | ||
desc 'Backfills Spree::Address name attribute using firstname and lastname | ||
concatenation to keep historical data when upgrading to new address name | ||
format' | ||
task up: :environment do | ||
with_log do | ||
update_all(update_statement) | ||
if !ActiveRecord::Base.connection.index_exists?(:spree_addresses, :name) | ||
ActiveRecord::Base.connection.add_index(:spree_addresses, :name) | ||
end | ||
end | ||
end | ||
|
||
desc 'Reverts Spree::Address name attribute backfill' | ||
task down: :environment do | ||
with_log do | ||
if ActiveRecord::Base.connection.index_exists?(:spree_addresses, :name) | ||
ActiveRecord::Base.connection.remove_index(:spree_addresses, :name) | ||
end | ||
update_all('name = NULL') | ||
end | ||
end | ||
|
||
private | ||
|
||
def update_all(statement) | ||
Spree::AddressForMigration.in_batches do |addresses| | ||
addresses.update_all(statement) | ||
end | ||
end | ||
|
||
def update_statement | ||
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with?('sqlite') | ||
"name=(firstname || ' ' || lastname)" | ||
else | ||
"name=CONCAT(firstname, ' ', lastname)" | ||
end | ||
end | ||
|
||
def with_log | ||
puts "Updating #{Spree::Address.count} addresses" | ||
yield | ||
puts 'Addresses updated' | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
core/spec/lib/tasks/migrations/migrate_address_names_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
path = Spree::Core::Engine.root.join('lib/tasks/migrations/migrate_address_names.rake') | ||
|
||
RSpec.describe 'solidus:migrations:migrate_address_names' do | ||
describe 'up' do | ||
include_context( | ||
'rake', | ||
task_path: path, | ||
task_name: 'solidus:migrations:migrate_address_names:up' | ||
) | ||
|
||
it 'migrates name data' do | ||
address = create(:address, firstname: 'Jane', lastname: 'Von Doe') | ||
|
||
expect { task.invoke }.to output( | ||
"Updating 1 addresses\nAddresses updated\n" | ||
).to_stdout | ||
expect(address.reload.name).to eq('Jane Von Doe') | ||
end | ||
end | ||
|
||
describe 'down' do | ||
include_context( | ||
'rake', | ||
task_path: path, | ||
task_name: 'solidus:migrations:migrate_address_names:down' | ||
) | ||
|
||
it 'rollbacks name data migration' do | ||
address = create(:address, name: 'Jane') | ||
|
||
expect { task.invoke }.to output( | ||
"Updating 1 addresses\nAddresses updated\n" | ||
).to_stdout | ||
expect(address.reload.name).to be_nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters