up
This commit is contained in:
+26
-1
@@ -11,15 +11,23 @@ type DB struct {
|
||||
}
|
||||
|
||||
func Init(path string) (*DB, error) {
|
||||
sqldb, err := sql.Open("sqlite", path) // "sqlite" au lieu de "sqlite3"
|
||||
sqldb, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Une seule connexion — évite les SQLITE_BUSY entre goroutines concurrentes
|
||||
sqldb.SetMaxOpenConns(1)
|
||||
sqldb.SetMaxIdleConns(1)
|
||||
|
||||
if err := sqldb.Ping(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// WAL : lectures non bloquantes + timeout 10s avant BUSY
|
||||
sqldb.Exec("PRAGMA journal_mode=WAL")
|
||||
sqldb.Exec("PRAGMA busy_timeout=10000")
|
||||
|
||||
database := &DB{sqldb}
|
||||
if err := database.migrate(); err != nil {
|
||||
return nil, err
|
||||
@@ -114,6 +122,9 @@ func (db *DB) migrate() error {
|
||||
}
|
||||
}
|
||||
|
||||
// Nettoyage : supprime les signaux watchlist pour les tickers retirés de la watchlist
|
||||
db.Exec(`DELETE FROM signals WHERE source='watchlist' AND ticker NOT IN (SELECT ticker FROM watchlist WHERE active=1)`)
|
||||
|
||||
// Migrations additives — on ignore les erreurs si la colonne/index existe déjà
|
||||
additive := []string{
|
||||
`ALTER TABLE news ADD COLUMN finnhub_id INTEGER`,
|
||||
@@ -130,6 +141,20 @@ func (db *DB) migrate() error {
|
||||
`CREATE INDEX IF NOT EXISTS idx_instruments_ticker ON instruments(ticker)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_signals_score ON signals(score DESC)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_signals_source ON signals(source)`,
|
||||
`ALTER TABLE signals ADD COLUMN insider_sell_value_30d REAL DEFAULT 0`,
|
||||
`ALTER TABLE signals ADD COLUMN earnings_date TEXT DEFAULT ''`,
|
||||
`CREATE TABLE IF NOT EXISTS company_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
ticker TEXT NOT NULL,
|
||||
event_type TEXT NOT NULL,
|
||||
title TEXT,
|
||||
accession_no TEXT UNIQUE,
|
||||
filing_date DATE,
|
||||
filing_url TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_company_events_ticker ON company_events(ticker)`,
|
||||
`ALTER TABLE signals ADD COLUMN ceo_change INTEGER DEFAULT 0`,
|
||||
}
|
||||
for _, q := range additive {
|
||||
db.Exec(q) // intentionnellement sans vérification d'erreur
|
||||
|
||||
Reference in New Issue
Block a user