Repository navigation
Description
Tracking issue for making apso import → apso generate lossless on an existing Postgres database. This is the onboarding path for any team that already has a database, and it blocks our first customer on a HIPAA business tier.
Why this came up
We're onboarding a healthcare SaaS customer onto Apso. Their app runs on an existing 80-table production Postgres schema, built by hand-written migrations over about seven months. The plan is to move their data layer onto Apso: apso import supabase, then apso generate, then serve the existing database through the generated service.
We ran that end to end locally with CLI 0.50.1, against a copy of the schema loaded with synthetic data. Then we compared the schema the generated service builds against the real one, object by object. Anything that doesn't survive that round trip means the generated service would change or drop production schema the moment it's pointed at the real database.
What we measured
We compared the source schema with the schema the generated service creates (CLI 0.50.1):
| Object | Source | After import and generate | Cause |
|---|---|---|---|
| Foreign-key columns | patient_id |
patientId (87 of 88 renamed) |
#241 |
created_at / updated_at |
timestamptz |
timestamp (about 100 columns) |
#243 |
| Indexes | 176 | 114 differ: 66 on renamed FKs, 35 partial, 4 expression, 9 template bugs | #241, #245, #249, #248 |
| Check constraints | 44 | 0 | #246 |
| Column defaults | 27 lost, 10 changed (70 more are gen_random_uuid() vs uuid_generate_v4(), same behavior) |
#247 |
On top of that, apso schema lint --fix silently changes what unique constraints mean (#244), and the importer has smaller gaps (#250). Separately, the generated TypeScript service starts with 91 known production-dependency vulnerabilities, 29 of them high (apsoai/service-template-ts#47). That needs fixing before Apso is offered as a HIPAA tier.
Done when
- joinColumnName is ignored for ManyToOne, so importing an existing database renames its foreign keys #241
joinColumnNamehonored for ManyToOne and OneToOne, and set by the importer - created_at and updated_at lose their time zone (generated as timestamp, not timestamptz) #243
created_at/updated_atkeeptimestamptz - apso schema lint --fix drops foreign-key columns from unique constraints and changes what they mean #244
lint --fixnever removes a column from an index or unique constraint - Index name and where in .apsorc (partial indexes, keep existing index names) #245 index
nameandwhere(partial indexes) - Check constraints in .apsorc: values, min and max shorthands with API validation, and a named checks list #246 named check constraints,
values,min/max, and acheckslist - Column defaults: SQL expressions and JSON literals are lost, and booleans get a forced default #247 SQL and JSON defaults, and booleans without a forced default
- unique and index flags ignored on uuid and timestamptz fields, and the created_at index is dropped #248
uniqueandindexhonored on every field type - Expression indexes on import (or document normalize-on-write) #249 expression indexes (lower priority, see the issue)
- Importer fidelity: language key, collision fix renames a column, arrays become text, composite keys, silent drops #250 importer fidelity: names, arrays,
language, collision handling - Generated service starts with 91 production dependency vulnerabilities (29 high) service-template-ts#47 service template dependency vulnerabilities
- A round-trip test in CI: import a fixture schema, generate, build the schema, diff against the source. The only allowed differences are documented ones.
Scope note: what belongs in the database
Not everything in a hand-built schema should be carried into .apsorc as-is. Of the 44 check constraints, about 11 are true integrity invariants: date ordering, positive counts, access-grant shape. 25 are status and kind value lists that read better as values and generate API validation. 7 are business rules that belong in application code, because they encode yearly-changing measure codes and an encryption envelope format. So Apso needs a small, explicit checks escape hatch, not a full constraint language. The docs should say which rules belong where.
Activity
fix: import keeps mixed timestamp types, renames a colliding relation…
feat: onDelete keeps a foreign key's SET NULL or RESTRICT rule on import
feat: import keeps timestamp nullability, key column types and constr…
feat: import keeps array element types, reports generated columns; in…
feat: a foreign key can be the primary key (primary on a relationship)
fix: timestamp and date fields can be primary keys; imported uniques …
fix: apsorc.schema.json describes what the CLI accepts, and a test ke…
feat: indexes keep their names and WHERE conditions (#245), flags on …
feat: check constraints in .apsorc and on import (#246)
feat: SQL and JSON defaults, no forced boolean default, uuid key defa…
fix: partial-index value lists and indexes beside a unique constraint…
fix: sync exit code, uuid keys on PGlite, and a switch to stop CREATE…
feat: lossless import of an existing Postgres database, first pass (#241
cultron commentedon Nov 24, 2026
Status after @apso/cli 0.53.0 (#251), @apso/schema-tools 0.2.3 and service-template-ts v2.1.3: #241, #243, #244, #245, #247, #248 and #249 are closed. Stored generated columns also import now, as a
generatedfield setting (TypeScript only).Still open: the check shorthands in #246, composite keys in the API (#250), and a second foreign key on one column, which the importer reports and does not carry.
cultron commentedon Nov 25, 2026
Shipped in 0.55.0 (#262): a second foreign key on a column survives an import, as the entity setting
foreignKeys. The importer writes an entry only when it can state the key exactly (target table imported, key points at the target's primary key, rule is NO ACTION, CASCADE, SET NULL or RESTRICT); any other second key is a line in the import report. Still open under this issue: #246 (check shorthands) and #250 (composite keys).cultron commentedon Nov 25, 2026
Shipped in 0.56.0 (#263): a foreign key keeps its ON UPDATE rule, SET DEFAULT, DEFERRABLE and its column's default on import (relationship settings
onUpdate,deferrable,joinColumnDefault); a key onto a table whose primary key is a relationship references that column; a key into a table that was not imported, another schema, or a column that is not the target's primary key is a line in the import report with its column kept. Still open under this issue: #246 (check shorthands) and #250 (composite keys).cultron commentedon Nov 25, 2026
Status, 2026-11-25 (later):
.apsorcreference, the schema guides and the migrate guide cover every key the import writes, and steps 1 to 4 of the guide were rerun on@apso/cli0.57.0.apso import datafails on any table with an array column and rounds timestamps to the millisecond. fix: apso import data copies array columns and keeps a timestamp's microseconds #265 fixes both by passing values as the text Postgres prints. It is open and not merged.bigintor identityidimports asserial; auuidprimary key with no default gainsuuid_generate_v4(); an enum type is renamed to<table>_<column>_enum.Still open from before: #246 (check shorthands) and #250 (composite keys).
cultron commentedon Nov 25, 2026
Status, 2026-11-25 (evening): #265 is merged and published as
@apso/cli0.57.1.apso import datanow copies a table with an array column and keeps timestamps to the microsecond. The migrate guide is updated (apsoai/docs#24).Still open: a
bigintor identityidimports asserial, auuidprimary key with no default gains one, an enum type is renamed, #246 (check shorthands) and #250 (composite keys).cultron commentedon Nov 25, 2026
Status, 2026-11-25 (night):
"primaryKeyType": "bigserial"), a uuid id with no default ("primaryKeyDefault": null) and an enum type's name (enumName).apso test smokefailed for the whole service.Still open: routes that address a row by a composite key, which need
@apso/crud(its routes are fixed at/:id) and a decision on the URL shape, and the check shorthands in #246.