diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-09 04:35:31 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-09 04:35:31 +0200 |
| commit | 77d76421829161df6b1ef628b4e6e051a2c3c2ee (patch) | |
| tree | 865a3a4044d16a4320981e3cb1efb7128fd8d096 /packages/meshbay-hub/src/meshbay_hub/db/migrations/versions | |
| parent | d752cd8ebae87bf2e0f5be09307b7fd45f3a60f3 (diff) | |
| download | meshbay-77d76421829161df6b1ef628b4e6e051a2c3c2ee.tar.gz | |
feat(hub): add SQLAlchemy 2.0 async DB layer + Alembic
Models: User, Node, Group, GroupMember, GEKBundle, RefreshToken, IPLog.
Engine configurable via MESHBAY_DATABASE_URL (asyncpg/aiosqlite).
Alembic async env.py + initial_schema migration autogenerated.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/db/migrations/versions')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/d28b9caf9f07_initial_schema.py | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/d28b9caf9f07_initial_schema.py b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/d28b9caf9f07_initial_schema.py new file mode 100644 index 0000000..d4a9aa6 --- /dev/null +++ b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/d28b9caf9f07_initial_schema.py @@ -0,0 +1,127 @@ +"""initial_schema + +Revision ID: d28b9caf9f07 +Revises: +Create Date: 2026-08-09 04:35:07.120021 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'd28b9caf9f07' +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('users', + sa.Column('id', sa.String(length=36), nullable=False), + sa.Column('username', sa.String(length=64), nullable=False), + sa.Column('email', sa.String(length=256), nullable=False), + sa.Column('pw_hash', sa.LargeBinary(), nullable=False), + sa.Column('pw_salt', sa.LargeBinary(), nullable=False), + sa.Column('pk_ed25519', sa.String(length=64), nullable=False), + sa.Column('pk_x25519', sa.String(length=64), nullable=False), + sa.Column('hub_id', sa.String(length=128), nullable=False), + sa.Column('status', sa.String(length=16), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('username') + ) + op.create_index('ix_users_email', 'users', ['email'], unique=False) + op.create_index('ix_users_username', 'users', ['username'], unique=False) + op.create_table('groups', + sa.Column('id', sa.String(length=36), nullable=False), + sa.Column('name', sa.String(length=128), nullable=False), + sa.Column('admin_id', sa.String(length=36), nullable=False), + sa.Column('visibility', sa.String(length=16), nullable=False), + sa.Column('join_policy', sa.String(length=16), nullable=False), + sa.Column('status', sa.String(length=16), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['admin_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_groups_name', 'groups', ['name'], unique=False) + op.create_table('ip_logs', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('user_id', sa.String(length=36), nullable=True), + sa.Column('event', sa.String(length=32), nullable=False), + sa.Column('ip_address', sa.String(length=45), nullable=False), + sa.Column('detail', sa.String(length=256), nullable=True), + sa.Column('timestamp', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_ip_logs_ip', 'ip_logs', ['ip_address'], unique=False) + op.create_index('ix_ip_logs_timestamp', 'ip_logs', ['timestamp'], unique=False) + op.create_index('ix_ip_logs_user_id', 'ip_logs', ['user_id'], unique=False) + op.create_table('nodes', + sa.Column('id', sa.String(length=36), nullable=False), + sa.Column('user_id', sa.String(length=36), nullable=False), + sa.Column('pk_node', sa.String(length=64), nullable=False), + sa.Column('endpoint_hint', sa.String(length=128), nullable=True), + sa.Column('announced_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_nodes_user_id', 'nodes', ['user_id'], unique=False) + op.create_table('refresh_tokens', + sa.Column('id', sa.String(length=36), nullable=False), + sa.Column('user_id', sa.String(length=36), nullable=False), + sa.Column('token_hash', sa.String(length=64), nullable=False), + sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.Column('revoked', sa.Boolean(), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('token_hash') + ) + op.create_index('ix_refresh_tokens_hash', 'refresh_tokens', ['token_hash'], unique=False) + op.create_table('gek_bundles', + sa.Column('group_id', sa.String(length=36), nullable=False), + sa.Column('user_id', sa.String(length=36), nullable=False), + sa.Column('pk_eph_b64', sa.String(length=64), nullable=False), + sa.Column('nonce_b64', sa.String(length=32), nullable=False), + sa.Column('wrapped_b64', sa.String(length=128), nullable=False), + sa.Column('stored_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('group_id', 'user_id') + ) + op.create_table('group_members', + sa.Column('group_id', sa.String(length=36), nullable=False), + sa.Column('user_id', sa.String(length=36), nullable=False), + sa.Column('joined_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('group_id', 'user_id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('group_members') + op.drop_table('gek_bundles') + op.drop_index('ix_refresh_tokens_hash', table_name='refresh_tokens') + op.drop_table('refresh_tokens') + op.drop_index('ix_nodes_user_id', table_name='nodes') + op.drop_table('nodes') + op.drop_index('ix_ip_logs_user_id', table_name='ip_logs') + op.drop_index('ix_ip_logs_timestamp', table_name='ip_logs') + op.drop_index('ix_ip_logs_ip', table_name='ip_logs') + op.drop_table('ip_logs') + op.drop_index('ix_groups_name', table_name='groups') + op.drop_table('groups') + op.drop_index('ix_users_username', table_name='users') + op.drop_index('ix_users_email', table_name='users') + op.drop_table('users') + # ### end Alembic commands ### |