Remove db.json migration logic
This commit is contained in:
parent
a8fe6ed7b3
commit
a5c5f5b077
@ -8,9 +8,8 @@ def row_to_dict(row):
|
|||||||
|
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
from schema import init_db as _init_db, migrate_from_tinydb
|
from schema import init_db as _init_db
|
||||||
_init_db()
|
_init_db()
|
||||||
migrate_from_tinydb()
|
|
||||||
|
|
||||||
|
|
||||||
def get_user_by_username(username):
|
def get_user_by_username(username):
|
||||||
|
|||||||
46
schema.py
46
schema.py
@ -1,6 +1,5 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
import os
|
||||||
import json
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
@ -69,48 +68,3 @@ def init_db():
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def migrate_from_tinydb():
|
|
||||||
json_path = os.path.join(os.path.dirname(config.DB_PATH), "db.json")
|
|
||||||
if not os.path.exists(json_path):
|
|
||||||
return
|
|
||||||
|
|
||||||
conn = get_connection()
|
|
||||||
try:
|
|
||||||
with open(json_path, "r") as f:
|
|
||||||
data = json.load(f)
|
|
||||||
|
|
||||||
if "users" in data:
|
|
||||||
for doc_id, record in data["users"].items():
|
|
||||||
record["id"] = int(doc_id)
|
|
||||||
if conn.execute("SELECT COUNT(*) FROM users WHERE id = ?", (record["id"],)).fetchone()[0] == 0:
|
|
||||||
conn.execute(
|
|
||||||
"INSERT INTO users (id, username, password_hash, role, max_goals) VALUES (?, ?, ?, ?, ?)",
|
|
||||||
(record["id"], record["username"], record["password_hash"], record["role"], record["max_goals"])
|
|
||||||
)
|
|
||||||
|
|
||||||
if "goals" in data:
|
|
||||||
for doc_id, record in data["goals"].items():
|
|
||||||
record["id"] = int(doc_id)
|
|
||||||
if conn.execute("SELECT COUNT(*) FROM goals WHERE id = ?", (record["id"],)).fetchone()[0] == 0:
|
|
||||||
conn.execute(
|
|
||||||
"INSERT INTO goals (id, user_id, title, activated) VALUES (?, ?, ?, ?)",
|
|
||||||
(record["id"], record["user_id"], record["title"], 1 if record.get("activated", True) else 0)
|
|
||||||
)
|
|
||||||
|
|
||||||
if "tasks" in data:
|
|
||||||
for doc_id, record in data["tasks"].items():
|
|
||||||
record["id"] = int(doc_id)
|
|
||||||
if conn.execute("SELECT COUNT(*) FROM tasks WHERE id = ?", (record["id"],)).fetchone()[0] == 0:
|
|
||||||
conn.execute(
|
|
||||||
"""INSERT INTO tasks (id, goal_id, title, desc, status, start_time, finished_time, "order")
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
||||||
(record["id"], record["goal_id"], record["title"], record.get("desc", ""),
|
|
||||||
record.get("status", "todo"), record.get("start_time"), record.get("finished_time"),
|
|
||||||
record.get("order", 0.0))
|
|
||||||
)
|
|
||||||
|
|
||||||
conn.commit()
|
|
||||||
finally:
|
|
||||||
conn.close()
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user