mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-21 03:11:07 -05:00
fixed
This commit is contained in:
@@ -7,7 +7,8 @@ from typing import List
|
|||||||
|
|
||||||
logger = logging.getLogger("DBSchemaManager")
|
logger = logging.getLogger("DBSchemaManager")
|
||||||
|
|
||||||
class SchemaManager:
|
|
||||||
|
class DatabaseSchemaManager:
|
||||||
"""Manages database schema creation and updates"""
|
"""Manages database schema creation and updates"""
|
||||||
|
|
||||||
SCHEMA_VERSION = 1 # Increment when schema changes
|
SCHEMA_VERSION = 1 # Increment when schema changes
|
||||||
@@ -20,11 +21,11 @@ class SchemaManager:
|
|||||||
try:
|
try:
|
||||||
self._create_schema_version_table()
|
self._create_schema_version_table()
|
||||||
current_version = self._get_schema_version()
|
current_version = self._get_schema_version()
|
||||||
|
|
||||||
if current_version < self.SCHEMA_VERSION:
|
if current_version < self.SCHEMA_VERSION:
|
||||||
self._apply_migrations(current_version)
|
self._apply_migrations(current_version)
|
||||||
self._update_schema_version()
|
self._update_schema_version()
|
||||||
|
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
logger.error(f"Schema initialization error: {e}")
|
logger.error(f"Schema initialization error: {e}")
|
||||||
raise
|
raise
|
||||||
@@ -33,11 +34,13 @@ class SchemaManager:
|
|||||||
"""Create schema version tracking table"""
|
"""Create schema version tracking table"""
|
||||||
with sqlite3.connect(self.db_path) as conn:
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
CREATE TABLE IF NOT EXISTS schema_version (
|
CREATE TABLE IF NOT EXISTS schema_version (
|
||||||
version INTEGER PRIMARY KEY
|
version INTEGER PRIMARY KEY
|
||||||
)
|
)
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
# Insert initial version if table is empty
|
# Insert initial version if table is empty
|
||||||
cursor.execute("INSERT OR IGNORE INTO schema_version VALUES (0)")
|
cursor.execute("INSERT OR IGNORE INTO schema_version VALUES (0)")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@@ -55,15 +58,14 @@ class SchemaManager:
|
|||||||
with sqlite3.connect(self.db_path) as conn:
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE schema_version SET version = ?",
|
"UPDATE schema_version SET version = ?", (self.SCHEMA_VERSION,)
|
||||||
(self.SCHEMA_VERSION,)
|
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
def _apply_migrations(self, current_version: int) -> None:
|
def _apply_migrations(self, current_version: int) -> None:
|
||||||
"""Apply necessary schema migrations"""
|
"""Apply necessary schema migrations"""
|
||||||
migrations = self._get_migrations(current_version)
|
migrations = self._get_migrations(current_version)
|
||||||
|
|
||||||
with sqlite3.connect(self.db_path) as conn:
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
for migration in migrations:
|
for migration in migrations:
|
||||||
@@ -77,10 +79,11 @@ class SchemaManager:
|
|||||||
def _get_migrations(self, current_version: int) -> List[str]:
|
def _get_migrations(self, current_version: int) -> List[str]:
|
||||||
"""Get list of migrations to apply"""
|
"""Get list of migrations to apply"""
|
||||||
migrations = []
|
migrations = []
|
||||||
|
|
||||||
# Version 0 to 1: Initial schema
|
# Version 0 to 1: Initial schema
|
||||||
if current_version < 1:
|
if current_version < 1:
|
||||||
migrations.append("""
|
migrations.append(
|
||||||
|
"""
|
||||||
CREATE TABLE IF NOT EXISTS archived_videos (
|
CREATE TABLE IF NOT EXISTS archived_videos (
|
||||||
original_url TEXT PRIMARY KEY,
|
original_url TEXT PRIMARY KEY,
|
||||||
discord_url TEXT NOT NULL,
|
discord_url TEXT NOT NULL,
|
||||||
@@ -100,10 +103,11 @@ class SchemaManager:
|
|||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_archived_at
|
CREATE INDEX IF NOT EXISTS idx_archived_at
|
||||||
ON archived_videos(archived_at);
|
ON archived_videos(archived_at);
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
# Add more migrations here as schema evolves
|
# Add more migrations here as schema evolves
|
||||||
# if current_version < 2:
|
# if current_version < 2:
|
||||||
# migrations.append(...)
|
# migrations.append(...)
|
||||||
|
|
||||||
return migrations
|
return migrations
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import logging
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Dict, Any, List
|
from typing import Optional, Dict, Any, List
|
||||||
|
|
||||||
from .schema_manager import SchemaManager
|
from .schema_manager import DatabaseSchemaManager
|
||||||
from .query_manager import DatabaseQueryManager
|
from .query_manager import DatabaseQueryManager
|
||||||
from .connection_manager import ConnectionManager
|
from .connection_manager import DatabaseConnectionManager
|
||||||
|
|
||||||
logger = logging.getLogger("VideoArchiverDB")
|
logger = logging.getLogger("VideoArchiverDB")
|
||||||
|
|
||||||
@@ -25,8 +25,8 @@ class VideoArchiveDB:
|
|||||||
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# Initialize managers
|
# Initialize managers
|
||||||
self.connection_manager = ConnectionManager(self.db_path)
|
self.connection_manager = DatabaseConnectionManager(self.db_path)
|
||||||
self.schema_manager = SchemaManager(self.db_path)
|
self.schema_manager = DatabaseSchemaManager(self.db_path)
|
||||||
self.query_manager = DatabaseQueryManager(self.connection_manager)
|
self.query_manager = DatabaseQueryManager(self.connection_manager)
|
||||||
|
|
||||||
# Initialize database schema
|
# Initialize database schema
|
||||||
|
|||||||
Reference in New Issue
Block a user