Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:53:39 +08:00
commit f52a5ade52
29 changed files with 3131 additions and 0 deletions

View File

@@ -0,0 +1 @@
Generic single-database configuration.

View File

@@ -0,0 +1,89 @@
from logging.config import fileConfig
from pathlib import Path
import os
import sys
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# Add parent directory to path to import our models
sys.path.insert(0, str(Path(__file__).parent.parent))
from src.candlekeep.db.models import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Set database URL programmatically
candlekeep_dir = Path.home() / ".candlekeep"
db_path = candlekeep_dir / "candlekeep.db"
config.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,30 @@
"""add_table_of_contents_field
Revision ID: 350115ea15b8
Revises: e5ffbf97468e
Create Date: 2025-11-01 17:03:15.297500
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '350115ea15b8'
down_revision: Union[str, Sequence[str], None] = 'e5ffbf97468e'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# Add table_of_contents JSON column
op.add_column('books', sa.Column('table_of_contents', sa.JSON(), nullable=True))
def downgrade() -> None:
"""Downgrade schema."""
# Remove table_of_contents column
op.drop_column('books', 'table_of_contents')

View File

@@ -0,0 +1,79 @@
"""Initial schema
Revision ID: e5ffbf97468e
Revises:
Create Date: 2025-11-01 11:55:11.896876
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'e5ffbf97468e'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('books',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('title', sa.String(length=500), nullable=False),
sa.Column('author', sa.String(length=255), nullable=True),
sa.Column('original_file_path', sa.String(length=1000), nullable=False),
sa.Column('markdown_file_path', sa.String(length=1000), nullable=False),
sa.Column('source_type', sa.Enum('PDF', 'MARKDOWN', name='sourcetype'), nullable=False),
sa.Column('file_hash', sa.String(length=64), nullable=False),
sa.Column('added_date', sa.DateTime(), nullable=False),
sa.Column('modified_date', sa.DateTime(), nullable=False),
sa.Column('pdf_creation_date', sa.DateTime(), nullable=True),
sa.Column('pdf_mod_date', sa.DateTime(), nullable=True),
sa.Column('pdf_creator', sa.String(length=255), nullable=True),
sa.Column('pdf_producer', sa.String(length=255), nullable=True),
sa.Column('page_count', sa.Integer(), nullable=True),
sa.Column('word_count', sa.Integer(), nullable=True),
sa.Column('chapter_count', sa.Integer(), nullable=True),
sa.Column('subject', sa.String(length=500), nullable=True),
sa.Column('keywords', sa.Text(), nullable=True),
sa.Column('category', sa.String(length=100), nullable=True),
sa.Column('tags', sa.JSON(), nullable=True),
sa.Column('isbn', sa.String(length=20), nullable=True),
sa.Column('publisher', sa.String(length=255), nullable=True),
sa.Column('publication_year', sa.Integer(), nullable=True),
sa.Column('language', sa.String(length=10), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('file_hash')
)
op.create_index(op.f('ix_books_author'), 'books', ['author'], unique=False)
op.create_index(op.f('ix_books_category'), 'books', ['category'], unique=False)
op.create_index(op.f('ix_books_source_type'), 'books', ['source_type'], unique=False)
op.create_index(op.f('ix_books_title'), 'books', ['title'], unique=False)
op.create_table('book_notes',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('book_id', sa.Integer(), nullable=False),
sa.Column('note_type', sa.Enum('SUMMARY', 'REVIEW', 'TAG', 'OTHER', name='notetype'), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('created_date', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['book_id'], ['books.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_book_type', 'book_notes', ['book_id', 'note_type'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('idx_book_type', table_name='book_notes')
op.drop_table('book_notes')
op.drop_index(op.f('ix_books_title'), table_name='books')
op.drop_index(op.f('ix_books_source_type'), table_name='books')
op.drop_index(op.f('ix_books_category'), table_name='books')
op.drop_index(op.f('ix_books_author'), table_name='books')
op.drop_table('books')
# ### end Alembic commands ###