rename db tables to singular

This commit is contained in:
phernandez
2024-12-02 21:09:20 -06:00
parent fe24e09075
commit 2982e77635
3 changed files with 35 additions and 15 deletions
@@ -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;
+8 -7
View File
@@ -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');
+10 -8
View File
@@ -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)