Skip to content

Commit

Permalink
chore: whoops
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Jan 30, 2025
1 parent 10bc1b6 commit 2cc2dd8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
10 changes: 5 additions & 5 deletions integration/testdata/real_world_invoice_system/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ test("create order for new products", async () => {
});

expect(order.shipping).toBe(10);
expect(order.total).toBe(240 + 33.6 + 25.2 + order.shipping);
expect(order.total).toBe(240 + 33.6 + 25.2 + order.shipping!);
});

test("check stock levels after order", async () => {
Expand Down Expand Up @@ -145,7 +145,7 @@ test("adjust quantity in order", async () => {
order = await actions.getOrder({ id: order!.id });

expect(order?.shipping).toBe(8);
expect(order?.total).toBe(240 + 16.8 + 25.2 + order!.shipping);
expect(order?.total).toBe(240 + 16.8 + 25.2 + order!.shipping!);
});

test("check customer statistics after adjusting quantity", async () => {
Expand Down Expand Up @@ -188,7 +188,7 @@ test("change product in order item", async () => {
order = await actions.getOrder({ id: order!.id });

expect(order?.shipping).toBe(8);
expect(order?.total).toBe(240 + 70 + 25.2 + order!.shipping);
expect(order?.total).toBe(240 + 70 + 25.2 + order!.shipping!);
});

test("check stock levels after adjusting product", async () => {
Expand Down Expand Up @@ -221,7 +221,7 @@ test("create another order", async () => {
});

expect(order.shipping).toBe(8);
expect(order.total).toBe(67.2 + order.shipping);
expect(order.total).toBe(67.2 + order.shipping!);
});

test("check customer statistics after adjusting product", async () => {
Expand Down Expand Up @@ -260,7 +260,7 @@ test("change order's customer", async () => {
});

expect(order.shipping).toBe(8);
expect(order.total).toBe(67.2 + order.shipping);
expect(order.total).toBe(67.2 + order.shipping!);
});

test("check that stock levels are the same", async () => {
Expand Down
4 changes: 2 additions & 2 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func computedFieldsStmts(schema *proto.Schema, existingComputedFns []*FunctionRo

// Trigger function which will perform a fake update on the earlier model in the expression chain
fnName := computedDependencyFuncName(schema.FindModel(strcase.ToCamel(previousModel)), schema.FindModel(currentModel), strings.Split(expr, "."))
sql := fmt.Sprintf("CREATE OR REPLACE FUNCTION \"%s\"() RETURNS TRIGGER AS $$\nBEGIN\n\t%s\nEND; $$ LANGUAGE plpgsql;\n", fnName, stmt)
sql := fmt.Sprintf("CREATE OR REPLACE FUNCTION \"%s\"() RETURNS TRIGGER AS $$\nBEGIN\n\t%s\n\tRETURN NULL;\nEND; $$ LANGUAGE plpgsql;\n", fnName, stmt)

// For the comp_dep function on the target field's model, we include a filter on the UPDATE trigger to only trigger if the target field has changed
whenCondition := "TRUE"
Expand All @@ -726,7 +726,7 @@ func computedFieldsStmts(schema *proto.Schema, existingComputedFns []*FunctionRo

// Must be an AFTER trigger as we need the data to be written in order to perform the joins and for the computation to take into account the updated data
triggerName := fnName
sql += fmt.Sprintf("\nCREATE OR REPLACE TRIGGER \"%s\" AFTER INSERT OR DELETE ON \"%s\" FOR EACH ROW EXECUTE PROCEDURE \"%s\"();", triggerName, strcase.ToSnake(currentModel), fnName)
sql += fmt.Sprintf("CREATE OR REPLACE TRIGGER \"%s\" AFTER INSERT OR DELETE ON \"%s\" FOR EACH ROW EXECUTE PROCEDURE \"%s\"();", triggerName, strcase.ToSnake(currentModel), fnName)
sql += fmt.Sprintf("\nCREATE OR REPLACE TRIGGER \"%s_update\" AFTER UPDATE ON \"%s\" FOR EACH ROW WHEN(%s) EXECUTE PROCEDURE \"%s\"();", triggerName, strcase.ToSnake(currentModel), whenCondition, fnName)

depFns[fnName] = sql
Expand Down
9 changes: 3 additions & 6 deletions migrations/testdata/computed_field_many_to_one.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,25 @@ CREATE OR REPLACE FUNCTION "item__exec_comp_fns"() RETURNS TRIGGER AS $$ BEGIN
RETURN NEW;
END; $$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER "item__comp" BEFORE INSERT OR UPDATE ON "item" FOR EACH ROW EXECUTE PROCEDURE "item__exec_comp_fns"();
-- product.agent in item.product.agent.commission
CREATE OR REPLACE FUNCTION "agent__to__product__2eb4dbe9__comp_dep"() RETURNS TRIGGER AS $$
BEGIN
UPDATE "product" SET id = id WHERE agent_id IN (NEW.id, OLD.id);
RETURN NULL;
END; $$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER "agent__to__product__2eb4dbe9__comp_dep" AFTER INSERT OR DELETE ON "agent" FOR EACH ROW EXECUTE PROCEDURE "agent__to__product__2eb4dbe9__comp_dep"();
CREATE OR REPLACE TRIGGER "agent__to__product__2eb4dbe9__comp_dep_update" AFTER UPDATE ON "agent" FOR EACH ROW WHEN(NEW.commission <> OLD.commission) EXECUTE PROCEDURE "agent__to__product__2eb4dbe9__comp_dep"();
-- item.product in item.product.price
CREATE OR REPLACE FUNCTION "product__to__item__037dbf3a__comp_dep"() RETURNS TRIGGER AS $$
BEGIN
UPDATE "item" SET id = id WHERE product_id IN (NEW.id, OLD.id);
RETURN NULL;
END; $$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER "product__to__item__037dbf3a__comp_dep" AFTER INSERT OR DELETE ON "product" FOR EACH ROW EXECUTE PROCEDURE "product__to__item__037dbf3a__comp_dep"();
CREATE OR REPLACE TRIGGER "product__to__item__037dbf3a__comp_dep_update" AFTER UPDATE ON "product" FOR EACH ROW WHEN(NEW.price <> OLD.price) EXECUTE PROCEDURE "product__to__item__037dbf3a__comp_dep"();
-- item.product in item.product.agent.commission
CREATE OR REPLACE FUNCTION "product__to__item__2eb4dbe9__comp_dep"() RETURNS TRIGGER AS $$
BEGIN
UPDATE "item" SET id = id WHERE product_id IN (NEW.id, OLD.id);
RETURN NULL;
END; $$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER "product__to__item__2eb4dbe9__comp_dep" AFTER INSERT OR DELETE ON "product" FOR EACH ROW EXECUTE PROCEDURE "product__to__item__2eb4dbe9__comp_dep"();
CREATE OR REPLACE TRIGGER "product__to__item__2eb4dbe9__comp_dep_update" AFTER UPDATE ON "product" FOR EACH ROW WHEN(TRUE) EXECUTE PROCEDURE "product__to__item__2eb4dbe9__comp_dep"();
UPDATE "item" SET id = id;
Expand Down

0 comments on commit 2cc2dd8

Please sign in to comment.