Finance Table Cleanup
Related documentation: Finance Module Backend · Data ownership · Core Backend · Auth Backend
Purpose
Section titled “Purpose”This document defines which legacy tables must be removed from the Finance database once the platform migration is complete.
This page is not about what Finance historically had.
It is about:
- what now belongs to Core
- what now belongs to Auth
- what Finance still keeps temporarily for runtime compatibility
- what can be deleted from Finance immediately after migration
- what must be deleted later, only after Finance code stops using it
1. Ownership after migration
Section titled “1. Ownership after migration”1.1 Core-owned master data
Section titled “1.1 Core-owned master data”These tables belong to Platform Core:
CompanyBusinessUnit
Reason:
- company master data is platform organizational data
- business unit master data is platform organizational data
- neither table is finance-domain business data
Finance may reference company ids and business unit ids, but Finance should not be the long-term source of truth for those entities.
1.2 Auth-owned identity and access data
Section titled “1.2 Auth-owned identity and access data”These tables belong to Platform Auth:
UserAccountSessionVerificationTokenCompanyMembershipBusinessUnitMembership
Reason:
- identity belongs to Auth
- memberships belong to Auth
- access/session state belongs to Auth
- Finance must consume user/access context, not own it
1.3 Finance-owned domain data
Section titled “1.3 Finance-owned domain data”Finance should keep only finance-domain tables such as:
InvoicePaymentApprovalPolicyApprovalStepApprovalActionVendorVendorCompanyLinkCustomerCustomerCompanyLinkExpenseCategoryIncomeIncomePaymentXeroConnectionXeroSyncLogAuditLog
This list is illustrative, not exhaustive. The rule is:
if the table represents finance workflow or finance reporting data, it stays in Financeif the table represents platform organization, identity, membership, or session data, it does not belong in Finance2. Current Finance runtime reality
Section titled “2. Current Finance runtime reality”The current canonical Finance schema is:
The old legacy schema that still shows previously-owned identity and membership tables is:
Important current fact:
- the current canonical Finance schema no longer contains
User,Account,Session,VerificationToken,CompanyMembership, orBusinessUnitMembership - but the current Finance schema still contains
CompanyandBusinessUnit
That means the cleanup has two phases:
- remove legacy identity/access tables that are already no longer modeled in Finance
- remove
CompanyandBusinessUnitonly after Finance is fully refactored to read Core-owned company/business-unit master data
3. Tables to delete from Finance now
Section titled “3. Tables to delete from Finance now”These tables are no longer part of the canonical Finance schema and should be treated as legacy leftovers if they still exist in the Finance database.
They should be dropped after final verification that no raw SQL, views, triggers, ad-hoc scripts, or external jobs still depend on them.
3.1 Identity tables
Section titled “3.1 Identity tables”UserAccountSessionVerificationToken
Why they must be removed:
- identity is now owned by Auth
- session lifecycle is now owned by Auth
- Finance should not persist platform users or auth sessions
3.2 Membership tables
Section titled “3.2 Membership tables”CompanyMembershipBusinessUnitMembership
Why they must be removed:
- company memberships are now owned by Auth
- business unit memberships are now owned by Auth
- Finance must rely on Auth-provided access context and ids, not local membership rows
3.3 Current action classification
Section titled “3.3 Current action classification”These tables are:
- not part of the current canonical Finance schema
- safe cleanup targets
- remove now after verification
4. Tables to delete from Finance later
Section titled “4. Tables to delete from Finance later”These tables do not belong to Finance architecturally, but they are still used by the current Finance runtime and therefore cannot be dropped yet.
4.1 Organizational master tables
Section titled “4.1 Organizational master tables”CompanyBusinessUnit
Why they are not removable yet:
- current Finance code still queries
prisma.company - current Finance code still queries
prisma.businessUnit - current Finance admin, upload, stats, Kisum-link, and invoice/income flows still depend on those tables
Examples in current Finance code:
- src/api/admin/companies/route.ts
- src/api/admin/business-units/route.ts
- src/lib/active-company.ts
- src/lib/auth-service-http.ts
4.2 Condition for deletion
Section titled “4.2 Condition for deletion”Company and BusinessUnit may be removed from Finance only after:
- Finance no longer reads or writes those tables locally
- Finance resolves company/business-unit master data from Core
- all Finance routes and services that currently use Prisma company/BU reads have been migrated
- background jobs, import scripts, and reporting queries have been updated
4.3 Current action classification
Section titled “4.3 Current action classification”These tables are:
- architecturally misplaced in Finance
- still operationally required today
- remove later after code migration
5. Enums and related objects that must not be dropped blindly
Section titled “5. Enums and related objects that must not be dropped blindly”Even when legacy tables are removed, some enums may still be used by Finance workflow tables.
Important examples in the current Finance schema:
CompanyRoleBusinessUnitRole
These are still used by approval workflow tables, especially:
So the cleanup rule is:
do not drop enums just because a legacy table disappeareddrop an enum only after confirming no remaining table, column, constraint, or function still references it6. Final cleanup matrix
Section titled “6. Final cleanup matrix”| Table | Target owner | Finance status today | Cleanup action |
|---|---|---|---|
User | Auth | legacy only | remove now |
Account | Auth | legacy only | remove now |
Session | Auth | legacy only | remove now |
VerificationToken | Auth | legacy only | remove now |
CompanyMembership | Auth | legacy only | remove now |
BusinessUnitMembership | Auth | legacy only | remove now |
Company | Core | still used by Finance runtime | remove later |
BusinessUnit | Core | still used by Finance runtime | remove later |
7. Required verification before deletion
Section titled “7. Required verification before deletion”Before dropping any table from Finance, verify:
- no Prisma model still references it in the canonical schema
- no raw SQL migration, script, cron, or report still queries it
- no route handler still depends on it
- no foreign key from a retained finance-domain table still points to it
- no enum or function used by active finance tables is being dropped indirectly
For Company and BusinessUnit, deletion is blocked until Finance has completed the Core master-data migration.
8. Final rule
Section titled “8. Final rule”The Finance cleanup should follow this order:
- remove legacy Auth-owned tables from Finance
- keep
CompanyandBusinessUnittemporarily while Finance still uses them - migrate Finance runtime reads/writes to Core-owned master data
- then remove
CompanyandBusinessUnitfrom Finance
This is the only safe cleanup sequence.