Skip to content

Commit

Permalink
Migrate db to add an id column to message table
Browse files Browse the repository at this point in the history
  • Loading branch information
ggozad committed Sep 5, 2024
1 parent 0780e7e commit 1ada561
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]" }]
license = { text = "MIT" }
Expand Down
2 changes: 2 additions & 0 deletions src/oterm/store/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 2 additions & 0 deletions src/oterm/store/upgrades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,4 +15,5 @@
+ v0_2_8_upgrades
+ v0_3_0_upgrades
+ v0_4_0_upgrades
+ v0_5_1_upgrades
)
30 changes: 30 additions & 0 deletions src/oterm/store/upgrades/v0_5_1.py
Original file line number Diff line number Diff line change
@@ -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])
]

0 comments on commit 1ada561

Please sign in to comment.