- db.py: Ajout migration ALTER TABLE pour colonne client_negative_floor (correction du bug 'no such column' sur les bases existantes) - panel.py: Tous les boutons du panel avec emojis - panel.py: Correction TextSelect -> TextDisplay (panel crash)
93 lines
3 KiB
Python
93 lines
3 KiB
Python
import aiosqlite
|
|
import os
|
|
|
|
DB_PATH = os.path.join(os.path.dirname(__file__), "..", "data", "bank_v2.db")
|
|
|
|
SCHEMA = """
|
|
CREATE TABLE IF NOT EXISTS banks (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
guild_id INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
role_id INTEGER NOT NULL,
|
|
establishment_code INTEGER NOT NULL,
|
|
vault_balance REAL DEFAULT 0.0,
|
|
clients_total_balance REAL DEFAULT 0.0,
|
|
client_negative_floor REAL DEFAULT 0.0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE(guild_id, name),
|
|
UNIQUE(guild_id, establishment_code)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS accounts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
guild_id INTEGER NOT NULL,
|
|
bank_id INTEGER NOT NULL,
|
|
user_id INTEGER NOT NULL,
|
|
rib TEXT NOT NULL UNIQUE,
|
|
account_type TEXT NOT NULL CHECK(account_type IN ('particulier', 'entreprise')),
|
|
solde REAL DEFAULT 0.0,
|
|
tva_rate REAL DEFAULT 0.0,
|
|
password TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (bank_id) REFERENCES banks(id) ON DELETE CASCADE,
|
|
UNIQUE(guild_id, bank_id, user_id, account_type)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS institutions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
guild_id INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
role_id INTEGER,
|
|
can_audit_all INTEGER DEFAULT 0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE(guild_id, name)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS fisc_config (
|
|
guild_id INTEGER PRIMARY KEY,
|
|
institution_id INTEGER,
|
|
FOREIGN KEY (institution_id) REFERENCES institutions(id) ON DELETE SET NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS transactions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
guild_id INTEGER NOT NULL,
|
|
emetteur_rib TEXT,
|
|
destinataire_rib TEXT,
|
|
emetteur_id INTEGER,
|
|
destinataire_id INTEGER,
|
|
montant REAL NOT NULL,
|
|
type TEXT NOT NULL,
|
|
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
metadata TEXT
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_accounts_guild_user ON accounts(guild_id, user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_accounts_rib ON accounts(rib);
|
|
CREATE INDEX IF NOT EXISTS idx_accounts_bank ON accounts(guild_id, bank_id);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_guild ON transactions(guild_id);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_ribs ON transactions(emetteur_rib, destinataire_rib);
|
|
CREATE INDEX IF NOT EXISTS idx_banks_guild ON banks(guild_id);
|
|
CREATE INDEX IF NOT EXISTS idx_institutions_guild ON institutions(guild_id);
|
|
"""
|
|
|
|
|
|
MIGRATIONS = [
|
|
"ALTER TABLE banks ADD COLUMN client_negative_floor REAL DEFAULT 0.0",
|
|
]
|
|
|
|
|
|
async def get_db() -> aiosqlite.Connection:
|
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
db = await aiosqlite.connect(DB_PATH)
|
|
db.row_factory = aiosqlite.Row
|
|
await db.execute("PRAGMA journal_mode=WAL")
|
|
await db.execute("PRAGMA foreign_keys=ON")
|
|
await db.executescript(SCHEMA)
|
|
for migration in MIGRATIONS:
|
|
try:
|
|
await db.execute(migration)
|
|
except Exception:
|
|
pass
|
|
await db.commit()
|
|
return db
|