schema-to-sql: drop stale insert_* overloads before creating the function #1

Merged
eliribble merged 1 commit from schema-to-sql-drop-stale-overloads into main 2026-09-15 04:25:37 +00:00
Owner

Problem

CREATE OR REPLACE FUNCTION only replaces a function whose argument types are unchanged. When an ArcGIS schema's field list changes, the regenerated insert_<table> function keeps the prior argument-type identity and the new definition lands as an additional overload. Callers resolve a single signature by parameter name, so the old identities become unreachable dead code. In nidus-sync this stranded 29 overloads across the fieldseeker insert_* procs (speciesabundance and traplocation had two each).

Change

Emit a guard before the CREATE that drops every existing overload of the same insert function in the schema:

DO $$
DECLARE
	v_existing regprocedure;
BEGIN
	FOR v_existing IN
		SELECT p.oid::regprocedure
		FROM pg_proc p
		JOIN pg_namespace n ON n.oid = p.pronamespace
		WHERE n.nspname = '<schema>'
			AND p.proname = 'insert_<table>'
			AND p.prokind = 'f'
	LOOP
		EXECUTE 'DROP FUNCTION ' || v_existing::text;
	END LOOP;
END $$;

The guard and the CREATE are separate -- +goose StatementBegin/End blocks so the DO block's internal semicolons are not split.

Verification

  • New unit test locks the drop guard ahead of CREATE.
  • Generated SQL applied in a rolled-back transaction against a table with two stale overloads: 2 -> exactly 1 (the current signature); re-applying stays at 1; the function inserts successfully.
  • go test ./cmd/schema-to-sql/, go vet ./cmd/schema-to-sql/, and gofmt -l are clean.

Note: go test ./... still fails to build fieldseeker/ and examples/ at this commit; that is pre-existing and unrelated to this change.

## Problem `CREATE OR REPLACE FUNCTION` only replaces a function whose argument types are unchanged. When an ArcGIS schema's field list changes, the regenerated `insert_<table>` function keeps the prior argument-type identity and the new definition lands as an additional overload. Callers resolve a single signature by parameter name, so the old identities become unreachable dead code. In nidus-sync this stranded 29 overloads across the fieldseeker `insert_*` procs (speciesabundance and traplocation had two each). ## Change Emit a guard before the `CREATE` that drops every existing overload of the same insert function in the schema: ```sql DO $$ DECLARE v_existing regprocedure; BEGIN FOR v_existing IN SELECT p.oid::regprocedure FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = '<schema>' AND p.proname = 'insert_<table>' AND p.prokind = 'f' LOOP EXECUTE 'DROP FUNCTION ' || v_existing::text; END LOOP; END $$; ``` The guard and the `CREATE` are separate `-- +goose StatementBegin/End` blocks so the DO block's internal semicolons are not split. ## Verification - New unit test locks the drop guard ahead of `CREATE`. - Generated SQL applied in a rolled-back transaction against a table with two stale overloads: 2 -> exactly 1 (the current signature); re-applying stays at 1; the function inserts successfully. - `go test ./cmd/schema-to-sql/`, `go vet ./cmd/schema-to-sql/`, and `gofmt -l` are clean. Note: `go test ./...` still fails to build `fieldseeker/` and `examples/` at this commit; that is pre-existing and unrelated to this change.
CREATE OR REPLACE FUNCTION only replaces a function whose argument types are unchanged. When a generated schema's field list changes, the previously generated insert_* function keeps its old argument-type identity and the new definition lands as an additional overload, leaving dead signatures that callers cannot resolve by name (this stranded 29 overloads across the fieldseeker insert_* procs).

Emit a guard before the CREATE that drops every existing overload of the same insert function in the schema, so each schema (re)generation converges on exactly one signature. The guard and the CREATE are separate goose statements so the DO block's internal semicolons are not split.

Add a unit test locking the drop guard ahead of CREATE.
eliribble deleted branch schema-to-sql-drop-stale-overloads 2026-09-15 04:25:37 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Gleipnir/arcgis-go!1
No description provided.