From 2982e77635f2b620a495b2d79677b2c7d1653d62 Mon Sep 17 00:00:00 2001 From: phernandez Date: Mon, 2 Dec 2024 21:09:20 -0600 Subject: [PATCH] rename db tables to singular --- ...000_rename_tables_and_add_entity_fields.sql | 17 +++++++++++++++++ db/schema.sql | 15 ++++++++------- src/basic_memory/models.py | 18 ++++++++++-------- 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 db/migrations/20240102000000_rename_tables_and_add_entity_fields.sql diff --git a/db/migrations/20240102000000_rename_tables_and_add_entity_fields.sql b/db/migrations/20240102000000_rename_tables_and_add_entity_fields.sql new file mode 100644 index 00000000..0a799daa --- /dev/null +++ b/db/migrations/20240102000000_rename_tables_and_add_entity_fields.sql @@ -0,0 +1,17 @@ +-- migrate:up + +-- Rename tables to singular form +ALTER TABLE entities RENAME TO entity; +ALTER TABLE observations RENAME TO observation; +ALTER TABLE relations RENAME TO relation; + +-- migrate:down + +-- Remove new columns from entity table +ALTER TABLE entity DROP COLUMN "references"; +ALTER TABLE entity DROP COLUMN description; + +-- Rename tables back to plural form +ALTER TABLE relation RENAME TO relations; +ALTER TABLE observation RENAME TO observations; +ALTER TABLE entity RENAME TO entities; diff --git a/db/schema.sql b/db/schema.sql index 9ce23a21..ad0c51a3 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS "schema_migrations" (version varchar(128) primary key); -CREATE TABLE entities ( +CREATE TABLE IF NOT EXISTS "entity" ( id TEXT PRIMARY KEY, -- timestamp-based ID (e.g., 20240101-entity-name) name TEXT NOT NULL, -- human readable name type TEXT NOT NULL, -- entity type (e.g., Person, Project, etc) @@ -8,26 +8,27 @@ CREATE TABLE entities ( description TEXT, -- main body content "references" TEXT -- reference list content ); -CREATE TABLE observations ( +CREATE TABLE IF NOT EXISTS "observation" ( id INTEGER PRIMARY KEY AUTOINCREMENT, entity_id TEXT NOT NULL, content TEXT NOT NULL, -- the actual observation text created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, context TEXT, -- where this observation came from - FOREIGN KEY (entity_id) REFERENCES entities(id) + FOREIGN KEY (entity_id) REFERENCES "entity"(id) ); -CREATE TABLE relations ( +CREATE TABLE IF NOT EXISTS "relation" ( id INTEGER PRIMARY KEY AUTOINCREMENT, from_entity_id TEXT NOT NULL, to_entity_id TEXT NOT NULL, relation_type TEXT NOT NULL, -- the verb describing the relationship context TEXT, -- optional context about the relationship created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (from_entity_id) REFERENCES entities(id), - FOREIGN KEY (to_entity_id) REFERENCES entities(id), + FOREIGN KEY (from_entity_id) REFERENCES "entity"(id), + FOREIGN KEY (to_entity_id) REFERENCES "entity"(id), -- Ensure we don't duplicate the exact same relationship UNIQUE(from_entity_id, to_entity_id, relation_type) ); -- Dbmate schema migrations INSERT INTO "schema_migrations" (version) VALUES - ('20240101000000'); + ('20240101000000'), + ('20240102000000'); diff --git a/src/basic_memory/models.py b/src/basic_memory/models.py index 24e70d2a..0bf905ed 100644 --- a/src/basic_memory/models.py +++ b/src/basic_memory/models.py @@ -1,6 +1,6 @@ from datetime import datetime, UTC from typing import List -from sqlalchemy import String, DateTime, ForeignKey +from sqlalchemy import String, DateTime, ForeignKey, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.ext.asyncio import AsyncAttrs from sqlalchemy.ext.declarative import declarative_base @@ -17,14 +17,16 @@ class Entity(AsyncAttrs, Base): - An entity type (e.g., "person", "organization", "event") - A description - A list of observations - - References (optional + - References (optional) """ - __tablename__ = "entities" + __tablename__ = "entity" # Primary key is a UUID string for compatibility with markdown IDs id: Mapped[str] = mapped_column(String, primary_key=True) name: Mapped[str] = mapped_column(String, unique=True, index=True) entity_type: Mapped[str] = mapped_column(String) + description: Mapped[str] = mapped_column(Text, nullable=False, default="") + references: Mapped[str] = mapped_column(Text, nullable=False, default="") created_at: Mapped[datetime] = mapped_column( DateTime, default=datetime.now(UTC) ) @@ -62,12 +64,12 @@ class Observation(AsyncAttrs, Base): - Can be added or removed independently - Should be atomic (one fact per observation) """ - __tablename__ = "observations" + __tablename__ = "observation" id: Mapped[str] = mapped_column(String, primary_key=True) entity_id: Mapped[str] = mapped_column( String, - ForeignKey("entities.id", ondelete="CASCADE"), + ForeignKey("entity.id", ondelete="CASCADE"), index=True ) content: Mapped[str] = mapped_column(String) @@ -89,17 +91,17 @@ class Relation(AsyncAttrs, Base): Relations define directed connections between entities. They are always stored in active voice and describe how entities interact or relate to each other. """ - __tablename__ = "relations" + __tablename__ = "relation" id: Mapped[str] = mapped_column(String, primary_key=True) from_id: Mapped[str] = mapped_column( String, - ForeignKey("entities.id", ondelete="CASCADE"), + ForeignKey("entity.id", ondelete="CASCADE"), index=True ) to_id: Mapped[str] = mapped_column( String, - ForeignKey("entities.id", ondelete="CASCADE"), + ForeignKey("entity.id", ondelete="CASCADE"), index=True ) relation_type: Mapped[str] = mapped_column(String)