From bcc02fbd78706dc849e99bf0ff79715fa9c18dc1 Mon Sep 17 00:00:00 2001 From: Linus Miller Date: Tue, 13 Jan 2026 09:49:03 +0100 Subject: [PATCH] add invoice unique constraints and rename tables --- bin/add_fisken_invoices.ts | 2 +- bin/add_phm_invoices.ts | 2 +- docker/postgres/003_accounting_schema.sql | 98 +++++++++++++---------- docker/postgres/dump.py | 6 +- server/lib/parse_stream.ts | 2 +- server/routes/api/invoices.ts | 4 +- server/routes/api/objects.ts | 2 +- shared/types.db.ts | 12 +-- 8 files changed, 72 insertions(+), 56 deletions(-) diff --git a/bin/add_fisken_invoices.ts b/bin/add_fisken_invoices.ts index 3f167b7..4758ab7 100644 --- a/bin/add_fisken_invoices.ts +++ b/bin/add_fisken_invoices.ts @@ -65,7 +65,7 @@ async function readdir(dir: string) { } const file = await trx.insertInto('file').values({ filename }).returning('id').executeTakeFirstOrThrow() - await trx.insertInto('filesToInvoice').values({ fileId: file.id, invoiceId: invoice.id }).execute() + await trx.insertInto('files_invoice').values({ fileId: file.id, invoiceId: invoice.id }).execute() } await trx.commit().execute() diff --git a/bin/add_phm_invoices.ts b/bin/add_phm_invoices.ts index 9d7c894..c857f4b 100644 --- a/bin/add_phm_invoices.ts +++ b/bin/add_phm_invoices.ts @@ -68,7 +68,7 @@ for (const row of rows.toReversed()) { } const file = await trx.insertInto('file').values({ filename }).returning('id').executeTakeFirstOrThrow() - await trx.insertInto('filesToInvoice').values({ fileId: file.id, invoiceId: invoice.id }).execute() + await trx.insertInto('files_invoice').values({ fileId: file.id, invoiceId: invoice.id }).execute() } } diff --git a/docker/postgres/003_accounting_schema.sql b/docker/postgres/003_accounting_schema.sql index e168781..d5f9144 100644 --- a/docker/postgres/003_accounting_schema.sql +++ b/docker/postgres/003_accounting_schema.sql @@ -2,7 +2,7 @@ -- PostgreSQL database dump -- -\restrict kNYhdwOhwE9I3bgAzdljyYgB5xyEpjhiaSCeYZfp84v3ey1GpvsdxX4U8Y8fQM3 +\restrict wU1dfnHqRTt2y70XvbaA1LDda0PgjnckSPP98WqYNl91ofZctLJ9wjIOEE8bpwR -- Dumped from database version 18.1 -- Dumped by pg_dump version 18.1 @@ -71,10 +71,10 @@ ALTER SEQUENCE public.account_id_seq OWNED BY public.account.id; -- --- Name: aliasesToSupplier; Type: TABLE; Schema: public; Owner: - +-- Name: aliases_supplier; Type: TABLE; Schema: public; Owner: - -- -CREATE TABLE public."aliasesToSupplier" ( +CREATE TABLE public.aliases_supplier ( id integer NOT NULL, "supplierId" integer NOT NULL, alias text NOT NULL @@ -82,10 +82,10 @@ CREATE TABLE public."aliasesToSupplier" ( -- --- Name: aliasesToSupplier_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- Name: aliases_supplier_id_seq; Type: SEQUENCE; Schema: public; Owner: - -- -CREATE SEQUENCE public."aliasesToSupplier_id_seq" +CREATE SEQUENCE public.aliases_supplier_id_seq AS integer START WITH 1 INCREMENT BY 1 @@ -95,10 +95,10 @@ CREATE SEQUENCE public."aliasesToSupplier_id_seq" -- --- Name: aliasesToSupplier_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- Name: aliases_supplier_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - -- -ALTER SEQUENCE public."aliasesToSupplier_id_seq" OWNED BY public."aliasesToSupplier".id; +ALTER SEQUENCE public.aliases_supplier_id_seq OWNED BY public.aliases_supplier.id; -- @@ -199,10 +199,10 @@ ALTER SEQUENCE public.file_id_seq OWNED BY public.file.id; -- --- Name: filesToInvoice; Type: TABLE; Schema: public; Owner: - +-- Name: files_invoice; Type: TABLE; Schema: public; Owner: - -- -CREATE TABLE public."filesToInvoice" ( +CREATE TABLE public.files_invoice ( "invoiceId" integer NOT NULL, "fileId" integer NOT NULL ); @@ -442,10 +442,10 @@ ALTER SEQUENCE public.transaction_id_seq OWNED BY public.transaction.id; -- --- Name: transactionsToObjects; Type: TABLE; Schema: public; Owner: - +-- Name: transactions_objects; Type: TABLE; Schema: public; Owner: - -- -CREATE TABLE public."transactionsToObjects" ( +CREATE TABLE public.transactions_objects ( "transactionId" integer NOT NULL, "objectId" integer NOT NULL ); @@ -459,10 +459,10 @@ ALTER TABLE ONLY public.account ALTER COLUMN id SET DEFAULT nextval('public.acco -- --- Name: aliasesToSupplier id; Type: DEFAULT; Schema: public; Owner: - +-- Name: aliases_supplier id; Type: DEFAULT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."aliasesToSupplier" ALTER COLUMN id SET DEFAULT nextval('public."aliasesToSupplier_id_seq"'::regclass); +ALTER TABLE ONLY public.aliases_supplier ALTER COLUMN id SET DEFAULT nextval('public.aliases_supplier_id_seq'::regclass); -- @@ -560,19 +560,19 @@ ALTER TABLE ONLY public.account -- --- Name: aliasesToSupplier aliasesToSupplier_alias_key; Type: CONSTRAINT; Schema: public; Owner: - +-- Name: aliases_supplier aliases_supplier_alias_key; Type: CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."aliasesToSupplier" - ADD CONSTRAINT "aliasesToSupplier_alias_key" UNIQUE (alias); +ALTER TABLE ONLY public.aliases_supplier + ADD CONSTRAINT aliases_supplier_alias_key UNIQUE (alias); -- --- Name: aliasesToSupplier aliasesToSupplier_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- Name: aliases_supplier aliases_supplier_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."aliasesToSupplier" - ADD CONSTRAINT "aliasesToSupplier_pkey" PRIMARY KEY (id); +ALTER TABLE ONLY public.aliases_supplier + ADD CONSTRAINT aliases_supplier_pkey PRIMARY KEY (id); -- @@ -600,11 +600,11 @@ ALTER TABLE ONLY public.file -- --- Name: filesToInvoice filesToInvoice_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- Name: files_invoice files_invoice_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."filesToInvoice" - ADD CONSTRAINT "filesToInvoice_pkey" PRIMARY KEY ("invoiceId", "fileId"); +ALTER TABLE ONLY public.files_invoice + ADD CONSTRAINT files_invoice_pkey PRIMARY KEY ("invoiceId", "fileId"); -- @@ -631,6 +631,22 @@ ALTER TABLE ONLY public."financialYear" ADD CONSTRAINT "financialYear_year_key" UNIQUE (year); +-- +-- Name: invoice invoice_fiskenNumber_key; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.invoice + ADD CONSTRAINT "invoice_fiskenNumber_key" UNIQUE ("fiskenNumber"); + + +-- +-- Name: invoice invoice_phmNumber_key; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.invoice + ADD CONSTRAINT "invoice_phmNumber_key" UNIQUE ("phmNumber"); + + -- -- Name: invoice invoice_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -696,35 +712,35 @@ ALTER TABLE ONLY public.transaction -- --- Name: transactionsToObjects transactionsToObjects_transactionId_objectId_key; Type: CONSTRAINT; Schema: public; Owner: - +-- Name: transactions_objects transactions_objects_transactionId_objectId_key; Type: CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."transactionsToObjects" - ADD CONSTRAINT "transactionsToObjects_transactionId_objectId_key" UNIQUE ("transactionId", "objectId"); +ALTER TABLE ONLY public.transactions_objects + ADD CONSTRAINT "transactions_objects_transactionId_objectId_key" UNIQUE ("transactionId", "objectId"); -- --- Name: aliasesToSupplier aliasesToSupplier_supplierId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- Name: aliases_supplier aliases_supplier_supplierId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."aliasesToSupplier" - ADD CONSTRAINT "aliasesToSupplier_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES public.supplier(id); +ALTER TABLE ONLY public.aliases_supplier + ADD CONSTRAINT "aliases_supplier_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES public.supplier(id); -- --- Name: filesToInvoice filesToInvoice_fileId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- Name: files_invoice files_invoice_fileId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."filesToInvoice" - ADD CONSTRAINT "filesToInvoice_fileId_fkey" FOREIGN KEY ("fileId") REFERENCES public.file(id); +ALTER TABLE ONLY public.files_invoice + ADD CONSTRAINT "files_invoice_fileId_fkey" FOREIGN KEY ("fileId") REFERENCES public.file(id); -- --- Name: filesToInvoice filesToInvoice_invoiceId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- Name: files_invoice files_invoice_invoiceId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."filesToInvoice" - ADD CONSTRAINT "filesToInvoice_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES public.invoice(id); +ALTER TABLE ONLY public.files_invoice + ADD CONSTRAINT "files_invoice_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES public.invoice(id); -- @@ -784,24 +800,24 @@ ALTER TABLE ONLY public.transaction -- --- Name: transactionsToObjects transactionsToObjects_objectId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- Name: transactions_objects transactions_objects_objectId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."transactionsToObjects" - ADD CONSTRAINT "transactionsToObjects_objectId_fkey" FOREIGN KEY ("objectId") REFERENCES public.object(id); +ALTER TABLE ONLY public.transactions_objects + ADD CONSTRAINT "transactions_objects_objectId_fkey" FOREIGN KEY ("objectId") REFERENCES public.object(id); -- --- Name: transactionsToObjects transactionsToObjects_transactionId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- Name: transactions_objects transactions_objects_transactionId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public."transactionsToObjects" - ADD CONSTRAINT "transactionsToObjects_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES public.transaction(id); +ALTER TABLE ONLY public.transactions_objects + ADD CONSTRAINT "transactions_objects_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES public.transaction(id); -- -- PostgreSQL database dump complete -- -\unrestrict kNYhdwOhwE9I3bgAzdljyYgB5xyEpjhiaSCeYZfp84v3ey1GpvsdxX4U8Y8fQM3 +\unrestrict wU1dfnHqRTt2y70XvbaA1LDda0PgjnckSPP98WqYNl91ofZctLJ9wjIOEE8bpwR diff --git a/docker/postgres/dump.py b/docker/postgres/dump.py index dca92f8..1cb2454 100755 --- a/docker/postgres/dump.py +++ b/docker/postgres/dump.py @@ -20,11 +20,11 @@ auth_tables = [ accounting_tables = [ 'account', 'accountBalance', - 'aliasesToSupplier', + 'aliases_supplier', 'dimension', 'entry', 'file', - 'filesToInvoice', + 'files_invoice', 'financialYear', 'invoice', 'journal', @@ -32,7 +32,7 @@ accounting_tables = [ 'supplier', 'supplierType', 'transaction', - 'transactionsToObjects', + 'transactions_objects', ] parser = argparse.ArgumentParser() diff --git a/server/lib/parse_stream.ts b/server/lib/parse_stream.ts index 67d39bf..45d6fba 100644 --- a/server/lib/parse_stream.ts +++ b/server/lib/parse_stream.ts @@ -321,7 +321,7 @@ export default async function parseStream(stream: ReadableStream, decoder: Decod } await trx - .insertInto('transactionsToObjects') + .insertInto('transactions_objects') .values({ transactionId, objectId, diff --git a/server/routes/api/invoices.ts b/server/routes/api/invoices.ts index a04acb6..45cd347 100644 --- a/server/routes/api/invoices.ts +++ b/server/routes/api/invoices.ts @@ -31,7 +31,7 @@ const invoiceRoutes: FastifyPluginCallbackZod = (fastify, _, done) => { jsonArrayFrom( eb .selectFrom('file as f') - .innerJoin('filesToInvoice as fi', 'f.id', 'fi.fileId') + .innerJoin('files_invoice as fi', 'f.id', 'fi.fileId') .select(['id', 'filename']) .whereRef('fi.invoiceId', '=', 'i.id'), ).as('files'), @@ -89,7 +89,7 @@ const invoiceRoutes: FastifyPluginCallbackZod = (fastify, _, done) => { jsonArrayFrom( eb .selectFrom('file as f') - .innerJoin('filesToInvoice as fi', 'f.id', 'fi.fileId') + .innerJoin('files_invoice as fi', 'f.id', 'fi.fileId') .select(['id', 'filename']) .whereRef('fi.invoiceId', '=', 'i.id'), ).as('files'), diff --git a/server/routes/api/objects.ts b/server/routes/api/objects.ts index 8ad7b4d..de37078 100644 --- a/server/routes/api/objects.ts +++ b/server/routes/api/objects.ts @@ -28,7 +28,7 @@ const objectRoutes: FastifyPluginCallbackZod = (fastify, _, done) => { async handler(request) { return db .selectFrom('transaction as t') - .innerJoin('transactionsToObjects as to', 't.id', 'to.transactionId') + .innerJoin('transactions_objects as to', 't.id', 'to.transactionId') .innerJoin('entry as e', 'e.id', 't.entryId') .select(['t.entryId', 'e.transactionDate', 't.accountNumber', 't.amount']) .where('to.objectId', '=', request.params.id) diff --git a/shared/types.db.ts b/shared/types.db.ts index 05caf51..d80bade 100644 --- a/shared/types.db.ts +++ b/shared/types.db.ts @@ -55,7 +55,7 @@ export interface AdmissionsRoles { roleId: number } -export interface AliasesToSupplier { +export interface AliasesSupplier { alias: string id: Generated supplierId: number @@ -108,7 +108,7 @@ export interface File { id: Generated } -export interface FilesToInvoice { +export interface FilesInvoice { fileId: number invoiceId: number } @@ -206,7 +206,7 @@ export interface Transaction { transactionDate: Timestamp | null } -export interface TransactionsToObjects { +export interface TransactionsObjects { objectId: number transactionId: number } @@ -237,13 +237,13 @@ export interface DB { accountBalance: AccountBalance admission: Admission admissions_roles: AdmissionsRoles - aliasesToSupplier: AliasesToSupplier + aliases_Supplier: AliasesSupplier dimension: Dimension emailToken: EmailToken entry: Entry error: Error file: File - filesToInvoice: FilesToInvoice + files_invoice: FilesInvoice financialYear: FinancialYear invite: Invite invites_roles: InvitesRoles @@ -255,7 +255,7 @@ export interface DB { supplier: Supplier supplierType: SupplierType transaction: Transaction - transactionsToObjects: TransactionsToObjects + transactions_objects: TransactionsObjects user: User users_roles: UsersRoles }