| """ |
| Database Initialization Script |
| Creates all tables and seeds Communities and Collections based on UNILAG structure. |
| """ |
|
|
| import os |
| import sys |
|
|
| |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
|
|
| from uraas.database import Collection, Community, SessionLocal, init_db, sync_schema_columns |
|
|
| |
| |
| |
| import uraas.services.citation_tracker |
|
|
| from uraas.utils.unilag_classifier import UNILAG_STRUCTURE |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _SCHEMA_MIGRATIONS = [ |
| "migrate_add_pid_source", |
| "migrate_add_community_ace_columns", |
| "migrate_add_author_isni", |
| "migrate_add_item_funders", |
| "migrate_2026_upgrade", |
| "migrate_add_sc_columns", |
| ] |
|
|
|
|
| def run_schema_migrations(): |
| import importlib |
|
|
| print("Applying schema migrations (idempotent β safe to re-run)...") |
| print(" Generic column sync:") |
| sync_schema_columns() |
| for mod_name in _SCHEMA_MIGRATIONS: |
| try: |
| mod = importlib.import_module(mod_name) |
| mod.main() |
| except Exception as e: |
| |
| |
| |
| |
| |
| print(f" [WARN] {mod_name} failed (continuing): {e}") |
| print() |
|
|
|
|
| def seed_communities_and_collections(): |
| """Seed the database with UNILAG faculty and department structure.""" |
| session = SessionLocal() |
|
|
| try: |
| print("Seeding Communities (Faculties) and Collections (Departments)...") |
|
|
| for faculty_name, departments in UNILAG_STRUCTURE.items(): |
| |
| community = session.query(Community).filter_by(name=faculty_name).first() |
| if not community: |
| community = Community(name=faculty_name) |
| session.add(community) |
| session.flush() |
| print(f" Created Community: {faculty_name}") |
|
|
| |
| for dept_name, keywords in departments.items(): |
| collection = session.query(Collection).filter_by(name=dept_name).first() |
| if not collection: |
| collection = Collection( |
| community_id=community.id, |
| name=dept_name, |
| keywords=", ".join(keywords), |
| ) |
| session.add(collection) |
| print(f" Created Collection: {dept_name}") |
|
|
| session.commit() |
| print("\n[OK] Database seeding completed successfully!") |
| print(f" Total Communities: {session.query(Community).count()}") |
| print(f" Total Collections: {session.query(Collection).count()}") |
|
|
| except Exception as e: |
| print(f"\n[ERR] Error seeding database: {e}") |
| session.rollback() |
| raise |
| finally: |
| session.close() |
|
|
|
|
| def main(): |
| print("=" * 60) |
| print("URAAS Database Initialization") |
| print("=" * 60) |
| print() |
|
|
| |
| print("Creating database tables...") |
| init_db() |
| print("[OK] Tables created successfully!") |
| print() |
|
|
| |
| |
| run_schema_migrations() |
|
|
| |
| seed_communities_and_collections() |
| print() |
| print("=" * 60) |
| print("Database is ready for use!") |
| print("=" * 60) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|