Skip to content

Commit

Permalink
create database command (#4)
Browse files Browse the repository at this point in the history
* feat: add create command
  • Loading branch information
lloydzhou authored Dec 29, 2023
1 parent 3b4803e commit 9575bc5
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 10 deletions.
37 changes: 36 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ dependencies = [
"flask-sqlalchemy>=3.1.1",
"flask-cors>=4.0.0",
"pymysql>=1.1.0",
"click>=8.1.7",
"bson>=0.5.10",
]
requires-python = ">=3.10"
readme = "README.md"
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

anyio==4.2.0
blinker==1.7.0
bson==0.5.10
ca-lark-oauth==0.0.5
ca-lark-sdk==0.0.7
ca-lark-webhook==0.0.3
Expand All @@ -23,7 +24,9 @@ Jinja2==3.1.2
MarkupSafe==2.1.3
pycryptodome==3.19.1
pymysql==1.1.0
python-dateutil==2.8.2
python-dotenv==1.0.0
six==1.16.0
sniffio==1.3.0
sqlalchemy==2.0.23
typing-extensions==4.9.0
Expand Down
249 changes: 240 additions & 9 deletions server/model/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from datetime import datetime

import bson
from app import db
from sqlalchemy import BINARY, String, text
import click
from app import app, db
from flask.cli import with_appcontext
from sqlalchemy import BINARY, ForeignKey, String, text


class ObjID(BINARY):
Expand Down Expand Up @@ -71,16 +73,245 @@ def is_valid(value):
return False


class User(db.Model):
__tablename__ = "user"
class Base(db.Model):
__abstract__ = True
id = db.Column(ObjID(12), primary_key=True)
openid = db.Column(db.String(128), nullable=True, comment="外部用户ID")
name = db.Column(db.String(128), nullable=True, comment="用户名")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="用户其他字段"
)
status = db.Column(db.Integer, nullable=True, default=0, server_default=text("0"))
created = db.Column(db.TIMESTAMP, nullable=False, default=datetime.utcnow)
modified = db.Column(
db.TIMESTAMP, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow
)


class User(Base):
__tablename__ = "user"
email = db.Column(db.String(128), nullable=True, comment="邮箱,这里考虑一下如何做唯一的用户")
telephone = db.Column(db.String(128), nullable=True, comment="手机号")
name = db.Column(db.String(128), nullable=True, comment="用户名")
avatar = db.Column(db.String(128), nullable=True, comment="头像")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="用户其他字段"
)


class Account(User):
passwd = db.Column(
db.String(128), nullable=True, server_default=text("''"), comment="登录密码"
)


class BindUser(Base):
__tablename__ = "bint_user"
user_id = db.Column(ObjID(12), ForeignKey("user.id"), nullable=True, comment="用户ID")
# 这里如果是飞书租户,可能会有不同的name等,但是在github这边不管是哪一个org,都是一样的
# 这里如何统一?
# 是不是说这里暂时不需要这个platform_id,还是说这个字段为空就好?
platform_id = db.Column(
ObjID(12), ForeignKey("im_platform.id"), nullable=True, comment="平台"
)
unionid = db.Column(db.String(128), nullable=True, comment="飞书的unionid")

# 这里还是用platform标记一下
platform = db.Column(db.String(128), nullable=True, comment="平台:github/lark")
email = db.Column(db.String(128), nullable=True, comment="邮箱")
name = db.Column(db.String(128), nullable=True, comment="用户名")
avatar = db.Column(db.String(128), nullable=True, comment="头像")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="用户其他字段"
)


class Team(Base):
__tablename__ = "team"
user_id = db.Column(ObjID(12), ForeignKey("user.id"), nullable=True, comment="用户ID")
code_platform_id = db.Column(
ObjID(12), ForeignKey("code_platform.id"), nullable=True, comment="代码平台"
)
im_platform_id = db.Column(
ObjID(12), ForeignKey("im_platform.id"), nullable=True, comment="协同平台"
)

name = db.Column(db.String(128), nullable=True, comment="名称")
description = db.Column(db.String(1024), nullable=True, comment="描述")
extra = db.Column(
JSONStr(1024),
nullable=True,
server_default=text("'{}'"),
comment="其他字段,可能有一些前期没想好的配置项放这里",
)


class TeamMember(Base):
__tablename__ = "team_member"
team_id = db.Column(
ObjID(12), ForeignKey("team.id"), nullable=True, comment="属于哪一个组"
)
code_user_id = db.Column(
ObjID(12),
ForeignKey("bind_user.id"),
nullable=True,
comment="从code_platform关联过来的用户",
)
im_user_id = db.Column(
ObjID(12),
ForeignKey("bind_user.id"),
nullable=True,
comment="从im_platform关联过来的用户",
)


