Skip to content

Commit

Permalink
reformated available migration output
Browse files Browse the repository at this point in the history
  • Loading branch information
sarzz2 committed Nov 26, 2023
1 parent a467cdd commit a0119eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
4 changes: 1 addition & 3 deletions bot/models/migrations/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from datetime import datetime
from typing import Literal, Optional

from pydantic import Field

from bot.models import Model


Expand All @@ -12,7 +10,7 @@ class Migration(Model):
version: int
direction: Literal["up", "down"]
name: str
timestamp: datetime = Field(default_factory=datetime.utcnow)
timestamp: datetime | None # it will be None if the migration hasn't been applied yet

@property
def filename(self):
Expand Down
27 changes: 8 additions & 19 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,11 @@ async def get_current_db_rev() -> Optional[Migration]:

async def get_available_migrations():
"""Gets info about migrations which have not been executed"""
# Fetch a set of applied migrations from the database
async with Model.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetch("SELECT version, direction FROM migrations")
applied_migrations = {(record["version"], record["direction"]) for record in result}

all_migrations = Revisions.revisions()
# Filter out the applied migrations
available_migrations = [
migration
for version_direction, migration in all_migrations.items()
if version_direction not in applied_migrations and migration.direction == "up"
]
curr_migration = await get_current_db_rev()
available_migrations = []
for values in Revisions.revisions().values():
if values.version > curr_migration.version and values.direction == "up":
available_migrations.append(values)

return available_migrations

Expand Down Expand Up @@ -219,12 +211,9 @@ async def migrate(ctx):
available_migrations = await get_available_migrations()

if available_migrations:
log.info("Available Migrations:")
for migration in available_migrations:
log.info(f"Name : {migration.name}")
log.info(f"Version : {migration.version}")
log.info(f"Direction : {migration.direction}")
log.info(f"Timestamp : {migration.timestamp}")
log.info(f"There are {len(available_migrations)} available Migrations!")
for migration in reversed(available_migrations):
log.info(f"{str(migration.version).zfill(3)}: {migration.name}")


async def update(n: int, is_target: bool = False):
Expand Down

0 comments on commit a0119eb

Please sign in to comment.