Skip to content

Commit

Permalink
Add name attribute to Spree::Address model
Browse files Browse the repository at this point in the history
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
filippoliverani committed Dec 2, 2019
1 parent 6aa7436 commit c2bb23b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
7 changes: 7 additions & 0 deletions core/db/migrate/20191121122841_add_name_to_spree_addresses.rb
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Rake task spec setup.
#
RSpec.shared_context "rake" do |task_path:, task_name:|
RSpec.shared_context 'rake' do |task_path:, task_name:|
require 'rake'

let(:task) do
Expand Down
51 changes: 51 additions & 0 deletions core/lib/tasks/migrations/migrate_address_names.rake
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 core/spec/lib/tasks/migrations/migrate_address_names_spec.rb
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
2 changes: 1 addition & 1 deletion core/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

require 'spree/testing_support/factories'
require 'spree/testing_support/preferences'
require 'spree/testing_support/rake'
require 'spree/testing_support/shared_contexts/rake'
require 'cancan/matchers'

ActiveJob::Base.queue_adapter = :test
Expand Down
8 changes: 6 additions & 2 deletions sample/db/samples/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
"(662)877-7894 x703", "689.578.8564 x72399"]

2.times do
first_name = first_names.sample
last_name = last_names.sample
name = "#{first_name} #{last_name}"
Spree::Address.create!(
firstname: first_names.sample,
lastname: last_names.sample,
name: name,
firstname: first_name,
lastname: last_name,
address1: street_addresses.sample,
address2: secondary_addresses.sample,
city: cities.sample,
Expand Down

0 comments on commit c2bb23b

Please sign in to comment.