Squash 16 migrations into single clean initial migration
Since Castle extension has not been released yet, squash all database migrations for cleaner initial deployments. This reduces migration complexity and improves maintainability. Changes: - Squash migrations m001-m016 into single m001_initial migration - Reduced from 651 lines (16 functions) to 327 lines (1 function) - 50% code reduction, 93.75% fewer migration functions Final database schema (7 tables): - castle_accounts: Chart of accounts with 40+ default accounts - castle_extension_settings: Castle configuration - castle_user_wallet_settings: User wallet associations - castle_manual_payment_requests: Payment approval workflow - castle_balance_assertions: Reconciliation with Beancount integration - castle_user_equity_status: Equity eligibility tracking - castle_account_permissions: Granular access control Tables removed (intentionally): - castle_journal_entries: Now managed by Fava/Beancount (dropped in m016) - castle_entry_lines: Now managed by Fava/Beancount (dropped in m016) New migration includes: - All 7 tables in their final state - All indexes properly prefixed with idx_castle_ - All foreign key constraints - 40+ default accounts with hierarchical names (Assets:Bitcoin:Lightning, etc.) - Comprehensive documentation Files: - migrations.py: Single clean m001_initial migration - migrations_old.py.bak: Backup of original 16 migrations for reference - MIGRATION_SQUASH_SUMMARY.md: Complete documentation of squash process Benefits: - Simpler initial deployments (1 migration instead of 16) - Easier to understand final schema - Faster migration execution - Cleaner codebase See MIGRATION_SQUASH_SUMMARY.md for full details and testing instructions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
461cf08a69
commit
9ac3494f1b
3 changed files with 1016 additions and 471 deletions
651
migrations_old.py.bak
Normal file
651
migrations_old.py.bak
Normal file
|
|
@ -0,0 +1,651 @@
|
|||
async def m001_initial(db):
|
||||
"""
|
||||
Initial migration for Castle accounting extension.
|
||||
Creates tables for double-entry bookkeeping system.
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE accounts (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
account_type TEXT NOT NULL,
|
||||
description TEXT,
|
||||
user_id TEXT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_accounts_user_id ON accounts (user_id);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_accounts_type ON accounts (account_type);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE journal_entries (
|
||||
id TEXT PRIMARY KEY,
|
||||
description TEXT NOT NULL,
|
||||
entry_date TIMESTAMP NOT NULL,
|
||||
created_by TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
|
||||
reference TEXT
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_journal_entries_created_by ON journal_entries (created_by);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_journal_entries_date ON journal_entries (entry_date);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE entry_lines (
|
||||
id TEXT PRIMARY KEY,
|
||||
journal_entry_id TEXT NOT NULL,
|
||||
account_id TEXT NOT NULL,
|
||||
debit INTEGER NOT NULL DEFAULT 0,
|
||||
credit INTEGER NOT NULL DEFAULT 0,
|
||||
description TEXT,
|
||||
metadata TEXT DEFAULT '{{}}'
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_entry_lines_journal_entry ON entry_lines (journal_entry_id);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_entry_lines_account ON entry_lines (account_id);
|
||||
"""
|
||||
)
|
||||
|
||||
# Insert default chart of accounts
|
||||
default_accounts = [
|
||||
# Assets
|
||||
("cash", "Cash", "asset", "Cash on hand"),
|
||||
("bank", "Bank Account", "asset", "Bank account"),
|
||||
("lightning", "Lightning Balance", "asset", "Lightning Network balance"),
|
||||
("accounts_receivable", "Accounts Receivable", "asset", "Money owed to the Castle"),
|
||||
|
||||
# Liabilities
|
||||
("accounts_payable", "Accounts Payable", "liability", "Money owed by the Castle"),
|
||||
|
||||
# Equity
|
||||
("member_equity", "Member Equity", "equity", "Member contributions"),
|
||||
("retained_earnings", "Retained Earnings", "equity", "Accumulated profits"),
|
||||
|
||||
# Revenue
|
||||
("accommodation_revenue", "Accommodation Revenue", "revenue", "Revenue from stays"),
|
||||
("service_revenue", "Service Revenue", "revenue", "Revenue from services"),
|
||||
("other_revenue", "Other Revenue", "revenue", "Other revenue"),
|
||||
|
||||
# Expenses
|
||||
("utilities", "Utilities", "expense", "Electricity, water, internet"),
|
||||
("food", "Food & Supplies", "expense", "Food and supplies"),
|
||||
("maintenance", "Maintenance", "expense", "Repairs and maintenance"),
|
||||
("other_expense", "Other Expenses", "expense", "Miscellaneous expenses"),
|
||||
]
|
||||
|
||||
for acc_id, name, acc_type, desc in default_accounts:
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO accounts (id, name, account_type, description)
|
||||
VALUES (:id, :name, :type, :description)
|
||||
""",
|
||||
{"id": acc_id, "name": name, "type": acc_type, "description": desc}
|
||||
)
|
||||
|
||||
|
||||
async def m002_extension_settings(db):
|
||||
"""
|
||||
Create extension_settings table for Castle configuration.
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE extension_settings (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
castle_wallet_id TEXT,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m003_user_wallet_settings(db):
|
||||
"""
|
||||
Create user_wallet_settings table for per-user wallet configuration.
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE user_wallet_settings (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
user_wallet_id TEXT,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m004_manual_payment_requests(db):
|
||||
"""
|
||||
Create manual_payment_requests table for user payment requests to Castle.
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE manual_payment_requests (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
amount INTEGER NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
|
||||
reviewed_at TIMESTAMP,
|
||||
reviewed_by TEXT,
|
||||
journal_entry_id TEXT
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_manual_payment_requests_user_id ON manual_payment_requests (user_id);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_manual_payment_requests_status ON manual_payment_requests (status);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m005_add_flag_and_meta(db):
|
||||
"""
|
||||
Add flag and meta columns to journal_entries table.
|
||||
- flag: Transaction status (* = cleared, ! = pending, # = flagged, x = void)
|
||||
- meta: JSON metadata for audit trail (source, tags, links, notes)
|
||||
"""
|
||||
await db.execute(
|
||||
"""
|
||||
ALTER TABLE journal_entries ADD COLUMN flag TEXT DEFAULT '*';
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
ALTER TABLE journal_entries ADD COLUMN meta TEXT DEFAULT '{}';
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m006_hierarchical_account_names(db):
|
||||
"""
|
||||
Migrate account names to hierarchical Beancount-style format.
|
||||
- "Cash" → "Assets:Cash"
|
||||
- "Accounts Receivable" → "Assets:Receivable"
|
||||
- "Food & Supplies" → "Expenses:Food:Supplies"
|
||||
- "Accounts Receivable - af983632" → "Assets:Receivable:User-af983632"
|
||||
"""
|
||||
from .account_utils import migrate_account_name
|
||||
from .models import AccountType
|
||||
|
||||
# Get all existing accounts
|
||||
accounts = await db.fetchall("SELECT * FROM accounts")
|
||||
|
||||
# Mapping of old names to new names
|
||||
name_mappings = {
|
||||
# Assets
|
||||
"cash": "Assets:Cash",
|
||||
"bank": "Assets:Bank",
|
||||
"lightning": "Assets:Bitcoin:Lightning",
|
||||
"accounts_receivable": "Assets:Receivable",
|
||||
|
||||
# Liabilities
|
||||
"accounts_payable": "Liabilities:Payable",
|
||||
|
||||
# Equity
|
||||
"member_equity": "Equity:MemberEquity",
|
||||
"retained_earnings": "Equity:RetainedEarnings",
|
||||
|
||||
# Revenue → Income
|
||||
"accommodation_revenue": "Income:Accommodation",
|
||||
"service_revenue": "Income:Service",
|
||||
"other_revenue": "Income:Other",
|
||||
|
||||
# Expenses
|
||||
"utilities": "Expenses:Utilities",
|
||||
"food": "Expenses:Food:Supplies",
|
||||
"maintenance": "Expenses:Maintenance",
|
||||
"other_expense": "Expenses:Other",
|
||||
}
|
||||
|
||||
# Update default accounts using ID-based mapping
|
||||
for old_id, new_name in name_mappings.items():
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET name = :new_name
|
||||
WHERE id = :old_id
|
||||
""",
|
||||
{"new_name": new_name, "old_id": old_id}
|
||||
)
|
||||
|
||||
# Update user-specific accounts (those with user_id set)
|
||||
user_accounts = await db.fetchall(
|
||||
"SELECT * FROM accounts WHERE user_id IS NOT NULL"
|
||||
)
|
||||
|
||||
for account in user_accounts:
|
||||
# Parse account type
|
||||
account_type = AccountType(account["account_type"])
|
||||
|
||||
# Migrate name
|
||||
new_name = migrate_account_name(account["name"], account_type)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET name = :new_name
|
||||
WHERE id = :id
|
||||
""",
|
||||
{"new_name": new_name, "id": account["id"]}
|
||||
)
|
||||
|
||||
|
||||
async def m007_balance_assertions(db):
|
||||
"""
|
||||
Create balance_assertions table for reconciliation.
|
||||
Allows admins to assert expected balances at specific dates.
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE balance_assertions (
|
||||
id TEXT PRIMARY KEY,
|
||||
date TIMESTAMP NOT NULL,
|
||||
account_id TEXT NOT NULL,
|
||||
expected_balance_sats INTEGER NOT NULL,
|
||||
expected_balance_fiat TEXT,
|
||||
fiat_currency TEXT,
|
||||
tolerance_sats INTEGER DEFAULT 0,
|
||||
tolerance_fiat TEXT DEFAULT '0',
|
||||
checked_balance_sats INTEGER,
|
||||
checked_balance_fiat TEXT,
|
||||
difference_sats INTEGER,
|
||||
difference_fiat TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
created_by TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
|
||||
checked_at TIMESTAMP,
|
||||
FOREIGN KEY (account_id) REFERENCES accounts (id)
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_balance_assertions_account_id ON balance_assertions (account_id);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_balance_assertions_status ON balance_assertions (status);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_balance_assertions_date ON balance_assertions (date);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m008_rename_lightning_account(db):
|
||||
"""
|
||||
Rename Lightning account from Assets:Lightning:Balance to Assets:Bitcoin:Lightning
|
||||
for better naming consistency.
|
||||
"""
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET name = 'Assets:Bitcoin:Lightning'
|
||||
WHERE name = 'Assets:Lightning:Balance'
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m009_add_onchain_bitcoin_account(db):
|
||||
"""
|
||||
Add Assets:Bitcoin:OnChain account for on-chain Bitcoin transactions.
|
||||
This allows tracking on-chain Bitcoin separately from Lightning Network payments.
|
||||
"""
|
||||
import uuid
|
||||
|
||||
# Check if the account already exists
|
||||
existing = await db.fetchone(
|
||||
"""
|
||||
SELECT id FROM accounts
|
||||
WHERE name = 'Assets:Bitcoin:OnChain'
|
||||
"""
|
||||
)
|
||||
|
||||
if not existing:
|
||||
# Create the on-chain Bitcoin asset account
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO accounts (id, name, account_type, description, created_at)
|
||||
VALUES (:id, :name, :type, :description, {db.timestamp_now})
|
||||
""",
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"name": "Assets:Bitcoin:OnChain",
|
||||
"type": "asset",
|
||||
"description": "On-chain Bitcoin wallet"
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def m010_user_equity_status(db):
|
||||
"""
|
||||
Create user_equity_status table for managing equity contribution eligibility.
|
||||
Only equity-eligible users can convert their expenses to equity contributions.
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE user_equity_status (
|
||||
user_id TEXT PRIMARY KEY,
|
||||
is_equity_eligible BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
equity_account_name TEXT,
|
||||
notes TEXT,
|
||||
granted_by TEXT NOT NULL,
|
||||
granted_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
|
||||
revoked_at TIMESTAMP
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_user_equity_status_eligible
|
||||
ON user_equity_status (is_equity_eligible)
|
||||
WHERE is_equity_eligible = TRUE;
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m011_account_permissions(db):
|
||||
"""
|
||||
Create account_permissions table for granular account access control.
|
||||
Allows admins to grant specific permissions (read, submit_expense, manage) to users for specific accounts.
|
||||
Supports hierarchical permission inheritance (permissions on parent accounts cascade to children).
|
||||
"""
|
||||
await db.execute(
|
||||
f"""
|
||||
CREATE TABLE account_permissions (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
account_id TEXT NOT NULL,
|
||||
permission_type TEXT NOT NULL,
|
||||
granted_by TEXT NOT NULL,
|
||||
granted_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
|
||||
expires_at TIMESTAMP,
|
||||
notes TEXT,
|
||||
FOREIGN KEY (account_id) REFERENCES accounts (id)
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
# Index for looking up permissions by user
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_account_permissions_user_id ON account_permissions (user_id);
|
||||
"""
|
||||
)
|
||||
|
||||
# Index for looking up permissions by account
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_account_permissions_account_id ON account_permissions (account_id);
|
||||
"""
|
||||
)
|
||||
|
||||
# Composite index for checking specific user+account permissions
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_account_permissions_user_account
|
||||
ON account_permissions (user_id, account_id);
|
||||
"""
|
||||
)
|
||||
|
||||
# Index for finding permissions by type
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_account_permissions_type ON account_permissions (permission_type);
|
||||
"""
|
||||
)
|
||||
|
||||
# Index for finding expired permissions
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_account_permissions_expires
|
||||
ON account_permissions (expires_at)
|
||||
WHERE expires_at IS NOT NULL;
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m012_update_default_accounts(db):
|
||||
"""
|
||||
Update default chart of accounts to include more detailed hierarchical structure.
|
||||
Adds new accounts for fixed assets, livestock, equity contributions, and detailed expenses.
|
||||
Only adds accounts that don't already exist.
|
||||
"""
|
||||
import uuid
|
||||
from .account_utils import DEFAULT_HIERARCHICAL_ACCOUNTS
|
||||
|
||||
for name, account_type, description in DEFAULT_HIERARCHICAL_ACCOUNTS:
|
||||
# Check if account already exists
|
||||
existing = await db.fetchone(
|
||||
"""
|
||||
SELECT id FROM accounts WHERE name = :name
|
||||
""",
|
||||
{"name": name}
|
||||
)
|
||||
|
||||
if not existing:
|
||||
# Create new account
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO accounts (id, name, account_type, description, created_at)
|
||||
VALUES (:id, :name, :type, :description, {db.timestamp_now})
|
||||
""",
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"name": name,
|
||||
"type": account_type.value,
|
||||
"description": description
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def m013_remove_parent_only_accounts(db):
|
||||
"""
|
||||
Remove parent-only accounts from the database.
|
||||
|
||||
Since Castle doesn't interface directly with Beancount (only exports to it),
|
||||
we don't need parent accounts that exist only for organizational hierarchy.
|
||||
The hierarchy is implicit in the colon-separated account names.
|
||||
|
||||
When exporting to Beancount, the parent accounts will be inferred from the
|
||||
hierarchical naming (e.g., "Assets:Bitcoin:Lightning" implies "Assets:Bitcoin" exists).
|
||||
|
||||
This keeps our database clean and prevents accidentally posting to parent accounts.
|
||||
|
||||
Removes:
|
||||
- Assets:Bitcoin (parent of Lightning and OnChain)
|
||||
- Equity (parent of user equity accounts like Equity:User-xxx)
|
||||
"""
|
||||
# Remove Assets:Bitcoin (parent account)
|
||||
await db.execute(
|
||||
"DELETE FROM accounts WHERE name = :name",
|
||||
{"name": "Assets:Bitcoin"}
|
||||
)
|
||||
|
||||
# Remove Equity (parent account)
|
||||
await db.execute(
|
||||
"DELETE FROM accounts WHERE name = :name",
|
||||
{"name": "Equity"}
|
||||
)
|
||||
|
||||
|
||||
async def m014_remove_legacy_equity_accounts(db):
|
||||
"""
|
||||
Remove legacy generic equity accounts that don't fit the user-specific equity model.
|
||||
|
||||
The castle extension uses dynamic user-specific equity accounts (Equity:User-{user_id})
|
||||
created automatically when granting equity eligibility. Generic equity accounts like
|
||||
MemberEquity and RetainedEarnings are not needed.
|
||||
|
||||
Removes:
|
||||
- Equity:MemberEquity
|
||||
- Equity:RetainedEarnings
|
||||
"""
|
||||
# Remove Equity:MemberEquity
|
||||
await db.execute(
|
||||
"DELETE FROM accounts WHERE name = :name",
|
||||
{"name": "Equity:MemberEquity"}
|
||||
)
|
||||
|
||||
# Remove Equity:RetainedEarnings
|
||||
await db.execute(
|
||||
"DELETE FROM accounts WHERE name = :name",
|
||||
{"name": "Equity:RetainedEarnings"}
|
||||
)
|
||||
|
||||
|
||||
async def m015_convert_to_single_amount_field(db):
|
||||
"""
|
||||
Convert entry_lines from separate debit/credit columns to single amount field.
|
||||
|
||||
This aligns Castle with Beancount's elegant design:
|
||||
- Positive amount = debit (increase assets/expenses, decrease liabilities/equity/revenue)
|
||||
- Negative amount = credit (decrease assets/expenses, increase liabilities/equity/revenue)
|
||||
|
||||
Benefits:
|
||||
- Simpler model (one field instead of two)
|
||||
- Direct compatibility with Beancount import/export
|
||||
- Eliminates invalid states (both debit and credit non-zero)
|
||||
- More intuitive for programmers (positive/negative instead of accounting conventions)
|
||||
|
||||
Migration formula: amount = debit - credit
|
||||
|
||||
Examples:
|
||||
- Expense transaction:
|
||||
* Expenses:Food:Groceries amount=+100 (debit)
|
||||
* Liabilities:Payable:User amount=-100 (credit)
|
||||
- Payment transaction:
|
||||
* Liabilities:Payable:User amount=+100 (debit)
|
||||
* Assets:Bitcoin:Lightning amount=-100 (credit)
|
||||
"""
|
||||
from sqlalchemy.exc import OperationalError
|
||||
|
||||
# Step 1: Add new amount column (nullable for migration)
|
||||
try:
|
||||
await db.execute(
|
||||
"ALTER TABLE entry_lines ADD COLUMN amount INTEGER"
|
||||
)
|
||||
except OperationalError:
|
||||
# Column might already exist if migration was partially run
|
||||
pass
|
||||
|
||||
# Step 2: Populate amount from existing debit/credit
|
||||
# Formula: amount = debit - credit
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE entry_lines
|
||||
SET amount = debit - credit
|
||||
WHERE amount IS NULL
|
||||
"""
|
||||
)
|
||||
|
||||
# Step 3: Create new table with amount field as NOT NULL
|
||||
# SQLite doesn't support ALTER COLUMN, so we need to recreate the table
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE TABLE entry_lines_new (
|
||||
id TEXT PRIMARY KEY,
|
||||
journal_entry_id TEXT NOT NULL,
|
||||
account_id TEXT NOT NULL,
|
||||
amount INTEGER NOT NULL,
|
||||
description TEXT,
|
||||
metadata TEXT DEFAULT '{}'
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
# Step 4: Copy data from old table to new
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO entry_lines_new (id, journal_entry_id, account_id, amount, description, metadata)
|
||||
SELECT id, journal_entry_id, account_id, amount, description, metadata
|
||||
FROM entry_lines
|
||||
"""
|
||||
)
|
||||
|
||||
# Step 5: Drop old table and rename new one
|
||||
await db.execute("DROP TABLE entry_lines")
|
||||
await db.execute("ALTER TABLE entry_lines_new RENAME TO entry_lines")
|
||||
|
||||
# Step 6: Recreate indexes
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_entry_lines_journal_entry ON entry_lines (journal_entry_id)
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE INDEX idx_entry_lines_account ON entry_lines (account_id)
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m016_drop_obsolete_journal_tables(db):
|
||||
"""
|
||||
Drop journal_entries and entry_lines tables.
|
||||
|
||||
Castle now uses Fava/Beancount as the single source of truth for accounting data.
|
||||
These tables are no longer written to or read from.
|
||||
|
||||
All journal entry operations now:
|
||||
- Write: Submit to Fava via FavaClient.add_entry()
|
||||
- Read: Query Fava via FavaClient.get_entries()
|
||||
|
||||
Migration completed as part of Castle extension cleanup (Nov 2025).
|
||||
No backwards compatibility concerns - user explicitly approved.
|
||||
"""
|
||||
# Drop entry_lines first (has foreign key to journal_entries)
|
||||
await db.execute("DROP TABLE IF EXISTS entry_lines")
|
||||
|
||||
# Drop journal_entries
|
||||
await db.execute("DROP TABLE IF EXISTS journal_entries")
|
||||
Loading…
Add table
Add a link
Reference in a new issue