Les choses sérieuses commencent
This commit is contained in:
parent
7a9fe18463
commit
c63f62721b
41 changed files with 1270 additions and 0 deletions
0
erminig/models/__init__.py
Normal file
0
erminig/models/__init__.py
Normal file
26
erminig/models/db.py
Normal file
26
erminig/models/db.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import sqlite3
|
||||
from erminig.config import Config
|
||||
|
||||
|
||||
def init_db():
|
||||
if Config.DB_PATH.exists():
|
||||
return
|
||||
|
||||
conn = sqlite3.connect(Config.DB_PATH)
|
||||
with open(Config.BASE_DIR / "schema.sql", "r") as f:
|
||||
conn.executescript(f.read())
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Base erminig.db initialisée avec succès.")
|
||||
|
||||
|
||||
class ErminigDB:
|
||||
def __enter__(self):
|
||||
self.conn = sqlite3.connect(Config.DB_PATH)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
self.cursor = self.conn.cursor()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.conn.commit()
|
||||
self.conn.close()
|
||||
29
erminig/models/upstreams.py
Normal file
29
erminig/models/upstreams.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from erminig.models.db import ErminigDB
|
||||
|
||||
|
||||
def upsert_upstream(db: ErminigDB, name, type_, url, pattern, file):
|
||||
db.cursor.execute(
|
||||
"""
|
||||
INSERT INTO upstreams (name, type, url, pattern, file, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
type=excluded.type,
|
||||
url=excluded.url,
|
||||
pattern=excluded.pattern,
|
||||
file=excluded.file
|
||||
RETURNING id
|
||||
""",
|
||||
(name, type_, url, pattern, file),
|
||||
)
|
||||
row = db.cursor.fetchone()
|
||||
return row[0] if row else None
|
||||
|
||||
|
||||
def get_all_upstreams(db: ErminigDB):
|
||||
db.cursor.execute(
|
||||
"""
|
||||
SELECT id, name, type, url, pattern, file FROM upstreams
|
||||
"""
|
||||
)
|
||||
rows = db.cursor.fetchall()
|
||||
return rows
|
||||
25
erminig/models/versions.py
Normal file
25
erminig/models/versions.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from erminig.models.db import ErminigDB
|
||||
|
||||
|
||||
def insert_version_if_new(db, upstream_id, version, url):
|
||||
db.cursor.execute(
|
||||
"""
|
||||
SELECT id FROM versions
|
||||
WHERE upstream_id = ? AND version = ?
|
||||
""",
|
||||
(upstream_id, version),
|
||||
)
|
||||
|
||||
if db.cursor.fetchone():
|
||||
return False # déjà en base
|
||||
|
||||
db.cursor.execute(
|
||||
"""
|
||||
INSERT INTO versions (upstream_id, version, url)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(upstream_id, version, url),
|
||||
)
|
||||
|
||||
print(f"[DB] Nouvelle version insérée pour {upstream_id} : {version}")
|
||||
return True
|
||||
Loading…
Add table
Add a link
Reference in a new issue