Adding or changing a function
Functions are no longer dropped and recreated at every launch. They live in functions/*.sql of a bundle and are (re)created only when the bundle changes, as <owner>_ddl.
1. Choose the bundle
lesage/sql/functions/for anything Le Sage uses (calculations, ABR, décomptes, imports…). This is the normal case.common/sql/functions/only for a helper several products need. It ships to Inpro and Aïda too, and must stay backward compatible. A function name belongs to one bundle only (lint ruleowner-bundle).
Files are grouped by topic (calculatecopyright.sql, abr3.sql, decomptes-lesage.sql…). Add to the right one, or create a new .sql file: every file in functions/ is run.
2. Write it
CREATE OR REPLACE FUNCTION public.worktitle(AIdOeu INTEGER) RETURNS TEXT AS
$BODY$
SELECT COALESCE(titrelong, '') FROM oeu WHERE idoeu = AIdOeu;
$BODY$ LANGUAGE sql STABLE
SET search_path = public;
COMMENT ON FUNCTION public.worktitle(INTEGER) IS 'Long title of a work, '''' when none';
- Only
CREATE OR REPLACE FUNCTION+COMMENT ON FUNCTION. Noudf_dropfunctionprelude any more. SET search_pathon the function is recommended (not enforced).- No table DDL inside a function body: use
CREATE TEMP TABLE, orTRUNCATE/DELETEa work table that a step created (likezDroits,zQuoteParts). - No role switch (
setrole,SET ROLE): functions run as the plain login now. - Never touch the FROZEN functions
oversimplify,shortcode,unaccent(indexes depend on them).
3. Regenerate functions.list
functions.list holds the exact signature of every function the bundle manages (public.worktitle(integer)). The engine drops any overload not listed, so a new function must be in it. It is generated, never hand-edited:
cd ~/dev/improvize/common
PGHOST=/var/run/postgresql npm run check -- apply sql ../lesage/sql --write-lists
Commit the updated lesage/sql/functions.list with your function.
Changing an existing function
| Change | What to do |
|---|---|
| Body only | Edit it. Nothing else (apart from the version bump rule below). |
| Argument types (new signature) | Edit the CREATE, run --write-lists. The old signature is no longer listed, so the engine drops it on the next upgrade. |
| Return type or argument names (same signature) | Put DROP FUNCTION IF EXISTS public.f(argtypes); just before the CREATE: Postgres refuses CREATE OR REPLACE for that. |
| Remove or rename | Delete the definition, add the old signature to retired.list (canonical form, e.g. public.f(integer, text)), run --write-lists. |
Drops never use CASCADE: if a view or another object depends on a function being dropped, the whole upgrade fails and rolls back. Drop or recreate the dependent object first (in post/ or a step).
The version bump applies to functions too
The bundle's checksum covers every file, functions included. Changing only a function after a release still needs version N → N+1 (no step file needed). Clients with N then get an upgrade that reruns functions/.
Neighbours of functions/
| You want | Put it in |
|---|---|
A view, a trigger, a GRANT, audit enablement | post/*.sql (runs every upgrade, as the owner; views CREATE OR REPLACE, triggers guarded with triggerexists). Le Sage's post/ must end with SELECT audit.sync() (90-audit.sql). |
| Reference rows (territories, SPRD…) | Edit lesage/data-src/<name>.csv (+ .json for table/key/types), then npm run check -- lint sql ../lesage/sql --write-data regenerates data/<name>.sql. |
| A table, column, index, constraint | A step: see Adding a column. |
No comments to display
No comments to display