-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaf394e
commit efb0077
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import uvicorn | ||
|
||
if __name__ == '__main__': | ||
uvicorn.run(app="core.deps:app", host="127.0.0.1", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from .migrate import migrate | ||
from tortoise import Tortoise | ||
from core.app import app | ||
from .settings import settings | ||
|
||
|
||
@app.on_event("startup") | ||
async def on_startup(): | ||
await Tortoise.init( | ||
db_url=settings.DB_CONNECTION, | ||
modules={ | ||
|
||
"migrate": ["core.migrate"], | ||
"models": [] | ||
} | ||
) | ||
await migrate() | ||
|
||
|
||
@app.on_event("shutdown") | ||
async def on_shutdown(): | ||
await Tortoise.close_connections() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
from typing import List | ||
from core.logger import logger | ||
from core.settings import settings | ||
from tortoise import fields, models | ||
from tortoise.transactions import in_transaction | ||
|
||
|
||
class Migrate(models.Model): | ||
id = fields.BigIntField(pk=True) | ||
file = fields.CharField(max_length=1024) | ||
|
||
|
||
async def migrate(): | ||
#create migration table | ||
async with in_transaction("default") as conn: | ||
await conn.execute_script( | ||
"create table if not exists \"migrate\"(\"id\" BIGSERIAL PRIMARY KEY, \"file\" varchar(1024));" | ||
) | ||
#collect new migrations | ||
scripts_files: List[str] = os.listdir(settings.DB_MIGRATE_PATH) | ||
if not scripts_files: | ||
return | ||
script_db: Migrate = await Migrate.filter().order_by('-id').first() | ||
if script_db is None: | ||
unregistered_migration_scripts = scripts_files | ||
else: | ||
unregistered_migration_scripts = scripts_files[scripts_files.index(script_db.file) + 1:] | ||
#make migrations | ||
logger.info(msg=f"Found {len(unregistered_migration_scripts)} changes: {unregistered_migration_scripts}") | ||
for migration_script in unregistered_migration_scripts: | ||
async with in_transaction("default") as t_conn: | ||
sql_script = open(file=f"{settings.DB_MIGRATE_PATH}{os.sep}{migration_script}", mode="r") | ||
await t_conn.execute_script(sql_script) | ||
await Migrate.create(file=migration_script) | ||
|
||
|
||
|