From 1ada561e4531ee360ecc023954eae0adcd8fd806 Mon Sep 17 00:00:00 2001 From: Yiorgis Gozadinos Date: Thu, 5 Sep 2024 15:58:32 +0200 Subject: [PATCH] Migrate db to add an id column to message table --- pyproject.toml | 2 +- src/oterm/store/setup.py | 2 ++ src/oterm/store/upgrades/__init__.py | 2 ++ src/oterm/store/upgrades/v0_5_1.py | 30 ++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/oterm/store/upgrades/v0_5_1.py diff --git a/pyproject.toml b/pyproject.toml index d52df28..46388cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "oterm" -version = "0.5.0" +version = "0.5.1" description = "A text-based terminal client for Ollama." authors = [{ name = "Yiorgis Gozadinos", email = "ggozadinos@gmail.com" }] license = { text = "MIT" } diff --git a/src/oterm/store/setup.py b/src/oterm/store/setup.py index 6b5ceec..a383eab 100644 --- a/src/oterm/store/setup.py +++ b/src/oterm/store/setup.py @@ -15,9 +15,11 @@ -- name: create_message_table CREATE TABLE IF NOT EXISTS "message" ( + "id" INTEGER, "chat_id" INTEGER NOT NULL, "author" TEXT NOT NULL, "text" TEXT NOT NULL, + PRIMARY KEY("id" AUTOINCREMENT) FOREIGN KEY("chat_id") REFERENCES "chat"("id") ON DELETE CASCADE ); diff --git a/src/oterm/store/upgrades/__init__.py b/src/oterm/store/upgrades/__init__.py index 7c59016..30f9c7f 100644 --- a/src/oterm/store/upgrades/__init__.py +++ b/src/oterm/store/upgrades/__init__.py @@ -5,6 +5,7 @@ from oterm.store.upgrades.v0_2_8 import upgrades as v0_2_8_upgrades from oterm.store.upgrades.v0_3_0 import upgrades as v0_3_0_upgrades from oterm.store.upgrades.v0_4_0 import upgrades as v0_4_0_upgrades +from oterm.store.upgrades.v0_5_1 import upgrades as v0_5_1_upgrades upgrades = ( v0_1_6_upgrades @@ -14,4 +15,5 @@ + v0_2_8_upgrades + v0_3_0_upgrades + v0_4_0_upgrades + + v0_5_1_upgrades ) diff --git a/src/oterm/store/upgrades/v0_5_1.py b/src/oterm/store/upgrades/v0_5_1.py new file mode 100644 index 0000000..8f3c42c --- /dev/null +++ b/src/oterm/store/upgrades/v0_5_1.py @@ -0,0 +1,30 @@ +from pathlib import Path +from typing import Awaitable, Callable + +import aiosqlite + + +async def add_id_to_messages(db_path: Path) -> None: + print("KAKAKAKAAKAK") + async with aiosqlite.connect(db_path) as connection: + try: + await connection.executescript( + """ + CREATE TABLE message_temp ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + chat_id INTEGER NOT NULL, + author TEXT NOT NULL, + text TEXT NOT NULL + ); + INSERT INTO message_temp (chat_id, author, text) SELECT chat_id, author, text FROM message; + DROP TABLE message; + ALTER TABLE message_temp RENAME TO message; + """ + ) + except aiosqlite.OperationalError: + pass + + +upgrades: list[tuple[str, list[Callable[[Path], Awaitable[None]]]]] = [ + ("0.5.1", [add_id_to_messages]) +]