Skip to content

Commit

Permalink
[OA-ADD] loyalty: migration (#398)
Browse files Browse the repository at this point in the history
* [OA-ADD] loyalty: migration

* [FIX] loyalty: fix migration

* [FIX] loyalty: pre migration
- fix SQL syntax

* [FIX] loyalty: re-fill data for program_type column

* [IMP] loyalty: format SQL query

* [IMP] loyalty: fill program_id column of table loyalty_rule

- fill reward_point_mode column to default value
- use reward_point_mode value insert loyalty_rule rows

* [FIX] loyalty: pre-commit syntax

* [FIX] loyalty: fix pre-migration script

* [FIX] loyalty: fill data before create SQL constraint

* add migration script

* correct seq

* fix

---------

Co-authored-by: nguyenvietlam0640 <[email protected]>
Co-authored-by: Roy Le <[email protected]>
  • Loading branch information
3 people authored Jul 29, 2023
1 parent d292641 commit f1781f9
Show file tree
Hide file tree
Showing 4 changed files with 830 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docsource/modules150-160.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Module coverage 15.0 -> 16.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| contacts |Nothing to do |No DB layout changes. |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |del| coupon | | |
| |del| coupon |Nothing to do |Merged into loyalty |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| crm |Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down Expand Up @@ -150,7 +150,7 @@ Module coverage 15.0 -> 16.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| gamification_sale_crm |Nothing to do | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |del| gift_card | | |
| |del| gift_card |Nothing to do |Merged into loyalty |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| google_account |Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down Expand Up @@ -454,7 +454,7 @@ Module coverage 15.0 -> 16.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| link_tracker |Nothing to do |No DB layout changes. |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |new| loyalty | | |
| |new| loyalty |Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |new| loyalty_delivery | | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
73 changes: 73 additions & 0 deletions openupgrade_scripts/scripts/loyalty/16.0.1.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from openupgradelib import openupgrade


def _move_gift_cart_to_loyalty_card(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE loyalty_card
ADD COLUMN IF NOT EXISTS old_gift_card_id INTEGER
""",
)
openupgrade.logged_query(
env.cr,
"""
WITH inserted_loyalty_program AS (
INSERT INTO loyalty_program (
company_id,
currency_id,
name,
active,
program_type,
applies_on,
trigger,
portal_visible
)
SELECT
DISTINCT(card.company_id),
company.currency_id,
jsonb_object_agg('en_US', 'Gift Cards'),
true,
'gift_card',
'future',
'auto',
true
FROM gift_card card
JOIN res_company company ON company.id = card.company_id
GROUP BY card.company_id, company.currency_id
RETURNING id, company_id
)
INSERT INTO loyalty_card (
old_gift_card_id,
program_id,
company_id,
partner_id,
code,
expiration_date,
points,
create_uid,
create_date,
write_uid,
write_date
)
SELECT
card.id,
program.id,
card.company_id,
card.partner_id,
card.code,
card.expired_date,
card.initial_amount,
card.create_uid,
card.create_date,
card.write_uid,
card.write_date
FROM gift_card card
JOIN inserted_loyalty_program program ON program.company_id = card.company_id
""",
)


@openupgrade.migrate()
def migrate(env, version):
_move_gift_cart_to_loyalty_card(env)
Loading

0 comments on commit f1781f9

Please sign in to comment.