Skip to content

Refactor database initialization and migrations to SQLAlchemy#2

Merged
edgeinfinity1 merged 1 commit into
mainfrom
codex/update-database-connections-to-use-sqlalchemy
Jul 10, 2026
Merged

Refactor database initialization and migrations to SQLAlchemy#2
edgeinfinity1 merged 1 commit into
mainfrom
codex/update-database-connections-to-use-sqlalchemy

Conversation

@edgeinfinity1

Copy link
Copy Markdown
Collaborator

Motivation

  • Replace legacy raw SQL / custom pool helpers with SQLAlchemy async engines and metadata so initial setup and migrations work with the new ORM layer.
  • Normalize MySQL vs SQLite differences using SQLAlchemy execution/inspection instead of ad-hoc string queries.
  • Align ORM model column types and constraints to match the intended schema used by the migrations and initializer.

Description

  • Rewrote maica/initializer/init_db.py to create MySQL databases via a server-level async engine and to create tables using SqlBaseAuth.metadata.create_all and SqlBaseData.metadata.create_all through SQLAlchemy async engines instead of ConnUtils raw queries.
  • Converted migrations migration_1.py..migration_4.py to use DatabaseUtils.engine_data / DatabaseUtils.engine_auth with text() execution and SQLAlchemy inspection, removing ConnUtils query_modify calls and handling MySQL/SQLite syntax differences appropriately.
  • In migration_2.py the mv_meta table is created via the ORM table metadata (SqlMvMeta.__table__.create(checkfirst=True)) and MySQL-only ALTERs are executed with text() when needed.
  • In migration_3.py the presence of suspended_until is detected with SQLAlchemy inspect and the column is added with an ALTER TABLE executed via the SQLAlchemy connection when missing.
  • In migration_4.py added helper async def _create_index_if_missing using SQLAlchemy inspect(...).get_indexes to avoid duplicate index creation, and applied table ALTERs / index creation through the async engine, preserving MySQL vs SQLite differences.
  • Updated ORM definitions in maica/maica_utils/database_models.py to declare explicit types and constraints (String, Text, Boolean, DateTime), add __table_args__ unique constraint for ms_cache.hash, and otherwise match the adjusted initializer/migration expectations.
  • Removed direct use of legacy pool objects from the modified files and replaced them with SQLAlchemy engine/session based operations.

Testing

  • Ran a syntax/bytecode check with python -m compileall maica/initializer/init_db.py maica/initializer/migrations/migration_1.py maica/initializer/migrations/migration_2.py maica/initializer/migrations/migration_3.py maica/initializer/migrations/migration_4.py maica/maica_utils/database_models.py, which completed successfully.

Codex Task

@edgeinfinity1 edgeinfinity1 merged commit 2f885a5 into main Jul 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant