added models and migrations

parent aaa8da24
...@@ -16,6 +16,11 @@ asyncpg = "^0.29.0" ...@@ -16,6 +16,11 @@ asyncpg = "^0.29.0"
aerich = "^0.7.2" aerich = "^0.7.2"
[tool.aerich]
tortoise_orm = "src.app.db.conf.TORTOISE_ORM"
location = "src/migrations"
src_folder = "./."
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"
from uuid import uuid4
from tortoise import fields, models
class IdMixin(models.Model):
id = fields.UUIDField(pk=True, default=uuid4)
class Meta:
abstract = True
class TimestampMixin(models.Model):
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
class Meta:
abstract = True
class BaseModel(IdMixin, TimestampMixin):
class Meta:
ordering = ('-created_at', )
from tortoise import fields
from .base import BaseModel
class User(BaseModel):
username = fields.CharField(max_length=50, unique=True)
name = fields.CharField(max_length=50)
surname = fields.CharField(max_length=50)
email = fields.CharField(max_length=50)
password = fields.CharField(max_length=100)
def __str__(self):
return f'{self.surname} {self.name}'
class Meta(BaseModel.Meta):
table = 'users'
class Folder(BaseModel):
name = fields.CharField(max_length=50)
user = fields.ForeignKeyField("models.User", related_name="user_folders")
def __str__(self):
return self.name
class Meta(BaseModel.Meta):
table = 'folders'
class File(BaseModel):
name = fields.CharField(max_length=50)
content = fields.TextField()
user = fields.ForeignKeyField("models.User", related_name="user_files")
folder = fields.ForeignKeyField("models.Folder", related_name="folder_files", null=True)
def __str__(self):
return self.name
class Meta(BaseModel.Meta):
table = 'files'
from tortoise import BaseDBAsyncClient
async def upgrade(db: BaseDBAsyncClient) -> str:
return """
CREATE TABLE IF NOT EXISTS "basemodel" (
"id" UUID NOT NULL PRIMARY KEY,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS "users" (
"id" UUID NOT NULL PRIMARY KEY,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"username" VARCHAR(50) NOT NULL UNIQUE,
"name" VARCHAR(50) NOT NULL,
"surname" VARCHAR(50) NOT NULL,
"email" VARCHAR(50) NOT NULL,
"password" VARCHAR(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS "folders" (
"id" UUID NOT NULL PRIMARY KEY,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" VARCHAR(50) NOT NULL,
"user_id" UUID NOT NULL REFERENCES "users" ("id") ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS "files" (
"id" UUID NOT NULL PRIMARY KEY,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" VARCHAR(50) NOT NULL,
"content" TEXT NOT NULL,
"folder_id" UUID REFERENCES "folders" ("id") ON DELETE CASCADE,
"user_id" UUID NOT NULL REFERENCES "users" ("id") ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS "aerich" (
"id" SERIAL NOT NULL PRIMARY KEY,
"version" VARCHAR(255) NOT NULL,
"app" VARCHAR(100) NOT NULL,
"content" JSONB NOT NULL
);"""
async def downgrade(db: BaseDBAsyncClient) -> str:
return """
"""
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment