Skip to content

Commit 113f7aa

Browse files
author
adrianryt
committed
Update tables after migrating translatable data
1 parent 8af9726 commit 113f7aa

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

core/db/migrate/20220804073928_transfer_data_to_translatable_tables.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# This migration comes from spree (originally 20220804073928)
21
class TransferDataToTranslatableTables < ActiveRecord::Migration[7.0]
32
DEFAULT_LOCALE = 'en'
43
PRODUCTS_TABLE = 'spree_products'
@@ -10,19 +9,56 @@ def up
109
# Products
1110
ActiveRecord::Base.connection.execute("
1211
INSERT INTO #{PRODUCT_TRANSLATIONS_TABLE} (name, description, locale, spree_product_id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug)
13-
SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug FROM #{PRODUCTS_TABLE}
12+
SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug FROM #{PRODUCTS_TABLE}
13+
")
14+
ActiveRecord::Base.connection.execute("
15+
UPDATE #{PRODUCTS_TABLE}
16+
SET name=null, description=null, meta_description=null, meta_keywords=null, meta_title=null, slug=null;
1417
")
1518
#Taxons
1619
ActiveRecord::Base.connection.execute("
1720
INSERT INTO #{TAXON_TRANSLATIONS_TABLE} (name, description, locale, spree_taxon_id, created_at, updated_at)
18-
SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at FROM #{TAXONS_TABLE}
21+
SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at FROM #{TAXONS_TABLE}
22+
")
23+
ActiveRecord::Base.connection.execute("
24+
UPDATE #{TAXONS_TABLE}
25+
SET name=null, description=null
1926
")
2027
end
2128

2229
def down
30+
ActiveRecord::Base.connection.execute("
31+
UPDATE #{PRODUCTS_TABLE} as products
32+
SET (name,
33+
description,
34+
meta_description,
35+
meta_keywords,
36+
meta_title,
37+
slug) =
38+
(t_products.name,
39+
t_products.description,
40+
t_products.meta_description,
41+
t_products.meta_keywords,
42+
t_products.meta_title,
43+
t_products.slug)
44+
FROM #{PRODUCT_TRANSLATIONS_TABLE} AS t_products
45+
WHERE t_products.spree_product_id = products.id
46+
")
47+
2348
ActiveRecord::Base.connection.execute("
2449
TRUNCATE TABLE #{PRODUCT_TRANSLATIONS_TABLE}
2550
")
51+
52+
ActiveRecord::Base.connection.execute("
53+
UPDATE #{TAXONS_TABLE} as taxons
54+
SET (name,
55+
description) =
56+
(t_taxons.name,
57+
t_taxons.description)
58+
FROM #{TAXON_TRANSLATIONS_TABLE} AS t_taxons
59+
WHERE t_taxons.spree_taxon_id = taxons.id
60+
")
61+
2662
ActiveRecord::Base.connection.execute("
2763
TRUNCATE TABLE #{TAXON_TRANSLATIONS_TABLE}
2864
")

0 commit comments

Comments
 (0)