Add support for snowflake ID primary key (#670)

* fix: 修复PostgreSQL SQL语法错误,将反引号替换为双引号

* feat: 新增雪花算法ID实现

* 优化雪花算法和主键类型

* 修复错误引用

* 添加雪花详情链接

* feat: add snowflake ID parser method

* 修复独立执行异常

* 更新系统时间错误类
This commit is contained in:
Dylan
2025-06-16 13:34:27 +08:00
committed by GitHub
parent 845f2f0ff8
commit 7afd8415cd
12 changed files with 626 additions and 325 deletions
+2 -2
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from sqlalchemy import Boolean, ForeignKey, String
from sqlalchemy import BigInteger, Boolean, ForeignKey, String
from sqlalchemy.dialects.postgresql import INTEGER
from sqlalchemy.orm import Mapped, mapped_column, relationship
@@ -32,7 +32,7 @@ class Dept(Base):
# 父级部门一对多
parent_id: Mapped[int | None] = mapped_column(
ForeignKey('sys_dept.id', ondelete='SET NULL'), default=None, index=True, comment='父部门ID'
BigInteger, ForeignKey('sys_dept.id', ondelete='SET NULL'), default=None, index=True, comment='父部门ID'
)
parent: Mapped[Optional['Dept']] = relationship(init=False, back_populates='children', remote_side=[id])
children: Mapped[Optional[list['Dept']]] = relationship(init=False, back_populates='parent')
+13 -13
View File
@@ -1,33 +1,33 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sqlalchemy import INT, Column, ForeignKey, Integer, Table
from sqlalchemy import BigInteger, Column, ForeignKey, Table
from backend.common.model import MappedBase
sys_user_role = Table(
'sys_user_role',
MappedBase.metadata,
Column('id', INT, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
Column('user_id', Integer, ForeignKey('sys_user.id', ondelete='CASCADE'), primary_key=True, comment='用户ID'),
Column('role_id', Integer, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色ID'),
Column('id', BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
Column('user_id', BigInteger, ForeignKey('sys_user.id', ondelete='CASCADE'), primary_key=True, comment='用户ID'),
Column('role_id', BigInteger, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色ID'),
)
sys_role_menu = Table(
'sys_role_menu',
MappedBase.metadata,
Column('id', INT, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
Column('role_id', Integer, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色ID'),
Column('menu_id', Integer, ForeignKey('sys_menu.id', ondelete='CASCADE'), primary_key=True, comment='菜单ID'),
Column('id', BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
Column('role_id', BigInteger, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色ID'),
Column('menu_id', BigInteger, ForeignKey('sys_menu.id', ondelete='CASCADE'), primary_key=True, comment='菜单ID'),
)
sys_role_data_scope = Table(
'sys_role_data_scope',
MappedBase.metadata,
Column('id', INT, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键 ID'),
Column('role_id', Integer, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色 ID'),
Column('id', BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键 ID'),
Column('role_id', BigInteger, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色 ID'),
Column(
'data_scope_id',
Integer,
BigInteger,
ForeignKey('sys_data_scope.id', ondelete='CASCADE'),
primary_key=True,
comment='数据范围 ID',
@@ -37,17 +37,17 @@ sys_role_data_scope = Table(
sys_data_scope_rule = Table(
'sys_data_scope_rule',
MappedBase.metadata,
Column('id', INT, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
Column('id', BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
Column(
'data_scope_id',
Integer,
BigInteger,
ForeignKey('sys_data_scope.id', ondelete='CASCADE'),
primary_key=True,
comment='数据范围 ID',
),
Column(
'data_rule_id',
Integer,
BigInteger,
ForeignKey('sys_data_rule.id', ondelete='CASCADE'),
primary_key=True,
comment='数据规则 ID',
+2 -2
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from sqlalchemy import ForeignKey, String
from sqlalchemy import BigInteger, ForeignKey, String
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.dialects.postgresql import TEXT
from sqlalchemy.orm import Mapped, mapped_column, relationship
@@ -42,7 +42,7 @@ class Menu(Base):
# 父级菜单一对多
parent_id: Mapped[int | None] = mapped_column(
ForeignKey('sys_menu.id', ondelete='SET NULL'), default=None, index=True, comment='父菜单ID'
BigInteger, ForeignKey('sys_menu.id', ondelete='SET NULL'), default=None, index=True, comment='父菜单ID'
)
parent: Mapped[Optional['Menu']] = relationship(init=False, back_populates='children', remote_side=[id])
children: Mapped[Optional[list['Menu']]] = relationship(init=False, back_populates='parent')
+9
View File
@@ -64,3 +64,12 @@ class TokenPayload:
@dataclasses.dataclass
class UploadUrl:
url: str
@dataclasses.dataclass
class SnowflakeInfo:
timestamp: int
datetime: str
cluster_id: int
node_id: int
sequence: int
+28 -2
View File
@@ -3,17 +3,43 @@
from datetime import datetime
from typing import Annotated
from sqlalchemy import DateTime
from sqlalchemy import BigInteger, DateTime
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, declared_attr, mapped_column
from backend.utils.snowflake import snowflake
from backend.utils.timezone import timezone
# 通用 Mapped 类型主键, 需手动添加,参考以下使用方式
# MappedBase -> id: Mapped[id_key]
# DataClassBase && Base -> id: Mapped[id_key] = mapped_column(init=False)
id_key = Annotated[
int, mapped_column(primary_key=True, index=True, autoincrement=True, sort_order=-999, comment='主键 ID')
int,
mapped_column(
BigInteger,
primary_key=True,
unique=True,
index=True,
autoincrement=True,
sort_order=-999,
comment='主键 ID',
),
]
# 雪花算法 Mapped 类型主键,使用方法与 id_key 相同
# 详情:https://fastapi-practices.github.io/fastapi_best_architecture_docs/backend/reference/pk.html
snowflake_id_key = Annotated[
int,
mapped_column(
BigInteger,
primary_key=True,
unique=True,
index=True,
default=snowflake.generate,
sort_order=-999,
comment='雪花算法主键 ID',
),
]
@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
from typing import TYPE_CHECKING, Union
from sqlalchemy import ForeignKey, String
from sqlalchemy import BigInteger, ForeignKey, String
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.dialects.postgresql import TEXT
from sqlalchemy.orm import Mapped, mapped_column, relationship
@@ -33,6 +33,6 @@ class GenColumn(DataClassBase):
# 代码生成业务模型一对多
gen_business_id: Mapped[int] = mapped_column(
ForeignKey('gen_business.id', ondelete='CASCADE'), default=0, comment='代码生成业务ID'
BigInteger, ForeignKey('gen_business.id', ondelete='CASCADE'), default=0, comment='代码生成业务ID'
)
gen_business: Mapped[Union['GenBusiness', None]] = relationship(init=False, back_populates='gen_column')
+4 -2
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from sqlalchemy import ForeignKey, String
from sqlalchemy import BigInteger, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from backend.common.model import Base, id_key
@@ -23,5 +23,7 @@ class UserSocial(Base):
source: Mapped[str] = mapped_column(String(20), comment='第三方用户来源')
# 用户社交信息一对多
user_id: Mapped[int] = mapped_column(ForeignKey('sys_user.id', ondelete='CASCADE'), comment='用户关联ID')
user_id: Mapped[int] = mapped_column(
BigInteger, ForeignKey('sys_user.id', ondelete='CASCADE'), comment='用户关联ID'
)
user: Mapped[User | None] = relationship(init=False, backref='socials')
+1 -1
View File
@@ -22,7 +22,7 @@ from backend.core.conf import settings
from backend.core.path_conf import PLUGIN_DIR
from backend.database.redis import RedisCli, redis_client
from backend.plugin.errors import PluginConfigError, PluginInjectError
from backend.utils._asyncio import run_await
from backend.utils._await import run_await
from backend.utils.import_parse import import_module_cached
+109 -115
View File
@@ -1,6 +1,6 @@
create table gen_business
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
app_name varchar(50) not null comment '应用名称(英文)',
table_name varchar(255) not null comment '表名称(英文)',
@@ -15,17 +15,16 @@ create table gen_business
remark longtext null comment '备注',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_gen_business_id
unique (id),
constraint table_name
unique (table_name)
)
comment '代码生成业务表';
create index ix_gen_business_id
on gen_business (id);
create table gen_column
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(50) not null comment '列名称',
comment varchar(255) null comment '列描述',
@@ -36,7 +35,9 @@ create table gen_column
length int not null comment '列长度',
is_pk tinyint(1) not null comment '是否主键',
is_nullable tinyint(1) not null comment '是否可为空',
gen_business_id int not null comment '代码生成业务ID',
gen_business_id bigint not null comment '代码生成业务ID',
constraint ix_gen_column_id
unique (id),
constraint gen_column_ibfk_1
foreign key (gen_business_id) references gen_business (id)
on delete cascade
@@ -46,12 +47,9 @@ create table gen_column
create index gen_business_id
on gen_column (gen_business_id);
create index ix_gen_column_id
on gen_column (id);
create table sys_config
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(20) not null comment '名称',
type varchar(20) null comment '类型',
@@ -61,30 +59,16 @@ create table sys_config
remark longtext null comment '备注',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_config_id
unique (id),
constraint `key`
unique (`key`)
)
comment '参数配置表';
create index ix_sys_config_id
on sys_config (id);
create table sys_data_scope
(
id int auto_increment comment '主键 ID'
primary key,
name varchar(50) not null comment '名称',
status int not null comment '状态(0停用 1正常)',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint name
unique (name)
)
comment '数据范围表';
create table sys_data_rule
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(500) not null comment '名称',
model varchar(50) not null comment 'SQLA 模型名,对应 DATA_PERMISSION_MODELS 键名',
@@ -92,29 +76,55 @@ create table sys_data_rule
operator int not null comment '运算符(0and、1or',
expression int not null comment '表达式(0==、1!=、2>、3>=、4<、5<=、6in、7not_in',
value varchar(255) not null comment '规则值',
scope_id int null comment '数据范围关联 ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_data_rule_id
unique (id),
constraint name
unique (name),
constraint sys_data_rule_ibfk_1
foreign key (scope_id) references sys_data_scope (id)
on delete set null
unique (name)
)
comment '数据规则表';
create index ix_sys_data_rule_id
on sys_data_rule (id);
create table sys_data_scope
(
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(50) not null comment '名称',
status int not null comment '状态(0停用 1正常)',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_data_scope_id
unique (id),
constraint name
unique (name)
)
comment '数据范围表';
create index scope_id
on sys_data_rule (scope_id);
create table sys_data_scope_rule
(
id bigint auto_increment comment '主键ID',
data_scope_id bigint not null comment '数据范围 ID',
data_rule_id bigint not null comment '数据规则 ID',
primary key (id, data_scope_id, data_rule_id),
constraint ix_sys_data_scope_rule_id
unique (id),
constraint sys_data_scope_rule_ibfk_1
foreign key (data_scope_id) references sys_data_scope (id)
on delete cascade,
constraint sys_data_scope_rule_ibfk_2
foreign key (data_rule_id) references sys_data_rule (id)
on delete cascade
);
create index ix_sys_data_scope_id
on sys_data_scope (id);
create index data_rule_id
on sys_data_scope_rule (data_rule_id);
create index data_scope_id
on sys_data_scope_rule (data_scope_id);
create table sys_dept
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(50) not null comment '部门名称',
sort int not null comment '排序',
@@ -123,24 +133,23 @@ create table sys_dept
email varchar(50) null comment '邮箱',
status int not null comment '部门状态(0停用 1正常)',
del_flag tinyint(1) not null comment '删除标志(0删除 1存在)',
parent_id int null comment '父部门ID',
parent_id bigint null comment '父部门ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_dept_id
unique (id),
constraint sys_dept_ibfk_1
foreign key (parent_id) references sys_dept (id)
on delete set null
)
comment '部门表';
create index ix_sys_dept_id
on sys_dept (id);
create index ix_sys_dept_parent_id
on sys_dept (parent_id);
create table sys_dict_type
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(32) not null comment '字典类型名称',
code varchar(32) not null comment '字典类型编码',
@@ -149,22 +158,26 @@ create table sys_dict_type
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint code
unique (code)
unique (code),
constraint ix_sys_dict_type_id
unique (id)
)
comment '字典类型表';
create table sys_dict_data
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
label varchar(32) not null comment '字典标签',
value varchar(32) not null comment '字典值',
sort int not null comment '排序',
status int not null comment '状态(0停用 1正常)',
remark longtext null comment '备注',
type_id int not null comment '字典类型关联ID',
type_id bigint not null comment '字典类型关联ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_dict_data_id
unique (id),
constraint label
unique (label),
constraint sys_dict_data_ibfk_1
@@ -173,18 +186,12 @@ create table sys_dict_data
)
comment '字典数据表';
create index ix_sys_dict_data_id
on sys_dict_data (id);
create index type_id
on sys_dict_data (type_id);
create index ix_sys_dict_type_id
on sys_dict_type (id);
create table sys_login_log
(
id int auto_increment comment '主键 ID'
id bigint not null comment '雪花算法主键 ID'
primary key,
user_uuid varchar(50) not null comment '用户UUID',
username varchar(20) not null comment '用户名',
@@ -199,23 +206,22 @@ create table sys_login_log
device varchar(50) null comment '设备',
msg longtext not null comment '提示消息',
login_time datetime not null comment '登录时间',
created_time datetime not null comment '创建时间'
created_time datetime not null comment '创建时间',
constraint ix_sys_login_log_id
unique (id)
)
comment '登录日志表';
create index ix_sys_login_log_id
on sys_login_log (id);
create table sys_menu
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
title varchar(50) not null comment '菜单标题',
name varchar(50) not null comment '菜单名称',
path varchar(200) not null comment '路由地址',
path varchar(200) null comment '路由地址',
sort int not null comment '排序',
icon varchar(100) null comment '菜单图标',
type int not null comment '菜单类型(0目录 1菜单 2按钮)',
type int not null comment '菜单类型(0目录 1菜单 2按钮 3内嵌 4外链',
component varchar(255) null comment '组件路径',
perms varchar(100) null comment '权限标识',
status int not null comment '菜单状态(0停用 1正常)',
@@ -223,24 +229,23 @@ create table sys_menu
cache int not null comment '是否缓存(0否 1是)',
link longtext null comment '外链地址',
remark longtext null comment '备注',
parent_id int null comment '父菜单ID',
parent_id bigint null comment '父菜单ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_menu_id
unique (id),
constraint sys_menu_ibfk_1
foreign key (parent_id) references sys_menu (id)
on delete set null
)
comment '菜单表';
create index ix_sys_menu_id
on sys_menu (id);
create index ix_sys_menu_parent_id
on sys_menu (parent_id);
create table sys_notice
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
title varchar(50) not null comment '标题',
type int not null comment '类型(0:通知、1:公告)',
@@ -249,16 +254,15 @@ create table sys_notice
status int not null comment '状态(0:隐藏、1:显示)',
content longtext not null comment '内容',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间'
updated_time datetime null comment '更新时间',
constraint ix_sys_notice_id
unique (id)
)
comment '系统通知公告表';
create index ix_sys_notice_id
on sys_notice (id);
create table sys_opera_log
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
trace_id varchar(32) not null comment '请求跟踪 ID',
username varchar(20) null comment '用户名',
@@ -279,16 +283,15 @@ create table sys_opera_log
msg longtext null comment '提示消息',
cost_time float not null comment '请求耗时(ms',
opera_time datetime not null comment '操作时间',
created_time datetime not null comment '创建时间'
created_time datetime not null comment '创建时间',
constraint ix_sys_opera_log_id
unique (id)
)
comment '操作日志表';
create index ix_sys_opera_log_id
on sys_opera_log (id);
create table sys_role
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
name varchar(20) not null comment '角色名称',
status int not null comment '角色状态(0停用 1正常)',
@@ -296,19 +299,18 @@ create table sys_role
remark longtext null comment '备注',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_role_id
unique (id),
constraint name
unique (name)
)
comment '角色表';
create index ix_sys_role_id
on sys_role (id);
create table sys_role_data_scope
(
id int auto_increment comment '主键 ID',
role_id int not null comment '角色 ID',
data_scope_id int not null comment '数据范围 ID',
id bigint auto_increment comment '主键 ID',
role_id bigint not null comment '角色 ID',
data_scope_id bigint not null comment '数据范围 ID',
primary key (id, role_id, data_scope_id),
constraint ix_sys_role_data_scope_id
unique (id),
@@ -328,9 +330,9 @@ create index role_id
create table sys_role_menu
(
id int auto_increment comment '主键ID',
role_id int not null comment '角色ID',
menu_id int not null comment '菜单ID',
id bigint auto_increment comment '主键ID',
role_id bigint not null comment '角色ID',
menu_id bigint not null comment '菜单ID',
primary key (id, role_id, menu_id),
constraint ix_sys_role_menu_id
unique (id),
@@ -350,31 +352,31 @@ create index role_id
create table sys_user
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
uuid varchar(50) not null,
username varchar(20) not null comment '用户名',
nickname varchar(20) not null comment '昵称',
password varchar(255) null comment '密码',
salt varbinary(255) null comment '加密盐',
email varchar(50) not null comment '邮箱',
password varchar(255) not null comment '密码',
salt varbinary(255) not null comment '加密盐',
email varchar(50) null comment '邮箱',
phone varchar(11) null comment '手机号',
avatar varchar(255) null comment '头像',
status int not null comment '用户账号状态(0停用 1正常)',
is_superuser tinyint(1) not null comment '超级权限(0否 1是)',
is_staff tinyint(1) not null comment '后台管理登陆(0否 1是)',
status int not null comment '用户账号状态(0停用 1正常)',
is_multi_login tinyint(1) not null comment '是否重复登陆(0否 1是)',
avatar varchar(255) null comment '头像',
phone varchar(11) null comment '手机号',
join_time datetime not null comment '注册时间',
last_login_time datetime null comment '上次登录',
dept_id int null comment '部门关联ID',
dept_id bigint null comment '部门关联ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_user_email
unique (email),
constraint ix_sys_user_id
unique (id),
constraint ix_sys_user_username
unique (username),
constraint nickname
unique (nickname),
constraint uuid
unique (uuid),
constraint sys_user_ibfk_1
@@ -386,17 +388,14 @@ create table sys_user
create index dept_id
on sys_user (dept_id);
create index ix_sys_user_id
on sys_user (id);
create index ix_sys_user_status
on sys_user (status);
create table sys_user_role
(
id int auto_increment comment '主键ID',
user_id int not null comment '用户ID',
role_id int not null comment '角色ID',
id bigint auto_increment comment '主键ID',
user_id bigint not null comment '用户ID',
role_id bigint not null comment '角色ID',
primary key (id, user_id, role_id),
constraint ix_sys_user_role_id
unique (id),
@@ -416,26 +415,21 @@ create index user_id
create table sys_user_social
(
id int auto_increment comment '主键 ID'
id bigint auto_increment comment '主键 ID'
primary key,
source varchar(20) not null comment '第三方用户来源',
open_id varchar(20) null comment '第三方用户 open id',
sid varchar(20) null comment '第三方用户 ID',
union_id varchar(20) null comment '第三方用户 union id',
scope varchar(120) null comment '第三方用户授予的权限',
code varchar(50) null comment '用户的授权 code',
user_id int null comment '用户关联ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
sid varchar(20) not null comment '第三方用户 ID',
source varchar(20) not null comment '第三方用户来源',
user_id bigint not null comment '用户关联ID',
created_time datetime not null comment '创建时间',
updated_time datetime null comment '更新时间',
constraint ix_sys_user_social_id
unique (id),
constraint sys_user_social_ibfk_1
foreign key (user_id) references sys_user (id)
on delete set null
on delete cascade
)
comment '用户社交表(OAuth2';
create index ix_sys_user_social_id
on sys_user_social (id);
create index user_id
on sys_user_social (user_id);
+327 -186
View File
@@ -1,12 +1,59 @@
create table sys_data_rule
(
id bigserial,
name varchar(500) not null,
model varchar(50) not null,
"column" varchar(20) not null,
operator integer not null,
expression integer not null,
value varchar(255) not null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_data_rule is '数据规则表';
comment on column sys_data_rule.id is '主键 ID';
comment on column sys_data_rule.name is '名称';
comment on column sys_data_rule.model is 'SQLA 模型名,对应 DATA_PERMISSION_MODELS 键名';
comment on column sys_data_rule."column" is '模型字段名';
comment on column sys_data_rule.operator is '运算符(0and、1or';
comment on column sys_data_rule.expression is '表达式(0==、1!=、2>、3>=、4<、5<=、6in、7not_in';
comment on column sys_data_rule.value is '规则值';
comment on column sys_data_rule.created_time is '创建时间';
comment on column sys_data_rule.updated_time is '更新时间';
alter table sys_data_rule
owner to postgres;
create unique index sys_data_rule_pkey
on sys_data_rule (id);
create unique index sys_data_rule_name_key
on sys_data_rule (name);
create unique index ix_sys_data_rule_id
on sys_data_rule (id);
create table sys_data_scope
(
id serial
primary key,
name varchar(50) not null
unique,
id bigserial,
name varchar(50) not null,
status integer not null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_data_scope is '数据范围表';
@@ -21,13 +68,21 @@ comment on column sys_data_scope.created_time is '创建时间';
comment on column sys_data_scope.updated_time is '更新时间';
create index ix_sys_data_scope_id
alter table sys_data_scope
owner to postgres;
create unique index sys_data_scope_pkey
on sys_data_scope (id);
create unique index sys_data_scope_name_key
on sys_data_scope (name);
create unique index ix_sys_data_scope_id
on sys_data_scope (id);
create table sys_dept
(
id serial
primary key,
id bigserial,
name varchar(50) not null,
sort integer not null,
leader varchar(20),
@@ -35,11 +90,12 @@ create table sys_dept
email varchar(50),
status integer not null,
del_flag integer not null,
parent_id integer
references sys_dept
on delete set null,
parent_id bigint
references ??? ()
on delete set null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key ()
);
comment on table sys_dept is '部门表';
@@ -66,7 +122,13 @@ comment on column sys_dept.created_time is '创建时间';
comment on column sys_dept.updated_time is '更新时间';
create index ix_sys_dept_id
alter table sys_dept
owner to postgres;
create unique index sys_dept_pkey
on sys_dept (id);
create unique index ix_sys_dept_id
on sys_dept (id);
create index ix_sys_dept_parent_id
@@ -74,8 +136,7 @@ create index ix_sys_dept_parent_id
create table sys_login_log
(
id serial
primary key,
id bigint not null,
user_uuid varchar(50) not null,
username varchar(20) not null,
status integer not null,
@@ -89,12 +150,13 @@ create table sys_login_log
device varchar(50),
msg text not null,
login_time timestamp with time zone not null,
created_time timestamp with time zone not null
created_time timestamp with time zone not null,
primary key ()
);
comment on table sys_login_log is '登录日志表';
comment on column sys_login_log.id is '主键 ID';
comment on column sys_login_log.id is '雪花算法主键 ID';
comment on column sys_login_log.user_uuid is '用户UUID';
@@ -124,16 +186,21 @@ comment on column sys_login_log.login_time is '登录时间';
comment on column sys_login_log.created_time is '创建时间';
create index ix_sys_login_log_id
alter table sys_login_log
owner to postgres;
create unique index sys_login_log_pkey
on sys_login_log (id);
create unique index ix_sys_login_log_id
on sys_login_log (id);
create table sys_menu
(
id serial
primary key,
id bigserial,
title varchar(50) not null,
name varchar(50) not null,
path varchar(200) not null,
path varchar(200),
sort integer not null,
icon varchar(100),
type integer not null,
@@ -144,11 +211,12 @@ create table sys_menu
cache integer not null,
link text,
remark text,
parent_id integer
references sys_menu
on delete set null,
parent_id bigint
references ??? ()
on delete set null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key ()
);
comment on table sys_menu is '菜单表';
@@ -165,7 +233,7 @@ comment on column sys_menu.sort is '排序';
comment on column sys_menu.icon is '菜单图标';
comment on column sys_menu.type is '菜单类型(0目录 1菜单 2按钮)';
comment on column sys_menu.type is '菜单类型(0目录 1菜单 2按钮 3内嵌 4外链';
comment on column sys_menu.component is '组件路径';
@@ -187,16 +255,21 @@ comment on column sys_menu.created_time is '创建时间';
comment on column sys_menu.updated_time is '更新时间';
create index ix_sys_menu_id
alter table sys_menu
owner to postgres;
create unique index sys_menu_pkey
on sys_menu (id);
create index ix_sys_menu_parent_id
on sys_menu (parent_id);
create unique index ix_sys_menu_id
on sys_menu (id);
create table sys_opera_log
(
id serial
primary key,
id bigserial,
trace_id varchar(32) not null,
username varchar(20),
method varchar(20) not null,
@@ -216,7 +289,8 @@ create table sys_opera_log
msg text,
cost_time double precision not null,
opera_time timestamp with time zone not null,
created_time timestamp with time zone not null
created_time timestamp with time zone not null,
primary key ()
);
comment on table sys_opera_log is '操作日志表';
@@ -263,20 +337,26 @@ comment on column sys_opera_log.opera_time is '操作时间';
comment on column sys_opera_log.created_time is '创建时间';
create index ix_sys_opera_log_id
alter table sys_opera_log
owner to postgres;
create unique index sys_opera_log_pkey
on sys_opera_log (id);
create unique index ix_sys_opera_log_id
on sys_opera_log (id);
create table sys_role
(
id serial
primary key,
name varchar(20) not null
unique,
id bigserial,
name varchar(20) not null,
status integer not null,
is_filter_scopes integer not null,
remark text,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_role is '角色表';
@@ -295,22 +375,31 @@ comment on column sys_role.created_time is '创建时间';
comment on column sys_role.updated_time is '更新时间';
create index ix_sys_role_id
alter table sys_role
owner to postgres;
create unique index sys_role_pkey
on sys_role (id);
create unique index sys_role_name_key
on sys_role (name);
create unique index ix_sys_role_id
on sys_role (id);
create table sys_config
(
id serial
primary key,
id bigserial,
name varchar(20) not null,
type varchar(20),
key varchar(50) not null
unique,
key varchar(50) not null,
value text not null,
is_frontend integer not null,
remark text,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_config is '参数配置表';
@@ -333,20 +422,29 @@ comment on column sys_config.created_time is '创建时间';
comment on column sys_config.updated_time is '更新时间';
create index ix_sys_config_id
alter table sys_config
owner to postgres;
create unique index sys_config_pkey
on sys_config (id);
create unique index sys_config_key_key
on sys_config (key);
create unique index ix_sys_config_id
on sys_config (id);
create table sys_dict_type
(
id serial
primary key,
id bigserial,
name varchar(32) not null,
code varchar(32) not null
unique,
code varchar(32) not null,
status integer not null,
remark text,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_dict_type is '字典类型表';
@@ -365,13 +463,21 @@ comment on column sys_dict_type.created_time is '创建时间';
comment on column sys_dict_type.updated_time is '更新时间';
create index ix_sys_dict_type_id
alter table sys_dict_type
owner to postgres;
create unique index sys_dict_type_pkey
on sys_dict_type (id);
create unique index sys_dict_type_code_key
on sys_dict_type (code);
create unique index ix_sys_dict_type_id
on sys_dict_type (id);
create table sys_notice
(
id serial
primary key,
id bigserial,
title varchar(50) not null,
type integer not null,
author varchar(16) not null,
@@ -379,7 +485,8 @@ create table sys_notice
status integer not null,
content text not null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key ()
);
comment on table sys_notice is '系统通知公告表';
@@ -402,16 +509,20 @@ comment on column sys_notice.created_time is '创建时间';
comment on column sys_notice.updated_time is '更新时间';
create index ix_sys_notice_id
alter table sys_notice
owner to postgres;
create unique index sys_notice_pkey
on sys_notice (id);
create unique index ix_sys_notice_id
on sys_notice (id);
create table gen_business
(
id serial
primary key,
id bigserial,
app_name varchar(50) not null,
table_name varchar(255) not null
unique,
table_name varchar(255) not null,
doc_comment varchar(255) not null,
table_comment varchar(255),
class_name varchar(50),
@@ -422,7 +533,9 @@ create table gen_business
gen_path varchar(255),
remark text,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table gen_business is '代码生成业务表';
@@ -455,62 +568,28 @@ comment on column gen_business.created_time is '创建时间';
comment on column gen_business.updated_time is '更新时间';
create index ix_gen_business_id
alter table gen_business
owner to postgres;
create unique index gen_business_pkey
on gen_business (id);
create table sys_data_rule
(
id serial
primary key,
name varchar(500) not null
unique,
model varchar(50) not null,
"column" varchar(20) not null,
operator integer not null,
expression integer not null,
value varchar(255) not null,
scope_id integer
references sys_data_scope
on delete set null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
);
create unique index gen_business_table_name_key
on gen_business (table_name);
comment on table sys_data_rule is '数据规则表';
comment on column sys_data_rule.id is '主键 ID';
comment on column sys_data_rule.name is '名称';
comment on column sys_data_rule.model is 'SQLA 模型名,对应 DATA_PERMISSION_MODELS 键名';
comment on column sys_data_rule."column" is '模型字段名';
comment on column sys_data_rule.operator is '运算符(0and、1or';
comment on column sys_data_rule.expression is '表达式(0==、1!=、2>、3>=、4<、5<=、6in、7not_in';
comment on column sys_data_rule.value is '规则值';
comment on column sys_data_rule.scope_id is '数据范围关联 ID';
comment on column sys_data_rule.created_time is '创建时间';
comment on column sys_data_rule.updated_time is '更新时间';
create index ix_sys_data_rule_id
on sys_data_rule (id);
create unique index ix_gen_business_id
on gen_business (id);
create table sys_role_menu
(
id serial,
role_id integer not null
references sys_role
on delete cascade,
menu_id integer not null
references sys_menu
on delete cascade,
primary key (id, role_id, menu_id)
id bigserial,
role_id bigint not null
references ??? ()
on delete cascade,
menu_id bigint not null
references ??? ()
on delete cascade,
primary key ()
);
comment on column sys_role_menu.id is '主键ID';
@@ -519,19 +598,25 @@ comment on column sys_role_menu.role_id is '角色ID';
comment on column sys_role_menu.menu_id is '菜单ID';
alter table sys_role_menu
owner to postgres;
create unique index sys_role_menu_pkey
on sys_role_menu (id, role_id, menu_id);
create unique index ix_sys_role_menu_id
on sys_role_menu (id);
create table sys_role_data_scope
(
id serial,
role_id integer not null
references sys_role
on delete cascade,
data_scope_id integer not null
references sys_data_scope
on delete cascade,
primary key (id, role_id, data_scope_id)
id bigserial,
role_id bigint not null
references ??? ()
on delete cascade,
data_scope_id bigint not null
references ??? ()
on delete cascade,
primary key ()
);
comment on column sys_role_data_scope.id is '主键 ID';
@@ -540,34 +625,66 @@ comment on column sys_role_data_scope.role_id is '角色 ID';
comment on column sys_role_data_scope.data_scope_id is '数据范围 ID';
alter table sys_role_data_scope
owner to postgres;
create unique index sys_role_data_scope_pkey
on sys_role_data_scope (id, role_id, data_scope_id);
create unique index ix_sys_role_data_scope_id
on sys_role_data_scope (id);
create table sys_data_scope_rule
(
id bigserial,
data_scope_id bigint not null
references ??? ()
on delete cascade,
data_rule_id bigint not null
references ??? ()
on delete cascade,
primary key ()
);
comment on column sys_data_scope_rule.id is '主键ID';
comment on column sys_data_scope_rule.data_scope_id is '数据范围 ID';
comment on column sys_data_scope_rule.data_rule_id is '数据规则 ID';
alter table sys_data_scope_rule
owner to postgres;
create unique index sys_data_scope_rule_pkey
on sys_data_scope_rule (id, data_scope_id, data_rule_id);
create unique index ix_sys_data_scope_rule_id
on sys_data_scope_rule (id);
create table sys_user
(
id serial
primary key,
uuid varchar(50) not null
unique,
id bigserial,
uuid varchar(50) not null,
username varchar(20) not null,
nickname varchar(20) not null
unique,
password varchar(255),
salt bytea,
email varchar(50) not null,
nickname varchar(20) not null,
password varchar(255) not null,
salt bytea not null,
email varchar(50),
phone varchar(11),
avatar varchar(255),
status integer not null,
is_superuser integer not null,
is_staff integer not null,
status integer not null,
is_multi_login integer not null,
avatar varchar(255),
phone varchar(11),
join_time timestamp with time zone not null,
last_login_time timestamp with time zone,
dept_id integer
references sys_dept
on delete set null,
dept_id bigint
references ??? ()
on delete set null,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_user is '用户表';
@@ -584,18 +701,18 @@ comment on column sys_user.salt is '加密盐';
comment on column sys_user.email is '邮箱';
comment on column sys_user.phone is '手机号';
comment on column sys_user.avatar is '头像';
comment on column sys_user.status is '用户账号状态(0停用 1正常)';
comment on column sys_user.is_superuser is '超级权限(0否 1是)';
comment on column sys_user.is_staff is '后台管理登陆(0否 1是)';
comment on column sys_user.status is '用户账号状态(0停用 1正常)';
comment on column sys_user.is_multi_login is '是否重复登陆(0否 1是)';
comment on column sys_user.avatar is '头像';
comment on column sys_user.phone is '手机号';
comment on column sys_user.join_time is '注册时间';
comment on column sys_user.last_login_time is '上次登录';
@@ -606,33 +723,42 @@ comment on column sys_user.created_time is '创建时间';
comment on column sys_user.updated_time is '更新时间';
alter table sys_user
owner to postgres;
create unique index sys_user_pkey
on sys_user (id);
create unique index sys_user_uuid_key
on sys_user (uuid);
create unique index ix_sys_user_email
on sys_user (email);
create index ix_sys_user_id
on sys_user (id);
create unique index ix_sys_user_username
on sys_user (username);
create unique index ix_sys_user_id
on sys_user (id);
create index ix_sys_user_status
on sys_user (status);
create table sys_dict_data
(
id serial
primary key,
label varchar(32) not null
unique,
id bigserial,
label varchar(32) not null,
value varchar(32) not null,
sort integer not null,
status integer not null,
remark text,
type_id integer not null
references sys_dict_type
on delete cascade,
type_id bigint not null
references ??? ()
on delete cascade,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key (),
unique ()
);
comment on table sys_dict_data is '字典数据表';
@@ -655,13 +781,21 @@ comment on column sys_dict_data.created_time is '创建时间';
comment on column sys_dict_data.updated_time is '更新时间';
create index ix_sys_dict_data_id
alter table sys_dict_data
owner to postgres;
create unique index sys_dict_data_pkey
on sys_dict_data (id);
create unique index sys_dict_data_label_key
on sys_dict_data (label);
create unique index ix_sys_dict_data_id
on sys_dict_data (id);
create table gen_column
(
id serial
primary key,
id bigserial,
name varchar(50) not null,
comment varchar(255),
type varchar(20) not null,
@@ -671,9 +805,10 @@ create table gen_column
length integer not null,
is_pk boolean not null,
is_nullable boolean not null,
gen_business_id integer not null
references gen_business
on delete cascade
gen_business_id bigint not null
references ??? ()
on delete cascade,
primary key ()
);
comment on table gen_column is '代码生成模型列表';
@@ -700,19 +835,25 @@ comment on column gen_column.is_nullable is '是否可为空';
comment on column gen_column.gen_business_id is '代码生成业务ID';
create index ix_gen_column_id
alter table gen_column
owner to postgres;
create unique index gen_column_pkey
on gen_column (id);
create unique index ix_gen_column_id
on gen_column (id);
create table sys_user_role
(
id serial,
user_id integer not null
references sys_user
on delete cascade,
role_id integer not null
references sys_role
on delete cascade,
primary key (id, user_id, role_id)
id bigserial,
user_id bigint not null
references ??? ()
on delete cascade,
role_id bigint not null
references ??? ()
on delete cascade,
primary key ()
);
comment on column sys_user_role.id is '主键ID';
@@ -721,47 +862,47 @@ comment on column sys_user_role.user_id is '用户ID';
comment on column sys_user_role.role_id is '角色ID';
alter table sys_user_role
owner to postgres;
create unique index sys_user_role_pkey
on sys_user_role (id, user_id, role_id);
create unique index ix_sys_user_role_id
on sys_user_role (id);
create table sys_user_social
(
id serial
primary key,
id bigserial,
sid varchar(20) not null,
source varchar(20) not null,
open_id varchar(20),
sid varchar(20),
union_id varchar(20),
scope varchar(120),
code varchar(50),
user_id integer
references sys_user
on delete set null,
user_id bigint not null
references ??? ()
on delete cascade,
created_time timestamp with time zone not null,
updated_time timestamp with time zone
updated_time timestamp with time zone,
primary key ()
);
comment on table sys_user_social is '用户社交表(OAuth2';
comment on column sys_user_social.id is '主键 ID';
comment on column sys_user_social.sid is '第三方用户 ID';
comment on column sys_user_social.source is '第三方用户来源';
comment on column sys_user_social.open_id is '第三方用户 open id';
comment on column sys_user_social.uid is '第三方用户 ID';
comment on column sys_user_social.union_id is '第三方用户 union id';
comment on column sys_user_social.scope is '第三方用户授予的权限';
comment on column sys_user_social.code is '用户的授权 code';
comment on column sys_user_social.user_id is '用户关联ID';
comment on column sys_user_social.created_time is '创建时间';
comment on column sys_user_social.updated_time is '更新时间';
create index ix_sys_user_social_id
alter table sys_user_social
owner to postgres;
create unique index sys_user_social_pkey
on sys_user_social (id);
create unique index ix_sys_user_social_id
on sys_user_social (id);
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
from dataclasses import dataclass
from backend.common.dataclasses import SnowflakeInfo
from backend.common.exception import errors
from backend.core.conf import settings
@dataclass(frozen=True)
class SnowflakeConfig:
"""雪花算法配置类"""
# 位分配
WORKER_ID_BITS: int = 5
DATACENTER_ID_BITS: int = 5
SEQUENCE_BITS: int = 12
# 最大值
MAX_WORKER_ID: int = (1 << WORKER_ID_BITS) - 1 # 31
MAX_DATACENTER_ID: int = (1 << DATACENTER_ID_BITS) - 1 # 31
SEQUENCE_MASK: int = (1 << SEQUENCE_BITS) - 1 # 4095
# 位移偏移
WORKER_ID_SHIFT: int = SEQUENCE_BITS
DATACENTER_ID_SHIFT: int = SEQUENCE_BITS + WORKER_ID_BITS
TIMESTAMP_LEFT_SHIFT: int = SEQUENCE_BITS + WORKER_ID_BITS + DATACENTER_ID_BITS
# 元年时间戳
EPOCH: int = 1262275200000
# 默认值
DEFAULT_DATACENTER_ID: int = 1
DEFAULT_WORKER_ID: int = 0
DEFAULT_SEQUENCE: int = 0
class Snowflake:
"""雪花算法类"""
def __init__(
self,
cluster_id: int = SnowflakeConfig.DEFAULT_DATACENTER_ID,
node_id: int = SnowflakeConfig.DEFAULT_WORKER_ID,
sequence: int = SnowflakeConfig.DEFAULT_SEQUENCE,
):
"""
初始化雪花算法生成器
:param cluster_id: 集群 ID (0-31)
:param node_id: 节点 ID (0-31)
:param sequence: 起始序列号
"""
if cluster_id < 0 or cluster_id > SnowflakeConfig.MAX_DATACENTER_ID:
raise errors.ForbiddenError(msg=f'集群编号必须在 0-{SnowflakeConfig.MAX_DATACENTER_ID} 之间')
if node_id < 0 or node_id > SnowflakeConfig.MAX_WORKER_ID:
raise errors.ForbiddenError(msg=f'节点编号必须在 0-{SnowflakeConfig.MAX_WORKER_ID} 之间')
self.node_id = node_id
self.cluster_id = cluster_id
self.sequence = sequence
self.last_timestamp = -1
@staticmethod
def _current_millis() -> int:
"""返回当前毫秒时间戳"""
return int(time.time() * 1000)
def _next_millis(self, last_timestamp: int) -> int:
"""
等待至下一毫秒
:param last_timestamp: 上次生成 ID 的时间戳
:return:
"""
timestamp = self._current_millis()
while timestamp <= last_timestamp:
time.sleep((last_timestamp - timestamp + 1) / 1000.0)
timestamp = self._current_millis()
return timestamp
def generate(self) -> int:
"""生成雪花 ID"""
timestamp = self._current_millis()
if timestamp < self.last_timestamp:
raise errors.ServerError(msg=f'系统时间倒退,拒绝生成 ID 直到 {self.last_timestamp}')
if timestamp == self.last_timestamp:
self.sequence = (self.sequence + 1) & SnowflakeConfig.SEQUENCE_MASK
if self.sequence == 0:
timestamp = self._next_millis(self.last_timestamp)
else:
self.sequence = 0
self.last_timestamp = timestamp
return (
((timestamp - SnowflakeConfig.EPOCH) << SnowflakeConfig.TIMESTAMP_LEFT_SHIFT)
| (self.cluster_id << SnowflakeConfig.DATACENTER_ID_SHIFT)
| (self.node_id << SnowflakeConfig.WORKER_ID_SHIFT)
| self.sequence
)
@staticmethod
def parse_id(snowflake_id: int) -> SnowflakeInfo:
"""
解析雪花 ID,获取其包含的详细信息
:param snowflake_id: 雪花ID
:return:
"""
timestamp = (snowflake_id >> SnowflakeConfig.TIMESTAMP_LEFT_SHIFT) + SnowflakeConfig.EPOCH
cluster_id = (snowflake_id >> SnowflakeConfig.DATACENTER_ID_SHIFT) & SnowflakeConfig.MAX_DATACENTER_ID
node_id = (snowflake_id >> SnowflakeConfig.WORKER_ID_SHIFT) & SnowflakeConfig.MAX_WORKER_ID
sequence = snowflake_id & SnowflakeConfig.SEQUENCE_MASK
return SnowflakeInfo(
timestamp=timestamp,
datetime=time.strftime(settings.DATETIME_FORMAT, time.localtime(timestamp / 1000)),
cluster_id=cluster_id,
node_id=node_id,
sequence=sequence,
)
snowflake = Snowflake()