diff --git a/.gitignore b/.gitignore index fc4d32e..c026f95 100644 --- a/.gitignore +++ b/.gitignore @@ -144,4 +144,4 @@ cython_debug/ # End of https://www.toptal.com/developers/gitignore/api/python -dev.db +*.db diff --git a/api/__init__.py b/api/__init__.py index 93226a1..e52c7ff 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -54,6 +54,7 @@ def register_shellcontext(app: Flask): """Shell context objects.""" return { 'db': db, + 'TargetExchange': target_exchange.models.TargetExchange } app.shell_context_processor(shell_context) @@ -63,3 +64,4 @@ def register_commands(app: Flask): """Register Click commands.""" app.cli.add_command(commands.clean) app.cli.add_command(commands.urls) + app.cli.add_command(commands.seed) diff --git a/api/commands.py b/api/commands.py index 4c670b5..15a9797 100644 --- a/api/commands.py +++ b/api/commands.py @@ -5,6 +5,8 @@ from flask import current_app from flask.cli import with_appcontext from werkzeug.exceptions import MethodNotAllowed, NotFound +from .target_exchange.models import TargetExchange + @click.command() def clean(): """Remove *.pyc and *.pyo files recursively starting at current directory. @@ -79,3 +81,11 @@ def urls(url, order): for row in rows: click.echo(str_template.format(*row[:column_length])) + +@click.command() +@with_appcontext +def seed(): + """Seed the database""" + + TargetExchange.ensure_created("") + TargetExchange.ensure_created("webhooks") diff --git a/api/target_exchange/__init__.py b/api/target_exchange/__init__.py index 72b68ef..5715038 100644 --- a/api/target_exchange/__init__.py +++ b/api/target_exchange/__init__.py @@ -1,3 +1,3 @@ """exchange management""" -from . import views +from . import views, models diff --git a/api/target_exchange/models.py b/api/target_exchange/models.py index 14e58ea..2bf60c1 100644 --- a/api/target_exchange/models.py +++ b/api/target_exchange/models.py @@ -6,5 +6,7 @@ class TargetExchange(SurrogatePK, Model): __tablename__ = "target-exchange" name = Column(db.String(255), unique=True, nullable=False) - def __init__(self, name, **kwargs) -> None: - Model.__init__(self, name=name, **kwargs) + @staticmethod + def ensure_created(name): + if TargetExchange.query.filter_by(name=name).first() is None: + TargetExchange(name=name).save() diff --git a/migrations/versions/14ceaaaa6e85_initial_create.py b/migrations/versions/14ceaaaa6e85_initial_create.py index b1f06d0..4be1eb2 100644 --- a/migrations/versions/14ceaaaa6e85_initial_create.py +++ b/migrations/versions/14ceaaaa6e85_initial_create.py @@ -1,7 +1,7 @@ """initial create Revision ID: 14ceaaaa6e85 -Revises: +Revises: Create Date: 2021-09-19 23:52:49.797793 """