feat:New database migration, new adaptation root path configuration
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
from tortoise import connections
|
||||
|
||||
|
||||
async def create_file_codes_table():
|
||||
conn = connections.get("default")
|
||||
await conn.execute_script(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS filecodes
|
||||
(
|
||||
id INTEGER not null
|
||||
primary key autoincrement,
|
||||
code VARCHAR(255) not null
|
||||
unique,
|
||||
prefix VARCHAR(255) default '' not null,
|
||||
suffix VARCHAR(255) default '' not null,
|
||||
uuid_file_name VARCHAR(255),
|
||||
file_path VARCHAR(255),
|
||||
size INT default 0 not null,
|
||||
text TEXT,
|
||||
expired_at TIMESTAMP,
|
||||
expired_count INT default 0 not null,
|
||||
used_count INT default 0 not null,
|
||||
created_at TIMESTAMP default CURRENT_TIMESTAMP not null
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_filecodes_code_1c7ee7
|
||||
on filecodes (code);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def create_key_value_table():
|
||||
conn = connections.get("default")
|
||||
await conn.execute_script(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS keyvalue
|
||||
(
|
||||
id INTEGER not null
|
||||
primary key autoincrement,
|
||||
key VARCHAR(255) not null
|
||||
unique,
|
||||
value JSON,
|
||||
created_at TIMESTAMP default CURRENT_TIMESTAMP not null
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_keyvalue_key_eab890
|
||||
on keyvalue (key);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def migrate():
|
||||
await create_file_codes_table()
|
||||
await create_key_value_table()
|
||||
@@ -40,6 +40,44 @@ class FileCodes(Model):
|
||||
auto_now_add=True, description="创建时间"
|
||||
)
|
||||
|
||||
file_hash = fields.CharField(
|
||||
max_length=128, # SHA-256需要64字符,这里预留扩展空间
|
||||
description="文件哈希值",
|
||||
unique=True,
|
||||
null=True # 允许旧数据为空
|
||||
)
|
||||
hash_algorithm = fields.CharField(
|
||||
max_length=20,
|
||||
description="哈希算法类型",
|
||||
null=True,
|
||||
default="sha256"
|
||||
)
|
||||
|
||||
class FileCodes(Model):
|
||||
# 新增分片字段
|
||||
chunk_size = fields.IntField(
|
||||
description="分片大小(字节)",
|
||||
default=0
|
||||
)
|
||||
total_chunks = fields.IntField(
|
||||
description="总分片数",
|
||||
default=0
|
||||
)
|
||||
uploaded_chunks = fields.IntField(
|
||||
description="已上传分片数",
|
||||
default=0
|
||||
)
|
||||
upload_status = fields.CharField(
|
||||
max_length=20,
|
||||
description="上传状态",
|
||||
default="pending", # pending/in_progress/completed
|
||||
choices=["pending", "in_progress", "completed"]
|
||||
)
|
||||
is_chunked = fields.BooleanField(
|
||||
description="是否分片上传",
|
||||
default=False
|
||||
)
|
||||
|
||||
async def is_expired(self):
|
||||
# 按时间
|
||||
if self.expired_at is None:
|
||||
@@ -54,6 +92,31 @@ class FileCodes(Model):
|
||||
return f"{self.file_path}/{self.uuid_file_name}"
|
||||
|
||||
|
||||
class FileChunks(Model):
|
||||
id = fields.IntField(pk=True)
|
||||
file_code = fields.ForeignKeyField(
|
||||
"models.FileCodes",
|
||||
related_name="chunks",
|
||||
on_delete=fields.CASCADE
|
||||
)
|
||||
chunk_number = fields.IntField(description="分片序号")
|
||||
chunk_hash = fields.CharField(
|
||||
max_length=128,
|
||||
description="分片哈希校验值"
|
||||
)
|
||||
chunk_path = fields.CharField(
|
||||
max_length=255,
|
||||
description="分片存储路径"
|
||||
)
|
||||
created_at = fields.DatetimeField(
|
||||
auto_now_add=True,
|
||||
description="上传时间"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = [("file_code", "chunk_number")]
|
||||
|
||||
|
||||
class KeyValue(Model):
|
||||
id: Optional[int] = fields.IntField(pk=True)
|
||||
key: Optional[str] = fields.CharField(
|
||||
|
||||
Reference in New Issue
Block a user