Skip to content

Commit

Permalink
feat: recompute existing data (#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza authored Jan 27, 2025
1 parent d0491a9 commit 61e0fcd
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ func computedFieldsStmts(schema *proto.Schema, existingComputedFns []*FunctionRo
fns := map[string]string{}
fieldsFns := map[*proto.Field]string{}
changedFields := map[*proto.Field]bool{}
recompute := []*proto.Model{}

// Adding computed field triggers and functions
for _, model := range schema.Models {
Expand Down Expand Up @@ -550,6 +551,10 @@ func computedFieldsStmts(schema *proto.Schema, existingComputedFns []*FunctionRo
Type: ChangeTypeModified,
})
changedFields[f] = true

if !lo.Contains(recompute, model) {
recompute = append(recompute, model)
}
}

// Functions to be dropped
Expand Down Expand Up @@ -742,6 +747,13 @@ func computedFieldsStmts(schema *proto.Schema, existingComputedFns []*FunctionRo
statements = append(statements, fmt.Sprintf("DROP FUNCTION %s;", fn))
}

// If a computed field has been added or changed, we need to recompute all existing data.
// This is done by fake updating each row on the table which will cause the triggers to run.
for _, model := range recompute {
sql := fmt.Sprintf("UPDATE %s SET id = id;", strcase.ToSnake(model.Name))
statements = append(statements, sql)
}

return
}

Expand Down
1 change: 1 addition & 0 deletions migrations/testdata/computed_field_changed_expression.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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();
UPDATE item SET id = id;

===

Expand Down
2 changes: 2 additions & 0 deletions migrations/testdata/computed_field_initial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ 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();
UPDATE item SET id = id;

===

[
Expand Down
1 change: 1 addition & 0 deletions migrations/testdata/computed_field_many_to_one.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ CREATE OR REPLACE FUNCTION product__to__item__9454cd1f__comp_dep() RETURNS TRIGG
RETURN NULL;
END; $$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER product__to__item__9454cd1f__comp_dep AFTER INSERT OR DELETE OR UPDATE ON "product" FOR EACH ROW EXECUTE PROCEDURE product__to__item__9454cd1f__comp_dep();
UPDATE item SET id = id;

===

Expand Down
1 change: 1 addition & 0 deletions migrations/testdata/computed_field_many_to_one_changed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ END; $$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER item__comp BEFORE INSERT OR UPDATE ON "item" FOR EACH ROW EXECUTE PROCEDURE item__exec_comp_fns();
DROP TRIGGER agent__to__item__26590ccb__comp_dep ON agent;
DROP FUNCTION agent__to__item__26590ccb__comp_dep;
UPDATE item SET id = id;

===

Expand Down
1 change: 1 addition & 0 deletions migrations/testdata/computed_field_multiple_depend.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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();
UPDATE item SET id = id;

===

Expand Down
1 change: 1 addition & 0 deletions migrations/testdata/computed_field_renamed_field.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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();
UPDATE item SET id = id;

===

Expand Down

0 comments on commit 61e0fcd

Please sign in to comment.