class CodePlatform(Base):
__tablename__ = "code_platform"
name = db.Column(db.String(128), nullable=True, comment="名称")
description = db.Column(db.String(1024), nullable=True, comment="描述")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class Repo(Base):
__tablename__ = "repo"
code_platform_id = db.Column(
ObjID(12), ForeignKey("code_platform.id"), nullable=True, comment="属于哪一个org"
)
application_id = db.Column(
ObjID(12),
ForeignKey("code_application.id"),
nullable=True,
comment="哪一个application_id",
)
name = db.Column(db.String(128), nullable=True, comment="名称")
description = db.Column(db.String(1024), nullable=True, comment="描述")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class RepoUser(Base):
__tablename__ = "repo_user"
code_platform_id = db.Column(
ObjID(12), ForeignKey("code_platform.id"), nullable=True, comment="属于哪一个org"
)
application_id = db.Column(
ObjID(12),
ForeignKey("code_application.id"),
nullable=True,
comment="哪一个application_id",
)
bind_user_id = db.Column(
ObjID(12), ForeignKey("bind_user.id"), nullable=True, comment="项目协作者"
)


class IMPlatform(Base):
__tablename__ = "im_platform"
tenant_key = db.Column(db.String(128), nullable=True, comment="飞书租户id")
name = db.Column(db.String(128), nullable=True, comment="名称")
description = db.Column(db.String(1024), nullable=True, comment="描述")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class CodeApplication(Base):
__tablename__ = "code_application"
platform_id = db.Column(
ObjID(12), ForeignKey("code_platform.id"), nullable=True, comment="代码平台"
)
installation_id = db.Column(db.String(128), nullable=True, comment="安装id")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class CodeEvent(Base):
__tablename__ = "code_event"
application_id = db.Column(
ObjID(12), ForeignKey("code_application.id"), nullable=True, comment="应用id"
)
event_id = db.Column(db.String(128), nullable=True, comment="event_id")
event_type = db.Column(db.String(128), nullable=True, comment="event_type")
content = db.Column(db.String(128), nullable=True, comment="主要内容")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class CodeAction(Base):
__tablename__ = "code_action"
event_id = db.Column(
ObjID(12), ForeignKey("code_event.id"), nullable=True, comment="事件ID"
)
action_type = db.Column(
db.String(128), nullable=True, comment="action_type: 主要是飞书那边的消息等"
)
content = db.Column(db.String(128), nullable=True, comment="主要内容")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class IMApplication(Base):
__tablename__ = "im_application"
platform_id = db.Column(
ObjID(12), ForeignKey("code_platform.id"), nullable=True, comment="协同平台"
)
app_id = db.Column(db.String(128), nullable=True, comment="app_id")
app_secret = db.Column(db.String(128), nullable=True, comment="app_id")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class IMEvent(Base):
__tablename__ = "im_event"
application_id = db.Column(
ObjID(12), ForeignKey("im_application.id"), nullable=True, comment="应用id"
)
event_id = db.Column(db.String(128), nullable=True, comment="event_id")
event_type = db.Column(db.String(128), nullable=True, comment="event_type")
content = db.Column(db.String(128), nullable=True, comment="主要内容")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class IMAction(Base):
__tablename__ = "im_action"
event_id = db.Column(
ObjID(12), ForeignKey("im_event.id"), nullable=True, comment="事件ID"
)
action_type = db.Column(
db.String(128), nullable=True, comment="action_type: 主要是github那边的动作等"
)
content = db.Column(db.String(128), nullable=True, comment="主要内容")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


class ChatGroup(Base):
__tablename__ = "chat_group"
repo_id = db.Column(
ObjID(12), ForeignKey("repo.id"), nullable=True, comment="属于哪一个项目"
)
im_application_id = db.Column(
ObjID(12), ForeignKey("code_application.id"), nullable=True, comment="哪一个项目创建的"
)
chat_id = db.Column(db.String(128), nullable=True, comment="chat_id")
name = db.Column(db.String(128), nullable=True, comment="群名称")
description = db.Column(db.String(128), nullable=True, comment="群描述")
extra = db.Column(
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="其他字段"
)


# create command function
@click.command(name="create")
@with_appcontext
def create():
db.create_all()


# add command function to cli commands
app.cli.add_command(create)

0 comments on commit 9575bc5

Please sign in to comment.