Skip to content

Commit

Permalink
fix(db): avoid implicit commits in TXNs
Browse files Browse the repository at this point in the history
This commit changes the Verfiy* database stored procedure calls to use
TEMPORARY tables instead of real tables.  This avoids an implicit commit
in every transaction.
  • Loading branch information
Jonathan Niles authored and sfount committed Jan 19, 2017
1 parent 9233592 commit ca955d8
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions server/models/procedures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ BEGIN
SELECT NULL FROM `stage_invoice` LIMIT 0;

IF (`no_invoice_stage` = 1) THEN
CREATE temporary table stage_invoice
CREATE TEMPORARY TABLE stage_invoice
(SELECT project_id, uuid, cost, debtor_uuid, service_id, user_id, date, description);
ELSE
INSERT INTO stage_invoice
Expand All @@ -50,12 +50,12 @@ BEGIN

IF (`no_invoice_item_stage` = 1) THEN
-- tables does not exist - create and enter data
create temporary table stage_invoice_item
CREATE TEMPORARY TABLE stage_invoice_item
(select uuid, inventory_uuid, quantity, transaction_price, inventory_price, debit, credit, invoice_uuid);
ELSE
-- table exists - only enter data
insert into stage_invoice_item
(select uuid, inventory_uuid, quantity, transaction_price, inventory_price, debit, credit, invoice_uuid);
INSERT INTO stage_invoice_item
(SELECT uuid, inventory_uuid, quantity, transaction_price, inventory_price, debit, credit, invoice_uuid);
END IF;
END $$

Expand All @@ -67,8 +67,8 @@ CREATE PROCEDURE StageBillingService(
BEGIN
CALL VerifyBillingServiceStageTable();

insert into stage_billing_service
(select id, invoice_uuid);
INSERT INTO stage_billing_service
(SELECT id, invoice_uuid);
END $$

CREATE PROCEDURE StageSubsidy(
Expand All @@ -78,21 +78,21 @@ CREATE PROCEDURE StageSubsidy(
BEGIN
CALL VerifySubsidyStageTable();

insert into stage_subsidy
(select id, invoice_uuid);
INSERT INTO stage_subsidy
(SELECT id, invoice_uuid);
END $$

-- create a temporary staging table for the subsidies, this is done via a helper
-- method to ensure it has been created as sale writing time (subsidies are an
-- optional entity that may or may not have been called for staging)
CREATE PROCEDURE VerifySubsidyStageTable()
BEGIN
create table if not exists stage_subsidy (id INTEGER, invoice_uuid BINARY(16));
CREATE TEMPORARY TABLE IF NOT EXISTS stage_subsidy (id INTEGER, invoice_uuid BINARY(16));
END $$

CREATE PROCEDURE VerifyBillingServiceStageTable()
BEGIN
create table if not exists stage_billing_service (id INTEGER, invoice_uuid BINARY(16));
CREATE TEMPORARY TABLE IF NOT EXISTS stage_billing_service (id INTEGER, invoice_uuid BINARY(16));
END $$

CREATE PROCEDURE WriteInvoice(
Expand All @@ -115,7 +115,7 @@ BEGIN

-- invoice details
INSERT INTO invoice (project_id, uuid, cost, debtor_uuid, service_id, user_id, date, description)
SELECT * from stage_invoice where stage_invoice.uuid = uuid;
SELECT * FROM stage_invoice WHERE stage_invoice.uuid = uuid;

-- invoice item details
INSERT INTO invoice_item (uuid, inventory_uuid, quantity, transaction_price, inventory_price, debit, credit, invoice_uuid)
Expand Down Expand Up @@ -147,7 +147,7 @@ BEGIN
WHERE invoice.uuid = uuid;

-- return information relevant to the final calculated and written bill
select items_cost, billing_services_cost, total_cost_to_debtor, total_subsidy_cost, total_subsidised_cost;
SELECT items_cost, billing_services_cost, total_cost_to_debtor, total_subsidy_cost, total_subsidised_cost;
END $$

CREATE PROCEDURE PostInvoice(
Expand Down

0 comments on commit ca955d8

Please sign in to comment.