From 1da7e11c01b64c2ecf446189cfbfdf010955696e Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Sat, 9 Dec 2023 16:15:21 +0100 Subject: [PATCH 1/5] Offer to adjust data instead of referring back to 1.3 UI --- lib/LedgerSMB/Upgrade_Tests.pm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/LedgerSMB/Upgrade_Tests.pm b/lib/LedgerSMB/Upgrade_Tests.pm index 4f6611f8c8..e76a2ed23b 100644 --- a/lib/LedgerSMB/Upgrade_Tests.pm +++ b/lib/LedgerSMB/Upgrade_Tests.pm @@ -653,7 +653,7 @@ push @tests, __PACKAGE__->new( ); push @tests, __PACKAGE__->new( - test_query => q{select distinct gifi_accno from account + test_query => q{select distinct gifi_accno as accno from account where not exists (select 1 from gifi where gifi.accno = account.gifi_accno) @@ -661,9 +661,12 @@ push @tests, __PACKAGE__->new( and gifi_accno !~ '^\s*$'}, display_name => marktext('GIFI accounts not in "gifi" table'), name => 'missing_gifi_table_rows', - display_cols => [ 'gifi_accno' ], - table => 'account', - instructions => marktext('Please use the 1.3 UI to add the GIFI accounts'), + display_cols => [ 'accno', 'description' ], + table => 'gifi', + insert => 1, + columns => ['description'], + id_columns => ['accno'], + instructions => marktext('Please add the missing GIFI accounts'), appname => 'ledgersmb', min_version => '1.3', max_version => '1.3' From 9a3d7aa9bf532fda98f4db6c32d0a1d19d0c2090 Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Sat, 9 Dec 2023 16:19:44 +0100 Subject: [PATCH 2/5] Accept errors during the prologue of the migration Errors are an expected side-effect from the Fixes.sql mechanism, which is where the lines in between come from. --- sql/upgrade/1.3-1.5.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql/upgrade/1.3-1.5.sql b/sql/upgrade/1.3-1.5.sql index 15e56a08f6..7fb2c0d19e 100644 --- a/sql/upgrade/1.3-1.5.sql +++ b/sql/upgrade/1.3-1.5.sql @@ -14,6 +14,9 @@ -- -- Chris Travers +\set ON_ERROR_STOP off +\set ON_ERROR_ROLLBACK on + BEGIN; -- PRE-RC update ALTER TABLE partscustomer RENAME customer_id TO credit_id; @@ -733,6 +736,7 @@ COMMIT; -- ------------------------------------------------------------------------ +\set ON_ERROR_STOP on BEGIN; From d91c796a3d4c57d2aea5936d53991c55a45e0e31 Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Sat, 9 Dec 2023 16:22:54 +0100 Subject: [PATCH 3/5] Adjust Fixes.sql code to run on the 'lsmb13' schema The active schema has the target migration schema loaded, so these fixups don't work on the active schema and instead should be applied to the 'old' (lsmb13) schema. --- sql/upgrade/1.3-1.5.sql | 284 ++++++++++++++++++++-------------------- 1 file changed, 142 insertions(+), 142 deletions(-) diff --git a/sql/upgrade/1.3-1.5.sql b/sql/upgrade/1.3-1.5.sql index 7fb2c0d19e..d8a1c3843f 100644 --- a/sql/upgrade/1.3-1.5.sql +++ b/sql/upgrade/1.3-1.5.sql @@ -19,80 +19,80 @@ BEGIN; -- PRE-RC update -ALTER TABLE partscustomer RENAME customer_id TO credit_id; -ALTER TABLE partsvendor RENAME entity_id TO credit_id; +ALTER TABLE lsmb13.partscustomer RENAME customer_id TO credit_id; +ALTER TABLE lsmb13.partsvendor RENAME entity_id TO credit_id; COMMIT; BEGIN; -- 1.3.4, fix for menu-- David Bandel -update menu_attribute set value = 'receive_order' where value = +update lsmb13.menu_attribute set value = 'receive_order' where value = 'consolidate_sales_order' and node_id = '65'; -update menu_attribute set id = '149' where value = 'receive_order' +update lsmb13.menu_attribute set id = '149' where value = 'receive_order' and node_id = '65'; -update menu_attribute set value = 'consolidate_sales_order' where +update lsmb13.menu_attribute set value = 'consolidate_sales_order' where value = 'receive_order' and node_id = '64'; -update menu_attribute set id = '152' where value = +update lsmb13.menu_attribute set id = '152' where value = 'consolidate_sales_order' and node_id = '64'; -- fix for bug 3430820 -update menu_attribute set value = 'pricegroup' where node_id = '83' and attribute = 'type'; -update menu_attribute set value = 'partsgroup' where node_id = '82' and attribute = 'type'; +update lsmb13.menu_attribute set value = 'pricegroup' where node_id = '83' and attribute = 'type'; +update lsmb13.menu_attribute set value = 'partsgroup' where node_id = '82' and attribute = 'type'; -UPDATE menu_attribute SET value = 'partsgroup' WHERE node_id = 91 and attribute = 'type'; -UPDATE menu_attribute SET value = 'pricegroup' WHERE node_id = 92 and attribute = 'type'; +UPDATE lsmb13.menu_attribute SET value = 'partsgroup' WHERE node_id = 91 and attribute = 'type'; +UPDATE lsmb13.menu_attribute SET value = 'pricegroup' WHERE node_id = 92 and attribute = 'type'; -- Very restrictive because some people still have Asset handling installed from -- Addons and so the node_id and id values may not match. Don't want to break -- what is working! --CT -UPDATE menu_attribute SET value = 'begin_import' WHERE id = 631 and value = 'import' and node_id = '235'; +UPDATE lsmb13.menu_attribute SET value = 'begin_import' WHERE id = 631 and value = 'import' and node_id = '235'; -- Getting rid of System/Backup menu since this is broken -DELETE FROM menu_acl WHERE node_id BETWEEN 133 AND 135; -DELETE FROM menu_attribute WHERE node_id BETWEEN 133 AND 135; -DELETE FROM menu_node WHERE id BETWEEN 133 AND 135; +DELETE FROM lsmb13.menu_acl WHERE node_id BETWEEN 133 AND 135; +DELETE FROM lsmb13.menu_attribute WHERE node_id BETWEEN 133 AND 135; +DELETE FROM lsmb13.menu_node WHERE id BETWEEN 133 AND 135; -- bad batch type for receipt batches -update menu_attribute set value = 'receipt' where node_id = 203 and attribute='batch_type'; +update lsmb13.menu_attribute set value = 'receipt' where node_id = 203 and attribute='batch_type'; COMMIT; BEGIN; -ALTER TABLE entity_credit_account drop constraint "entity_credit_account_language_code_fkey"; +ALTER TABLE lsmb13.entity_credit_account drop constraint "entity_credit_account_language_code_fkey"; COMMIT; BEGIN; -ALTER TABLE entity_credit_account ADD FOREIGN KEY (language_code) REFERENCES language(code); +ALTER TABLE lsmb13.entity_credit_account ADD FOREIGN KEY (language_code) REFERENCES language(code); COMMIT; BEGIN; -UPDATE menu_attribute SET value = 'invoice' +UPDATE lsmb13.menu_attribute SET value = 'invoice' WHERE node_id = 117 AND attribute = 'type'; -UPDATE menu_attribute SET value = 'sales_order' +UPDATE lsmb13.menu_attribute SET value = 'sales_order' WHERE node_id = 118 AND attribute = 'type'; COMMIT; BEGIN; -ALTER TABLE entity_bank_account DROP CONSTRAINT entity_bank_account_pkey; -ALTER TABLE entity_bank_account ALTER COLUMN bic DROP NOT NULL; -ALTER TABLE entity_bank_account ADD UNIQUE(bic,iban); -CREATE UNIQUE INDEX eba_iban_null_bic_u ON entity_bank_account(iban) WHERE bic IS NULL; +ALTER TABLE lsmb13.entity_bank_account DROP CONSTRAINT entity_bank_account_pkey; +ALTER TABLE lsmb13.entity_bank_account ALTER COLUMN bic DROP NOT NULL; +ALTER TABLE lsmb13.entity_bank_account ADD UNIQUE(bic,iban); +CREATE UNIQUE INDEX lsmb13.eba_iban_null_bic_u ON lsmb13.entity_bank_account(iban) WHERE bic IS NULL; COMMIT; BEGIN; -- Data fixes for 1.2-1.3 upgrade. Will fail otherwise --Chris T -UPDATE parts +UPDATE lsmb13.parts SET income_accno_id = (SELECT account.id - FROM account JOIN lsmb12.chart USING (accno) + FROM lsmb13.account JOIN lsmb12.chart USING (accno) WHERE chart.id = income_accno_id), expense_accno_id = (SELECT account.id - FROM account JOIN lsmb12.chart USING (accno) + FROM lsmb13.account JOIN lsmb12.chart USING (accno) WHERE chart.id = expense_accno_id), inventory_accno_id = (SELECT account.id - FROM account JOIN lsmb12.chart USING (accno) + FROM lsmb13.account JOIN lsmb12.chart USING (accno) WHERE chart.id = inventory_accno_id) WHERE id IN (SELECT id FROM lsmb12.parts op WHERE op.id = parts.id @@ -104,21 +104,21 @@ COMMIT; BEGIN; -- Fix menu Shipping -> Ship to actually point to the shipping interface -- used to point to sales order consolidation -UPDATE menu_attribute +UPDATE lsmb13.menu_attribute SET value = 'ship_order' WHERE attribute='type' - AND node_id = (SELECT id FROM menu_node WHERE label = 'Ship'); + AND node_id = (SELECT id FROM lsmb13.menu_node WHERE label = 'Ship'); COMMIT; BEGIN; -- fix for non-existant role handling in menu_generate() and related -CREATE OR REPLACE FUNCTION menu_generate() RETURNS SETOF menu_item AS +CREATE OR REPLACE FUNCTION lsmb13.menu_generate() RETURNS SETOF menu_item AS $$ DECLARE - item menu_item; - arg menu_attribute%ROWTYPE; + item lsmb13.menu_item; + arg lsmb13.menu_attribute%ROWTYPE; BEGIN FOR item IN SELECT n.position, n.id, c.level, n.label, c.path, @@ -127,10 +127,10 @@ BEGIN 0, ',') c(id integer, parent integer, "level" integer, path text, list_order integer) - JOIN menu_node n USING(id) - JOIN menu_attribute ma ON (n.id = ma.node_id) + JOIN lsmb13.menu_node n USING(id) + JOIN lsmb13.menu_attribute ma ON (n.id = ma.node_id) WHERE n.id IN (select node_id - FROM menu_acl + FROM lsmb13.menu_acl JOIN (select rolname FROM pg_roles UNION select 'public') pgr @@ -153,9 +153,9 @@ BEGIN cc(id integer, parent integer, "level" integer, path text, list_order integer) - JOIN menu_node cn USING(id) + JOIN lsmb13.menu_node cn USING(id) WHERE cn.id IN - (select node_id FROM menu_acl + (select node_id FROM lsmb13.menu_acl JOIN (select rolname FROM pg_roles UNION select 'public') pgr @@ -184,17 +184,17 @@ BEGIN END; $$ language plpgsql; -COMMENT ON FUNCTION menu_generate() IS +COMMENT ON FUNCTION lsmb13.menu_generate() IS $$ This function returns the complete menu tree. It is used to generate nested menus for the web interface. $$; -CREATE OR REPLACE FUNCTION menu_children(in_parent_id int) RETURNS SETOF menu_item +CREATE OR REPLACE FUNCTION lsmb13.menu_children(in_parent_id int) RETURNS SETOF menu_item AS $$ declare - item menu_item; - arg menu_attribute%ROWTYPE; + item lsmb13.menu_item; + arg lsmb13.menu_attribute%ROWTYPE; begin FOR item IN SELECT n.position, n.id, c.level, n.label, c.path, @@ -203,10 +203,10 @@ begin in_parent_id, 1, ',') c(id integer, parent integer, "level" integer, path text, list_order integer) - JOIN menu_node n USING(id) - JOIN menu_attribute ma ON (n.id = ma.node_id) + JOIN lsmb13.menu_node n USING(id) + JOIN lsmb13.menu_attribute ma ON (n.id = ma.node_id) WHERE n.id IN (select node_id - FROM menu_acl + FROM lsmb13.menu_acl JOIN (select rolname FROM pg_roles UNION select 'public') pgr @@ -229,7 +229,7 @@ begin cc(id integer, parent integer, "level" integer, path text, list_order integer) - JOIN menu_node cn USING(id) + JOIN lsmb13.menu_node cn USING(id) WHERE cn.id IN (select node_id FROM menu_acl JOIN (select rolname FROM pg_roles @@ -261,14 +261,14 @@ $$ language plpgsql; COMMIT; BEGIN; -- Search Assets menu -update menu_node set parent = 229 where id = 233; +update lsmb13.menu_node set parent = 229 where id = 233; COMMIT; BEGIN; -- timecard additional info -ALTER TABLE jcitems ADD total numeric NOT NULL DEFAULT 0; -ALTER TABLE jcitems ADD non_billable numeric NOT NULL DEFAULT 0; +ALTER TABLE lsmb13.jcitems ADD total numeric NOT NULL DEFAULT 0; +ALTER TABLE lsmb13.jcitems ADD non_billable numeric NOT NULL DEFAULT 0; -UPDATE jcitems +UPDATE lsmb13.jcitems SET total = qty WHERE qty IS NOT NULL and total = 0; @@ -278,7 +278,7 @@ BEGIN; -- FX RECON -ALTER TABLE cr_report ADD recon_fx bool default false; +ALTER TABLE lsmb13.cr_report ADD recon_fx bool default false; COMMIT; @@ -286,25 +286,25 @@ BEGIN; -- MIN VALUE FOR TAXES -ALTER TABLE tax ADD minvalue numeric; -ALTER TABLE tax ADD maxvalue numeric; +ALTER TABLE lsmb13.tax ADD minvalue numeric; +ALTER TABLE lsmb13.tax ADD maxvalue numeric; COMMIT; BEGIN; -ALTER TABLE mime_type ADD invoice_include bool default false; -UPDATE mime_type SET invoice_include = 'true' where mime_type like 'image/%'; +ALTER TABLE lsmb13.mime_type ADD invoice_include bool default false; +UPDATE lsmb13.mime_type SET invoice_include = 'true' where mime_type like 'image/%'; COMMIT; BEGIN; -UPDATE menu_attribute SET value = 'sales_quotation' +UPDATE lsmb13.menu_attribute SET value = 'sales_quotation' WHERE node_id = 169 AND attribute = 'template'; -UPDATE menu_attribute SET value = 'request_quotation' +UPDATE lsmb13.menu_attribute SET value = 'request_quotation' WHERE node_id = 170 AND attribute = 'template'; COMMIT; @@ -312,8 +312,8 @@ BEGIN; -- fixes for menu taking a long time to render when few permissions are granted -DROP TYPE IF EXISTS menu_item CASCADE; -CREATE TYPE menu_item AS ( +DROP TYPE IF EXISTS lsmb13.menu_item CASCADE; +CREATE TYPE lsmb13.menu_item AS ( position int, id int, level int, @@ -325,31 +325,31 @@ CREATE TYPE menu_item AS ( -CREATE OR REPLACE FUNCTION menu_generate() RETURNS SETOF menu_item AS +CREATE OR REPLACE FUNCTION lsmb13.menu_generate() RETURNS SETOF menu_item AS $$ DECLARE - item menu_item; - arg menu_attribute%ROWTYPE; + item lsmb13.menu_item; + arg lsmb13.menu_attribute%ROWTYPE; BEGIN FOR item IN WITH RECURSIVE tree (path, id, parent, level, positions) AS (select id::text as path, id, parent, 0 as level, position::text - FROM menu_node where parent is null + FROM lsmb13.menu_node where parent is null UNION select path || ',' || n.id::text, n.id, n.parent, t.level + 1, t.positions || ',' || n.position - FROM menu_node n + FROM lsmb13.menu_node n JOIN tree t ON t.id = n.parent) SELECT n.position, n.id, c.level, n.label, c.path, n.parent, to_args(array[ma.attribute, ma.value]) FROM tree c - JOIN menu_node n USING(id) - JOIN menu_attribute ma ON (n.id = ma.node_id) + JOIN lsmb13.menu_node n USING(id) + JOIN lsmb13.menu_attribute ma ON (n.id = ma.node_id) WHERE n.id IN (select node_id - FROM menu_acl acl + FROM lsmb13.menu_acl acl LEFT JOIN pg_roles pr on pr.rolname = acl.role_name WHERE CASE WHEN role_name ilike 'public' @@ -367,10 +367,10 @@ BEGIN END)) or exists (select cn.id, cc.path FROM tree cc - JOIN menu_node cn USING(id) + JOIN lsmb13.menu_node cn USING(id) WHERE cn.id IN (select node_id - FROM menu_acl acl + FROM lsmb13.menu_acl acl LEFT JOIN pg_roles pr on pr.rolname = acl.role_name WHERE CASE WHEN rolname @@ -400,18 +400,18 @@ BEGIN END; $$ language plpgsql; -COMMENT ON FUNCTION menu_generate() IS +COMMENT ON FUNCTION lsmb13.menu_generate() IS $$ This function returns the complete menu tree. It is used to generate nested menus for the web interface. $$; -CREATE OR REPLACE FUNCTION menu_children(in_parent_id int) RETURNS SETOF menu_item +CREATE OR REPLACE FUNCTION lsmb13.menu_children(in_parent_id int) RETURNS SETOF menu_item AS $$ -SELECT * FROM menu_generate() where parent = $1; +SELECT * FROM lsmb13.menu_generate() where parent = $1; $$ language sql; -COMMENT ON FUNCTION menu_children(int) IS +COMMENT ON FUNCTION lsmb13.menu_children(int) IS $$ This function returns all menu items which are children of in_parent_id (the only input parameter). @@ -420,23 +420,23 @@ associated with nodes directly descendant from the parent. It is used for menues for frameless browsers.$$; CREATE OR REPLACE FUNCTION -menu_insert(in_parent_id int, in_position int, in_label text) +lsmb13.menu_insert(in_parent_id int, in_position int, in_label text) returns int AS $$ DECLARE new_id int; BEGIN - UPDATE menu_node + UPDATE lsmb13.menu_node SET position = position * -1 WHERE parent = in_parent_id AND position >= in_position; - INSERT INTO menu_node (parent, position, label) + INSERT INTO lsmb13.menu_node (parent, position, label) VALUES (in_parent_id, in_position, in_label); SELECT INTO new_id currval('menu_node_id_seq'); - UPDATE menu_node + UPDATE lsmb13.menu_node SET position = (position * -1) + 1 WHERE parent = in_parent_id AND position < 0; @@ -445,32 +445,32 @@ BEGIN END; $$ language plpgsql; -comment on function menu_insert(int, int, text) is $$ +comment on function lsmb13.menu_insert(int, int, text) is $$ This function inserts menu items at arbitrary positions. The arguments are, in order: parent, position, label. The return value is the id number of the menu item created. $$; -DROP VIEW menu_friendly; -CREATE VIEW menu_friendly AS +DROP VIEW lsmb13.menu_friendly; +CREATE VIEW lsmb13.menu_friendly AS WITH RECURSIVE tree (path, id, parent, level, positions) AS (select id::text as path, id, parent, 0 as level, position::text - FROM menu_node where parent is null + FROM lsmb13.menu_node where parent is null UNION select path || ',' || n.id::text, n.id, n.parent, t.level + 1, t.positions || ',' || n.position - FROM menu_node n + FROM lsmb13.menu_node n JOIN tree t ON t.id = n.parent) SELECT t."level", t.path, (repeat(' '::text, (2 * t."level")) || (n.label)::text) AS label, n.id, n."position" FROM tree t - JOIN menu_node n USING (id) + JOIN lsmb13.menu_node n USING (id) ORDER BY string_to_array(t.positions, ',')::int[]; -COMMENT ON VIEW menu_friendly IS +COMMENT ON VIEW lsmb13.menu_friendly IS $$ A nice human-readable view for investigating the menu tree. Does not show menu attributes or acls.$$; @@ -478,28 +478,28 @@ COMMIT; BEGIN; -- Fix for menu anomilies -DELETE FROM menu_acl - where node_id in (select node_id from menu_attribute where attribute = 'menu'); +DELETE FROM lsmb13.menu_acl + where node_id in (select node_id from lsmb13.menu_attribute where attribute = 'menu'); COMMIT; BEGIN; -- fix primary key for make/model -ALTER TABLE makemodel DROP CONSTRAINT makemodel_pkey; -ALTER TABLE makemodel ADD PRIMARY KEY(parts_id, make, model); +ALTER TABLE lsmb13.makemodel DROP CONSTRAINT makemodel_pkey; +ALTER TABLE lsmb13.makemodel ADD PRIMARY KEY(parts_id, make, model); COMMIT; BEGIN; -- performance fix for all years list functions -create index ac_transdate_year_idx on acc_trans(EXTRACT ('YEAR' FROM transdate)); +create index lsmb13.ac_transdate_year_idx on lsmb13.acc_trans(EXTRACT ('YEAR' FROM transdate)); COMMIT; BEGIN; -- RECEIPT REVERSAL broken: -insert into batch_class (id,class) values (7,'receipt_reversal'); +insert into lsmb13.batch_class (id,class) values (7,'receipt_reversal'); COMMIT; @@ -507,9 +507,9 @@ BEGIN; -- FIXING AP MENU - update menu_attribute set value = 'tax_paid' where node_id = 28 and attribute = 'report'; + update lsmb13.menu_attribute set value = 'tax_paid' where node_id = 28 and attribute = 'report'; -update menu_attribute set value = 'ap_aging' where node_id = 27 and attribute = 'report'; +update lsmb13.menu_attribute set value = 'ap_aging' where node_id = 27 and attribute = 'report'; COMMIT; @@ -517,43 +517,43 @@ BEGIN; -- inventory from 1.3.30 and lower -UPDATE parts +UPDATE lsmb13.parts SET onhand = onhand + coalesce((select sum(qty) - from inventory + from lsmb13.inventory where orderitems_id IN (select id - from orderitems oi - join oe on oi.trans_id = oe.id + from lsmb13.orderitems oi + join lsmb13.oe on oi.trans_id = oe.id where closed is not true) and parts_id = parts.id), 0) - WHERE string_to_array((setting_get('version')).value::text, '.')::int[] + WHERE string_to_array((select value from lsmb13.defaults where setting_key='version'), '.')::int[] < '{1,3,31}'; COMMIT; BEGIN; -delete from menu_attribute where node_id = 192 and attribute = 'menu'; +delete from lsmb13.menu_attribute where node_id = 192 and attribute = 'menu'; -DELETE FROM menu_acl WHERE node_id = 60 AND exists (select 1 from menu_attribute where node_id = 60 and attribute = 'menu'); +DELETE FROM lsmb13.menu_acl WHERE node_id = 60 AND exists (select 1 from lsmb13.menu_attribute where node_id = 60 and attribute = 'menu'); COMMIT; BEGIN; -ALTER FUNCTION admin__save_user(int, int, text, text, bool) SET datestyle = 'ISO,YMD'; -ALTER FUNCTION user__change_password(text) SET datestyle = 'ISO,YMD'; +ALTER FUNCTION lsmb13.admin__save_user(int, int, text, text, bool) SET datestyle = 'ISO,YMD'; +ALTER FUNCTION lsmb13.user__change_password(text) SET datestyle = 'ISO,YMD'; COMMIT; BEGIN; -ALTER TABLE ar DISABLE TRIGGER ALL; -ALTER TABLE ap DISABLE TRIGGER ALL; +ALTER TABLE lsmb13.ar DISABLE TRIGGER ALL; +ALTER TABLE lsmb13.ap DISABLE TRIGGER ALL; -ALTER TABLE ap ADD COLUMN crdate date; -ALTER TABLE ar ADD COLUMN crdate date; +ALTER TABLE lsmb13.ap ADD COLUMN crdate date; +ALTER TABLE lsmb13.ar ADD COLUMN crdate date; -UPDATE ap SET crdate=transdate; -UPDATE ar SET crdate=transdate; +UPDATE lsmb13.ap SET crdate=transdate; +UPDATE lsmb13.ar SET crdate=transdate; -COMMENT ON COLUMN ap.crdate IS +COMMENT ON COLUMN lsmb13.ap.crdate IS $$ This is for recording the AR/AP creation date, which is always that date, when the invoice created. This is different, than transdate or duedate. This kind of date does not effect on ledger/financial data, but for administrative purposes in Hungary, probably in other countries, too. Use case: @@ -561,7 +561,7 @@ if somebody pay in cash, crdate=transdate=duedate if somebody will receive goods T+5 days and have 15 days term, the dates are the following: crdate: now, transdate=crdate+5, duedate=transdate+15. There are rules in Hungary, how to fill out a correct invoice, where the crdate and transdate should be important.$$; -COMMENT ON COLUMN ar.crdate IS +COMMENT ON COLUMN lsmb13.ar.crdate IS $$ This is for recording the AR/AP creation date, which is always that date, when the invoice created. This is different, than transdate or duedate. This kind of date does not effect on ledger/financial data, but for administrative purposes in Hungary, probably in other countries, too. Use case: @@ -569,28 +569,28 @@ if somebody pay in cash, crdate=transdate=duedate if somebody will receive goods T+5 days and have 15 days term, the dates are the following: crdate: now, transdate=crdate+5, duedate=transdate+15. There are rules in Hungary, how to fill out a correct invoice, where the crdate and transdate should be important.$$; -ALTER TABLE ar ENABLE TRIGGER ALL; -ALTER TABLE ap ENABLE TRIGGER ALL; +ALTER TABLE lsmb13.ar ENABLE TRIGGER ALL; +ALTER TABLE lsmb13.ap ENABLE TRIGGER ALL; COMMIT; BEGIN; - ALTER TABLE entity_bank_account ADD COLUMN remark varchar; + ALTER TABLE lsmb13.entity_bank_account ADD COLUMN remark varchar; COMMENT ON COLUMN entity_bank_account.remark IS $$ This field contains the notes for an account, like: This is USD account, this one is HUF account, this one is the default account, this account for paying specific taxes. If a partner has more than one account, now you are able to write remarks for them. $$; -DROP FUNCTION eca__save_bank_account (int, int, text, text, int); -DROP FUNCTION entity__save_bank_account (int, text, text, int); +DROP FUNCTION lsmb13.eca__save_bank_account (int, int, text, text, int); +DROP FUNCTION lsmb13.entity__save_bank_account (int, text, text, int); -CREATE OR REPLACE FUNCTION eca__save_bank_account +CREATE OR REPLACE FUNCTION lsmb13.eca__save_bank_account (in_entity_id int, in_credit_id int, in_bic text, in_iban text, in_remark text, in_bank_account_id int) RETURNS int AS $$ DECLARE out_id int; BEGIN - UPDATE entity_bank_account + UPDATE lsmb13.entity_bank_account SET bic = in_bic, iban = in_iban, remark = in_remark @@ -599,13 +599,13 @@ BEGIN IF FOUND THEN out_id = in_bank_account_id; ELSE - INSERT INTO entity_bank_account(entity_id, bic, iban, remark) + INSERT INTO lsmb13.entity_bank_account(entity_id, bic, iban, remark) VALUES(in_entity_id, in_bic, in_iban, in_remark); SELECT CURRVAL('entity_bank_account_id_seq') INTO out_id ; END IF; IF in_credit_id IS NOT NULL THEN - UPDATE entity_credit_account SET bank_account = out_id + UPDATE lsmb13.entity_credit_account SET bank_account = out_id WHERE id = in_credit_id; END IF; @@ -613,18 +613,18 @@ BEGIN END; $$ LANGUAGE PLPGSQL; -COMMENT ON FUNCTION eca__save_bank_account +COMMENT ON FUNCTION lsmb13.eca__save_bank_account (in_entity_id int, in_credit_id int, in_bic text, in_iban text, in_remark text, in_bank_account_id int) IS $$ Saves bank account to the credit account.$$; -CREATE OR REPLACE FUNCTION entity__save_bank_account +CREATE OR REPLACE FUNCTION lsmb13.entity__save_bank_account (in_entity_id int, in_bic text, in_iban text, in_remark text, in_bank_account_id int) RETURNS int AS $$ DECLARE out_id int; BEGIN - UPDATE entity_bank_account + UPDATE lsmb13.entity_bank_account SET bic = in_bic, iban = in_iban, remark = in_remark @@ -633,7 +633,7 @@ BEGIN IF FOUND THEN out_id = in_bank_account_id; ELSE - INSERT INTO entity_bank_account(entity_id, bic, iban, remark) + INSERT INTO lsmb13.entity_bank_account(entity_id, bic, iban, remark) VALUES(in_entity_id, in_bic, in_iban, in_remark); SELECT CURRVAL('entity_bank_account_id_seq') INTO out_id ; END IF; @@ -642,29 +642,29 @@ BEGIN END; $$ LANGUAGE PLPGSQL; -COMMENT ON FUNCTION entity__save_bank_account +COMMENT ON FUNCTION lsmb13.entity__save_bank_account (in_entity_id int, in_bic text, in_iban text, in_remark text, in_bank_account_id int) IS $$Saves a bank account to the entity.$$; COMMIT; BEGIN; -ALTER TABLE location ALTER COLUMN mail_code DROP NOT NULL; +ALTER TABLE lsmb13.location ALTER COLUMN mail_code DROP NOT NULL; COMMIT; BEGIN; -UPDATE menu_attribute +UPDATE lsmb13.menu_attribute SET value = 'sales_quotation' where value = 'quotation' AND attribute='template'; COMMIT; BEGIN; -CREATE FUNCTION prevent_closed_transactions() RETURNS trigger +CREATE FUNCTION lsmb13.prevent_closed_transactions() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE t_end_date date; BEGIN -SELECT max(end_date) into t_end_date FROM account_checkpoint; +SELECT max(end_date) into t_end_date FROM lsmb13.account_checkpoint; IF new.transdate <= t_end_date THEN RAISE EXCEPTION 'Transaction entered into closed period. Transdate: %', new.transdate; @@ -674,57 +674,57 @@ END; $$; -CREATE TRIGGER acc_trans_prevent_closed BEFORE INSERT ON acc_trans +CREATE TRIGGER lsmb13.acc_trans_prevent_closed BEFORE INSERT ON acc_trans FOR EACH ROW EXECUTE PROCEDURE prevent_closed_transactions(); -CREATE TRIGGER ap_prevent_closed BEFORE INSERT ON ap +CREATE TRIGGER lsmb13.ap_prevent_closed BEFORE INSERT ON ap FOR EACH ROW EXECUTE PROCEDURE prevent_closed_transactions(); -CREATE TRIGGER ar_prevent_closed BEFORE INSERT ON ar +CREATE TRIGGER lsmb13.ar_prevent_closed BEFORE INSERT ON ar FOR EACH ROW EXECUTE PROCEDURE prevent_closed_transactions(); -CREATE TRIGGER gl_prevent_closed BEFORE INSERT ON gl +CREATE TRIGGER lsmb13.gl_prevent_closed BEFORE INSERT ON gl FOR EACH ROW EXECUTE PROCEDURE prevent_closed_transactions(); COMMIT; BEGIN; -INSERT INTO defaults VALUES ('disable_back', '0'); +INSERT INTO lsmb13.defaults VALUES ('disable_back', '0'); COMMIT; BEGIN; -ALTER TABLE batch DROP CONSTRAINT "batch_locked_by_fkey"; -ALTER TABLE batch ADD FOREIGN KEY (locked_by) REFERENCES session(session_id) +ALTER TABLE lsmb13.batch DROP CONSTRAINT "batch_locked_by_fkey"; +ALTER TABLE lsmb13.batch ADD FOREIGN KEY (locked_by) REFERENCES session(session_id) ON DELETE SET NULL; COMMIT; BEGIN; -ALTER TABLE invoice ALTER COLUMN allocated TYPE NUMERIC; +ALTER TABLE lsmb13.invoice ALTER COLUMN allocated TYPE NUMERIC; COMMIT; BEGIN; -ALTER TABLE entity_employee ADD is_manager bool DEFAULT FALSE; -UPDATE entity_employee SET is_manager = true WHERE role = 'manager'; +ALTER TABLE lsmb13.entity_employee ADD is_manager bool DEFAULT FALSE; +UPDATE lsmb13.entity_employee SET is_manager = true WHERE role = 'manager'; COMMIT; BEGIN; -ALTER TABLE BATCH DROP CONSTRAINT "batch_locked_by_fkey"; +ALTER TABLE lsmb13.BATCH DROP CONSTRAINT "batch_locked_by_fkey"; -ALTER TABLE BATCH ADD FOREIGN KEY (locked_by) references session (session_id) +ALTER TABLE lsmb13.BATCH ADD FOREIGN KEY (locked_by) references session (session_id) ON DELETE SET NULL; COMMIT; BEGIN; -ALTER TABLE invoice ADD vendor_sku text; -UPDATE invoice SET vendor_sku = (select min(partnumber) from partsvendor +ALTER TABLE lsmb13.invoice ADD vendor_sku text; +UPDATE lsmb13.invoice SET vendor_sku = (select min(partnumber) from lsmb13.partsvendor where parts_id = invoice.parts_id AND credit_id = ( select entity_credit_account - from ap + from lsmb13.ap where ap.id = invoice.trans_id ) ) - WHERE trans_id in (select id from ap); + WHERE trans_id in (select id from lsmb13.ap); COMMIT; From 8957eb263b0a95adff7699d72b7ff9d0c6e50684 Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Sat, 9 Dec 2023 16:28:53 +0100 Subject: [PATCH 4/5] Nullify data causing migration failure The nullified data is in the dataset that triggered this, just replacing zeroes with null. --- sql/upgrade/1.3-1.5.sql | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sql/upgrade/1.3-1.5.sql b/sql/upgrade/1.3-1.5.sql index d8a1c3843f..e020dfffd3 100644 --- a/sql/upgrade/1.3-1.5.sql +++ b/sql/upgrade/1.3-1.5.sql @@ -728,6 +728,32 @@ UPDATE lsmb13.invoice SET vendor_sku = (select min(partnumber) from lsmb13.parts COMMIT; +BEGIN; + +ALTER TABLE lsmb13.ar DISABLE TRIGGER ALL; + +UPDATE lsmb13.ar SET person_id = NULL + WHERE NOT EXISTS (select 1 from lsmb13.entity_employee emp + where ar.person_id = emp.entity_id); +\echo Nonexisting person_id in AR + +ALTER TABLE lsmb13.ar ENABLE TRIGGER ALL; + +COMMIT; + + +BEGIN; + +ALTER TABLE lsmb13.ap DISABLE TRIGGER ALL; + +UPDATE lsmb13.ap SET person_id = NULL + WHERE NOT EXISTS (select 1 from lsmb13.entity_employee emp + where ap.person_id = emp.entity_id); +\echo Nonexisting person_id in AP + +ALTER TABLE lsmb13.ap ENABLE TRIGGER ALL; + +COMMIT; ------------------------------------------------------------------------ From 77b0dff6bbdfaf9254db423f98df097154d2f86a Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Sat, 9 Dec 2023 17:05:59 +0100 Subject: [PATCH 5/5] Address data quality problems in the 1.3 migration Since the data can't be resolved, we have no other option than removing it. --- sql/upgrade/1.3-1.5.sql | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/sql/upgrade/1.3-1.5.sql b/sql/upgrade/1.3-1.5.sql index e020dfffd3..3015c0d8e9 100644 --- a/sql/upgrade/1.3-1.5.sql +++ b/sql/upgrade/1.3-1.5.sql @@ -755,6 +755,40 @@ ALTER TABLE lsmb13.ap ENABLE TRIGGER ALL; COMMIT; +BEGIN; + +ALTER TABLE lsmb13.ar DISABLE TRIGGER ALL; +ALTER TABLE lsmb13.ap DISABLE TRIGGER ALL; + +\echo Removing AR transaction headers without amounts and lines +DELETE FROM lsmb13.ar + WHERE NOT ( (amount IS NULL AND curr IS NULL) + OR (amount IS NOT NULL AND curr IS NOT NULL)) + AND 0 = (select count(*) from lsmb13.acc_trans ac where ar.id = ac.trans_id); + +\echo Removing AP transaction headers without amounts and lines +DELETE FROM lsmb13.ap + WHERE NOT ( (amount IS NULL AND curr IS NULL) + OR (amount IS NOT NULL AND curr IS NOT NULL)) + AND 0 = (select count(*) from lsmb13.acc_trans ac where ap.id = ac.trans_id); + +ALTER TABLE lsmb13.ar ENABLE TRIGGER ALL; +ALTER TABLE lsmb13.ap ENABLE TRIGGER ALL; + +COMMIT; + +BEGIN; + +ALTER TABLE lsmb13.acc_trans DISABLE TRIGGER ALL; + +UPDATE lsmb13.acc_trans a + SET invoice_id = NULL + WHERE NOT EXISTS (select 1 from lsmb13.invoice i where a.invoice_id = i.id); + +ALTER TABLE lsmb13.acc_trans ENABLE TRIGGER ALL; + +COMMIT; + ------------------------------------------------------------------------ --