diff --git a/README.md b/README.md index 41c0b3e..2ed2b00 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -Flask example project \ No newline at end of file +Flask example project + + +## run flask migration +1. `flask db init` - generate migration folder +2. `flask db stamp head` - update database +3. `flask db migrate -m 'comment'` - autogenerate migration script +4. `flask db upgrade` - apply migration \ No newline at end of file diff --git a/app.py b/app.py index 11e2013..ddecd90 100644 --- a/app.py +++ b/app.py @@ -20,7 +20,14 @@ def home() -> str: @app.route('/animals', methods=['GET']) def index() -> Response: animals = Animal.query.all() - return jsonify({"animals": [AnimalResponse.model_validate(animal).model_dump(mode='json') for animal in animals]}) + return jsonify({"animals": [ + AnimalResponse.model_validate(animal).model_dump(mode='json') for + animal in animals]}) + + +@app.route('/health') +def health(): + return Response(status=200) @app.route('/animal', methods=['POST']) @@ -29,7 +36,9 @@ def add_animal() -> tuple[Response, int]: new_animal = Animal( animal_type=data.animal_type, name=data.name, - birth_date=data.birth_date + birth_date=data.birth_date, + breed=data.breed, + photo_url=data.photo_url, ) db.session.add(new_animal) db.session.commit() @@ -50,6 +59,8 @@ def update_animal(pk: int) -> Union[Response, tuple[Response, int]]: animal.animal_type = data.animal_type animal.name = data.name + animal.breed = data.breed + animal.photo_url = data.photo_url animal.birth_date = data.birth_date db.session.commit() return jsonify( diff --git a/migrations/versions/f67eaa69e91f_add_columns.py b/migrations/versions/f67eaa69e91f_add_columns.py new file mode 100644 index 0000000..44fe4d0 --- /dev/null +++ b/migrations/versions/f67eaa69e91f_add_columns.py @@ -0,0 +1,34 @@ +"""add columns + +Revision ID: f67eaa69e91f +Revises: e08fc0218f8b +Create Date: 2024-04-14 21:07:50.074349 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f67eaa69e91f' +down_revision = 'e08fc0218f8b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('animal', schema=None) as batch_op: + batch_op.add_column(sa.Column('breed', sa.String(), nullable=False)) + batch_op.add_column(sa.Column('photo_url', sa.String(), nullable=False)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('animal', schema=None) as batch_op: + batch_op.drop_column('photo_url') + batch_op.drop_column('breed') + + # ### end Alembic commands ### diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..28f560c 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -1,11 +1,14 @@ +from datetime import date, timedelta +from pydantic import BaseModel, ConfigDict, computed_field from datetime import date -from pydantic import BaseModel, ConfigDict class AnimalCreate(BaseModel): animal_type: str name: str birth_date: date + breed: str + photo_url: str class AnimalResponse(BaseModel): @@ -15,3 +18,13 @@ class AnimalResponse(BaseModel): animal_type: str name: str birth_date: date + breed: str + photo_url: str + + + @computed_field + @property + def age(self) -> int: + return date.today().year - self.birth_date.year + + diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..cfa983f 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -9,3 +9,6 @@ class Animal(db.Model): animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + breed = db.Column(db.String, nullable=False) + photo_url = db.Column(db.String, nullable=False) + diff --git a/templates/home.html b/templates/home.html index 80b942e..e5278a7 100644 --- a/templates/home.html +++ b/templates/home.html @@ -33,6 +33,15 @@