Skip to content

Commit

Permalink
Add Alembic and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
smallwat3r committed Nov 3, 2024
1 parent c825b13 commit d054fbb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ yarn: ## Install frontend deps using Yarn
shell: ## Pop up a Flask shell in Shhh
docker exec -it shhh-app-1 flask shell

.PHONY: db
db: ## Run flask db command, ex: `make db c='--help'`
docker exec -it shhh-app-1 flask db $(c)

.PHONY: logs
logs: ## Follow Flask logs
docker logs shhh-app-1 -f -n 10
Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,16 @@ make dc-start-adminer-mysql # to start the app with adminer (SQL editor)
make dc-stop-mysql # to stop the app
```

#### Initiate the database tables
#### Migrations

Enter a Flask shell in running container with:
Run the migrations using:
``` sh
make shell
make db c='upgrade'
```

If deployed on Heroku, you can access the Flask shell with:
If deployed on Heroku, you can run the migrations using:
``` sh
heroku run --app=<heroku-app-name> python3 -m flask shell
```

From the Flask shell, copy and run:
``` python
from shhh.adapters import orm
from shhh.extensions import db
orm.metadata.create_all(db.engine)
exit()
heroku run --app=<heroku-app-name> python3 -m flask db upgrade
```

This will ensure the necessary tables are create in the database, and make sure your
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
alembic==1.13.3
APScheduler==3.10.4
blinker==1.8.2
certifi==2024.8.30
Expand All @@ -7,6 +8,7 @@ click==8.1.7
cryptography==43.0.3
cssmin==0.2.0
Flask==3.0.3
Flask-Alembic==3.1.1
Flask-APScheduler==1.13.1
Flask-Assets==2.1.0
Flask-SQLAlchemy==3.1.1
Expand All @@ -16,6 +18,7 @@ idna==3.10
itsdangerous==2.2.0
Jinja2==3.1.4
jsmin==3.0.1
Mako==1.3.6
MarkupSafe==3.0.2
marshmallow==3.23.1
packaging==24.1
Expand Down
5 changes: 4 additions & 1 deletion shhh/adapters/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
db.Column("date_created", db.DateTime),
db.Column("date_expires", db.DateTime),
db.Column("external_id", db.String(20), nullable=False),
db.Column("tries", db.Integer, default=DEFAULT_READ_TRIES_VALUE))
db.Column("tries", db.Integer, default=DEFAULT_READ_TRIES_VALUE),
db.Index("external_id_idx", "external_id"),
db.Index("date_expires_idx", "date_expires"),
)


def start_mappers() -> None:
Expand Down
3 changes: 3 additions & 0 deletions shhh/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import TYPE_CHECKING

from flask import Flask, Response, render_template as rt
from flask_alembic import Alembic
from flask_assets import Bundle
from htmlmin.main import minify

Expand Down Expand Up @@ -84,6 +85,8 @@ def _register_blueprints(app: Flask) -> None:


def _register_extensions(app: Flask) -> None:
alembic = Alembic(metadatas={"default": orm.metadata})
alembic.init_app(app)
assets.init_app(app)
db.init_app(app)
scheduler.init_app(app)
Expand Down
34 changes: 34 additions & 0 deletions shhh/migrations/1730636773_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""initial
Revision ID: 1730636773
Revises:
Create Date: 2024-11-03 12:26:13.593575
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '1730636773'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = ('default',)
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_table('secret',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('encrypted_text', postgresql.BYTEA(), autoincrement=False, nullable=True),
sa.Column('date_created', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('date_expires', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('external_id', sa.VARCHAR(length=20), autoincrement=False, nullable=False),
sa.Column('tries', sa.INTEGER(), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('id', name='secret_pkey')
)


def downgrade() -> None:
op.drop_table('secret')
27 changes: 27 additions & 0 deletions shhh/migrations/1730637997_add_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""add indexes
Revision ID: 1730637997
Revises: 1730636773
Create Date: 2024-11-03 12:46:37.418955
"""
from typing import Sequence, Union

from alembic import op


# revision identifiers, used by Alembic.
revision: str = '1730637997'
down_revision: Union[str, None] = '1730636773'
branch_labels: Union[str, Sequence[str], None] = ()
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_index('date_expires_idx', 'secret', ['date_expires'], unique=False)
op.create_index('external_id_idx', 'secret', ['external_id'], unique=False)


def downgrade() -> None:
op.drop_index('external_id_idx', table_name='secret')
op.drop_index('date_expires_idx', table_name='secret')
26 changes: 26 additions & 0 deletions shhh/migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""${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, 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:
${upgrades if upgrades else "pass"}


def downgrade() -> None:
${downgrades if downgrades else "pass"}

0 comments on commit d054fbb

Please sign in to comment.