From 815303693e3c633a49b7d4cf0efcff61e21dd4f3 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Tue, 14 Feb 2023 20:41:39 -0500 Subject: [PATCH] sqlite autoincrement support --- .gitignore | 2 ++ alembic/versions/b03fcdc9b301_.py | 2 +- project/users/models.py | 4 ++-- tasks.py | 16 +++++++++++++--- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a3f8f3e..8d88ed1 100644 --- a/.gitignore +++ b/.gitignore @@ -140,3 +140,5 @@ cython_debug/ # sqlite *.sqlite3 + +.idea/ diff --git a/alembic/versions/b03fcdc9b301_.py b/alembic/versions/b03fcdc9b301_.py index d89769b..255db18 100644 --- a/alembic/versions/b03fcdc9b301_.py +++ b/alembic/versions/b03fcdc9b301_.py @@ -20,7 +20,7 @@ def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.create_table( "users", - sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False), + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), sa.Column("username", sa.String(length=128), nullable=False), sa.Column("email", sa.String(length=128), nullable=False), sa.PrimaryKeyConstraint("id"), diff --git a/project/users/models.py b/project/users/models.py index f629fd8..2d4ccb6 100644 --- a/project/users/models.py +++ b/project/users/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import BigInteger, Column, String +from sqlalchemy import Integer, Column, String from project.database import Base @@ -7,7 +7,7 @@ class User(Base): __tablename__ = "users" - id = Column(BigInteger, primary_key=True, autoincrement=True) + id = Column(Integer, primary_key=True, autoincrement=True) username = Column(String(128), unique=True, nullable=False) email = Column(String(128), unique=True, nullable=False) diff --git a/tasks.py b/tasks.py index 36394b9..19dd154 100644 --- a/tasks.py +++ b/tasks.py @@ -16,7 +16,7 @@ def start_dev(c): @task def stop_dev(c): """Stops the developer environment.""" - print("Stoping local development environment.") + print("Stopping local development environment...") c.run("docker-compose -f docker-compose.yml down") @@ -48,6 +48,16 @@ def stop_redis(c): c.run("docker compose -f docker-compose-redis.yml down") +@task +def init_db(c): + """Initialize the database.""" + from project.database import Base, engine + + Base.metadata.bind = engine + Base.metadata.create_all() + print(f"Created: {engine.url}") + + @task def create_migration(c): """Creates a sqlalchemy migration.""" @@ -62,8 +72,8 @@ def apply_migration(c, revision="head"): @task(help={"username": "A username", "email": "The user's email"}) def add_user(c, username="", email=""): - "Adds a dummy user to the application Database" - if not username or email: + """Adds a dummy user to the application Database""" + if not (username and email): raise ValueError("You must provide a username and email") from project.database import SessionLocal from project.users.models import User