# At launch: the connect rule and its messages

What `Sage.exe` does with the bundles at each launch (`TDM.ApplyBundles` → `RunConnectRule` in `catBundleUpgrade.pas`), and how to read what went wrong.

## Startup sequence

1. Read the parameters (`srv= owner= user= pwd= db=`, plus `sqlroot=` and `dev=1`), connect as the login.
2. Licence check.
3. **Connect rule**, one transaction: wait for the advisory lock (another PC may be upgrading: splash with *Annuler*, gives up after 5 minutes), read the ledger, decide, apply.
4. Write the legacy stamp `config.lastversiondbupdate` (only after *upToDate*/*upgraded*, never lowered).
5. Open the datasets, as the plain login.

[![The connect rule](https://docs.improvize.eu/uploads/images/gallery/2026-11/d3-connect.png)](https://docs.improvize.eu/uploads/images/gallery/2026-11/d3-connect.png)

## Roles

- `<owner>` (e.g. `izowner`): owns the tables. Steps, `data/` and `post/` run as this role.
- `<owner>_ddl`: owns the managed functions and the ledger, so an old exe running as `<owner>` can no longer drop or overwrite them. `functions/` runs as this role.
- Created per server by `common/roles/ddl-roles.sql` (operator, see `common/roles/RUNBOOK.md`); rerun it when a new login is added, or that login gets *refused* as soon as there is work to do.

## Messages and what they mean

<table id="bkmrk-resultcausefix-clien"><thead><tr><th>Result</th><th>Cause</th><th>Fix</th></tr></thead><tbody><tr><td>*clientBehind* "Quelqu'un de votre équipe utilise une version plus récente…"</td><td>The database has a higher bundle version than this exe ships.</td><td>Update the exe. On a test DB: lower `version` in `improvize.bundles`.</td></tr><tr><td>*refused* `noddlrole` / `notddlmember`</td><td>`<owner>_ddl` is missing, or the login isn't a member.</td><td>Run `ddl-roles.sql` on the server.</td></tr><tr><td>*refused* `ownersuperuser`</td><td>The owner role is a superuser (e.g. `owner=cat` locally).</td><td>Use a non-superuser owner role.</td></tr><tr><td>*failed* "L'installation de Le Sage est incomplète : réinstallez le programme."</td><td>A bundle folder is missing under `sqlroot` (default: the exe's folder).</td><td>Reinstall, or fix `sqlroot=`.</td></tr><tr><td>*failed* "cette base n'est pas une base Le Sage"</td><td>Step 0001's precondition raised `IZ001`: empty or non-Le Sage database.</td><td>Wrong `db=`; empty databases come from `lesage/makedb/` only.</td></tr><tr><td>*failed* `foreignowner` (repair query shown, with *Copier*)</td><td>A managed function is owned by someone other than `<owner>` / `<owner>_ddl`.</td><td>Run the shown `ALTER FUNCTION … OWNER TO` as superuser.</td></tr><tr><td>*failed* with a file and line</td><td>An SQL error in that bundle file. Everything was rolled back.</td><td>Fix the file (next version if it was released).</td></tr><tr><td>*failed* lock wait</td><td>Another PC held the upgrade lock for more than 5 minutes.</td><td>Retry; check `pg_stat_activity` (`application_name` = `lesage-delphi 13.000.xx (login@PC)`).</td></tr></tbody></table>

On *refused* and *failed* the user can choose *Lancer quand même* (runs with whatever the database has, some functions may fail) or *Quitter* (default).

## Inspecting a database

```sql
SELECT bundle, version, checksum, dirty, appliedat, appliedby FROM improvize.bundles;
SELECT bundle, fromversion, toversion, kind, appliedat, appliedby
  FROM improvize.bundlehistory ORDER BY id DESC LIMIT 20;   -- kind: upgrade | reinstall | reload
```

Compare a checksum with the files: `npm run check -- manifest ../lesage/sql` (in `common/`) prints the per-file manifest and the bundle checksum.