feat(supabase): animals and relations M3 with RPCs, history and caps
Species rows (8 + other), animals with the D6b birth model, relations with composite workspace keys, current/ended/voided states and a partial unique index; definer RPCs for animals and relations (one lock order, shared write budget, caps), invoker reads (search_animals, list_client_animals, client_archive_preview) and search_clients with current_animal_count. pgTAP 014 (254), parity test, sandbox suite, types from the isolated stack.
This commit is contained in:
1 parent
2dc04bcfba
commit
b54c8e5bd8
8 files changed
+3354
-11
No files matched your search
@@ -1,16 +1,20 @@
|
||||
/**
|
||||
* D-R7 parity: the constants of src/field-limits.ts behave like the SQL CHECK
|
||||
* constraints of public.clients, proven by inserting values at and past each
|
||||
* constraints of public.clients, public.animals and
|
||||
* public.animal_client_relations, proven by inserting values at and past each
|
||||
* bound into the migrated PGlite schema (contract §12.2). The form may refuse
|
||||
* more than SQL (never less): a value the TypeScript rule accepts must be
|
||||
* accepted by SQL, and the listed samples must agree both ways.
|
||||
*/
|
||||
import type { PGlite } from '@electric-sql/pglite'
|
||||
import {
|
||||
ANIMAL_FIELD_LIMITS,
|
||||
BIRTH_YEAR_MIN,
|
||||
CLIENT_FIELD_LIMITS,
|
||||
COUNTRY_CODE_PATTERN,
|
||||
EMAIL_PATTERN,
|
||||
PHONE_E164_PATTERN,
|
||||
RECORD_DATE_MIN,
|
||||
codePointLength,
|
||||
isCleanText,
|
||||
} from '../../src/field-limits'
|
||||
@@ -73,6 +77,52 @@ const textFields: [Column, number][] = [
|
||||
['city', CLIENT_FIELD_LIMITS.city],
|
||||
]
|
||||
|
||||
/** One animal row in a rolled-back transaction: does the schema accept these columns? */
|
||||
async function animalAccepts(
|
||||
columns: Record<string, string>,
|
||||
): Promise<boolean> {
|
||||
const row = { name: 'Parity', species_id: 'equine', ...columns }
|
||||
const names = Object.keys(row)
|
||||
await db.exec('begin')
|
||||
try {
|
||||
await db.query(
|
||||
`insert into public.animals (workspace_id, sex, create_request_id, create_request_digest, ${names.join(', ')})
|
||||
values ($1, 'unknown', gen_random_uuid(), '\\x00', ${names.map((_, i) => `$${i + 2}`).join(', ')})`,
|
||||
[workspaceId, ...Object.values(row)],
|
||||
)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
} finally {
|
||||
await db.exec('rollback')
|
||||
}
|
||||
}
|
||||
|
||||
/** One relation row (ended, so both dates are allowed) to a fresh animal and client. */
|
||||
async function relationAccepts(
|
||||
column: 'started_on' | 'ended_on',
|
||||
value: string,
|
||||
): Promise<boolean> {
|
||||
await db.exec('begin')
|
||||
try {
|
||||
await db.query(
|
||||
`with a as (insert into public.animals (workspace_id, name, species_id, sex, create_request_id, create_request_digest)
|
||||
values ($1, 'Parity', 'equine', 'unknown', gen_random_uuid(), '\\x00') returning id),
|
||||
c as (insert into public.clients (workspace_id, kind, display_name, create_request_id, create_request_digest)
|
||||
values ($1, 'person', 'Parity', gen_random_uuid(), '\\x00') returning id)
|
||||
insert into public.animal_client_relations (workspace_id, animal_id, client_id, relation_role, ended_at, ${column},
|
||||
create_request_id, create_request_digest)
|
||||
select $1, a.id, c.id, 'owner', now(), $2, gen_random_uuid(), '\\x00' from a, c`,
|
||||
[workspaceId, value],
|
||||
)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
} finally {
|
||||
await db.exec('rollback')
|
||||
}
|
||||
}
|
||||
|
||||
const tsEmail = (v: string) =>
|
||||
isCleanText(v, CLIENT_FIELD_LIMITS.email) &&
|
||||
v === v.toLowerCase() &&
|
||||
@@ -167,4 +217,64 @@ describe('field limits match the SQL constraints (D-R7)', () => {
|
||||
])
|
||||
}
|
||||
})
|
||||
|
||||
it.each([
|
||||
['name', ANIMAL_FIELD_LIMITS.name],
|
||||
['breed', ANIMAL_FIELD_LIMITS.breed],
|
||||
] as const)(
|
||||
'animals.%s: the text rule and its bound %i agree',
|
||||
async (column, max) => {
|
||||
for (const value of textSamples(max)) {
|
||||
expect([
|
||||
column,
|
||||
JSON.stringify(value).slice(0, 24),
|
||||
await animalAccepts({ [column]: value }),
|
||||
]).toEqual([
|
||||
column,
|
||||
JSON.stringify(value).slice(0, 24),
|
||||
isCleanText(value, max),
|
||||
])
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
it('animals.species_detail: the text rule and its bound agree (with other)', async () => {
|
||||
for (const value of textSamples(ANIMAL_FIELD_LIMITS.speciesDetail)) {
|
||||
expect([
|
||||
JSON.stringify(value).slice(0, 24),
|
||||
await animalAccepts({ species_id: 'other', species_detail: value }),
|
||||
]).toEqual([
|
||||
JSON.stringify(value).slice(0, 24),
|
||||
isCleanText(value, ANIMAL_FIELD_LIMITS.speciesDetail),
|
||||
])
|
||||
}
|
||||
})
|
||||
|
||||
it('birth year: the floor agrees', async () => {
|
||||
for (const year of [BIRTH_YEAR_MIN - 1, BIRTH_YEAR_MIN, 2015]) {
|
||||
expect([year, await animalAccepts({ birth_year: String(year) })]).toEqual(
|
||||
[year, year >= BIRTH_YEAR_MIN],
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('record dates: the floor agrees on birth and relation dates', async () => {
|
||||
const before = '1899-12-31'
|
||||
for (const value of [before, RECORD_DATE_MIN, '2015-04-12']) {
|
||||
const expected = value >= RECORD_DATE_MIN
|
||||
const year = value.slice(0, 4)
|
||||
expect([
|
||||
value,
|
||||
await animalAccepts({ birth_year: year, birth_date: value }),
|
||||
]).toEqual([value, expected])
|
||||
expect([value, await relationAccepts('started_on', value)]).toEqual([
|
||||
value,
|
||||
expected,
|
||||
])
|
||||
expect([value, await relationAccepts('ended_on', value)]).toEqual([
|
||||
value,
|
||||
expected,
|
||||
])
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user