73-file atomic rename per docs/tenancies-design.md:
- Migration 0085: rename table + indexes + FK constraints; rename
documents.reservation_id → tenancy_id; migrate jsonb permission maps
(reservations resource → tenancies; collapse create+activate → manage);
rewrite historical audit_logs.entity_type='berth_reservation' →
'berth_tenancy'. FK renames wrapped in DO blocks so dev DBs that pre-date
the FK additions don't abort.
- Schema: berthReservations → berthTenancies; BerthReservation type →
BerthTenancy; indexes idx_br_* / idx_brr_* → idx_bt_*.
- RolePermissions: resource { view, create, activate, cancel } collapses to
{ view, manage, cancel }; all 8 default seed bundles + role-form + matrix
updated.
- Service: berth-reservations.service.ts → berth-tenancies.service.ts;
endReservation → endTenancy; listReservations → listTenancies.
- API: /api/v1/berth-reservations → /api/v1/tenancies (+ nested [id]);
/api/v1/berths/[id]/reservations → /api/v1/berths/[id]/tenancies.
- Validators: reservations.ts → tenancies.ts; RESERVATION_STATUSES →
TENANCY_STATUSES; endReservationSchema → endTenancySchema.
- Routes: /{portSlug}/berth-reservations → /{portSlug}/tenancies;
/portal/my-reservations → /portal/my-tenancies.
- Components: src/components/reservations/* → src/components/tenancies/*;
BerthReservationsTab → BerthTenanciesTab; ClientReservationsTab →
ClientTenanciesTab; ReservationList → TenancyList.
- Socket events: berth_reservation:* → berth_tenancy:*; payload
reservationId → tenancyId.
- Webhook events: berth_reservation.* → berth_tenancy.*.
- Portal: getPortalUserReservations → getPortalUserTenancies;
PortalReservation → PortalTenancy; PortalDashboard.counts.activeReservations
→ activeTenancies; PortalNav label "Reservations" → "Tenancies".
- Dossier: DossierReservation → DossierTenancy; reservationDecisions →
tenancyDecisions across smart-archive-dialog + bulk-archive routes.
- Documents schema: documents.reservationId → documents.tenancyId
(TS + DB column + index + FK constraint).
- Activity feed label berth_reservation → berth_tenancy (matched against
migrated historical audit rows).
KEPT (separate concepts):
- Reservation Agreement document type (the contract sent to clients).
- "Reservation" pipeline stage name.
- {{reservation.*}} merge tokens in template authoring.
- interest.reservationStatus / reservationDocStatus / dateReservationSent
fields (track agreement signing on the deal).
- reservation-agreement-context.ts service (builds merge context for the
Reservation Agreement doc; only its DB imports were renamed).
Verified: tsc clean, 1480/1480 vitest passing, migration applied.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
74 lines
5.0 KiB
TypeScript
74 lines
5.0 KiB
TypeScript
/**
|
|
* Vitest global setup. The teardown phase runs after the test run completes
|
|
* (whether passing or failing) and purges any `test-port-*` rows the
|
|
* `makePort()` factory created during the run, plus the dependent rows
|
|
* those ports own.
|
|
*
|
|
* Without this, integration tests leak hundreds of test ports per run; we
|
|
* once accumulated >17k such rows in dev, slowing every page load that
|
|
* fetches the port-switcher list.
|
|
*/
|
|
|
|
// `globalSetup` runs in vitest's parent process, so the test workers' env
|
|
// from `loadEnv` doesn't apply here - we have to load .env ourselves before
|
|
// importing the db module (which validates DATABASE_URL at import time).
|
|
import 'dotenv/config';
|
|
import { sql } from 'drizzle-orm';
|
|
import { db, closeDb } from '@/lib/db';
|
|
|
|
export async function setup() {
|
|
// No-op: per-suite setup happens inside individual test files.
|
|
}
|
|
|
|
export async function teardown() {
|
|
// Same DB as the dev/test environment - only delete obvious test rows
|
|
// (slug prefix is a marker the factory always sets).
|
|
await db.execute(sql`
|
|
-- Stage the doomed port ids
|
|
WITH doomed AS (
|
|
SELECT id FROM ports WHERE slug LIKE 'test-port-%'
|
|
)
|
|
-- Cascade-delete dependent rows. Order respects FK chains.
|
|
, del_audit AS (DELETE FROM audit_logs WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_bml AS (DELETE FROM berth_maintenance_log WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_resv AS (DELETE FROM berth_tenancies WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_caddr AS (DELETE FROM client_addresses WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_cmlog AS (DELETE FROM client_merge_log WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_crel AS (DELETE FROM client_relationships WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_compaddr AS (DELETE FROM company_addresses WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_comp AS (DELETE FROM companies WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_cfd AS (DELETE FROM custom_field_definitions WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_docs AS (DELETE FROM documents WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_dfold AS (DELETE FROM document_folders WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_dt AS (DELETE FROM document_templates WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_emt AS (DELETE FROM email_threads WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_ema AS (DELETE FROM email_accounts WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_exp AS (DELETE FROM expenses WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_files AS (DELETE FROM files WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_ft AS (DELETE FROM form_templates WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_gr AS (DELETE FROM generated_reports WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_int AS (DELETE FROM interests WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_ib AS (DELETE FROM interest_berths WHERE berth_id IN (SELECT id FROM berths WHERE port_id IN (SELECT id FROM doomed)) RETURNING 1)
|
|
, del_inv AS (DELETE FROM invoices WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_notif AS (DELETE FROM notifications WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_pro AS (DELETE FROM port_role_overrides WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_pu AS (DELETE FROM portal_users WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_rem AS (DELETE FROM reminders WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_rc AS (DELETE FROM residential_clients WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_ri AS (DELETE FROM residential_interests WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_sv AS (DELETE FROM saved_views WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_sr AS (DELETE FROM scheduled_reports WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_ss AS (DELETE FROM system_settings WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_tags AS (DELETE FROM tags WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_unp AS (DELETE FROM user_notification_preferences WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_upr AS (DELETE FROM user_port_roles WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_wh AS (DELETE FROM webhooks WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_yachts AS (DELETE FROM yachts WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_berths AS (DELETE FROM berths WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_clients AS (DELETE FROM clients WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
|
|
, del_ports AS (DELETE FROM ports WHERE id IN (SELECT id FROM doomed) RETURNING 1)
|
|
SELECT 1
|
|
`);
|
|
await closeDb();
|
|
}
|