Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/dev_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- dev
paths-ignore:
- 'readme.md'
- 'script'

jobs:
build_and_deploy:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/prod_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- main
paths-ignore:
- 'readme.md'
- 'script'

jobs:
build_and_deploy:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ pip-log.txt
pip-delete-this-directory.txt
.DS_Store

# 数据库文件
db.sqlite3

# 环境变量文件,开发时使用,生成部署时改为环境变量
.env
Binary file removed db.sqlite3
Binary file not shown.
1 change: 1 addition & 0 deletions dbinit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import script.dbinit
162 changes: 162 additions & 0 deletions script/dbinit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import environ
import os
from pathlib import Path
import sqlite3
import re
import pymysql


env = environ.Env()
BASE_DIR = Path(__file__).resolve().parent.parent
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

# 检测环境文件是否存在以及DB_ENGINE是否存在
if not os.path.exists(os.path.join(BASE_DIR, ".env")):
print("环境文件不存在")
exit()
if not env("DB_ENGINE"):
print("DB_ENGINE不存在")
exit()

# 判断数据库类型
DB_ENGINE = env("DB_ENGINE")

if DB_ENGINE == "sqlite3":
# 判断是否存在db.sqlite3文件
if not os.path.exists(os.path.join(BASE_DIR, "db.sqlite3")):
# 创建db.sqlite3文件
open(os.path.join(BASE_DIR, "db.sqlite3"), "w").close()
# 若存在,询问是否需要删除并重新初始化
if os.path.exists(os.path.join(BASE_DIR, "db.sqlite3")):
if input("是否需要删除并重新初始化数据库?(y/n): ") == "y":
os.remove(os.path.join(BASE_DIR, "db.sqlite3"))
open(os.path.join(BASE_DIR, "db.sqlite3"), "w").close()
else:
print("数据库初始化失败")
exit()

# 连接数据库
connection = sqlite3.connect(os.path.join(BASE_DIR, "db.sqlite3"))

# 读取sql文件夹下的所有sql文件并执行
sql_dir = os.path.join(BASE_DIR, "script", "sql")
for file in os.listdir(sql_dir):
print(f"正在处理:{file}")
with open(os.path.join(sql_dir, file), "r") as f:
sql_content = f.read()

# 提取创建表的SQL语句
create_table_match = re.search(r'CREATE TABLE[^;]*;', sql_content, re.DOTALL | re.IGNORECASE)
insert_data_match = re.search(r'INSERT INTO[^;]*;', sql_content, re.DOTALL | re.IGNORECASE)

# 执行创建表的SQL
cursor = connection.cursor()
try:
if create_table_match:
# 替换MySQL特有的语法为SQLite兼容语法
create_sql = create_table_match.group(0)

# 删除ENGINE相关的语句
create_sql = re.sub(r'ENGINE=.*?;', ';', create_sql)

# 正确处理SQLite的AUTOINCREMENT语法(应该是PRIMARY KEY AUTOINCREMENT)
create_sql = create_sql.replace('AUTO_INCREMENT', '')
create_sql = re.sub(r'`id`\s+bigint\s+NOT\s+NULL\s*,', '`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,', create_sql)
create_sql = re.sub(r'`id`\s+INTEGER\s+NOT\s+NULL\s*,', '`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,', create_sql)
create_sql = re.sub(r'`id`\s+int\s+NOT\s+NULL\s*,', '`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,', create_sql)

# 替换一些不兼容的数据类型
create_sql = create_sql.replace('bigint', 'INTEGER')
create_sql = create_sql.replace('tinyint(1)', 'BOOLEAN')
create_sql = create_sql.replace('int', 'INTEGER')

# 移除UNIQUE KEY和KEY语句
create_sql = re.sub(r',\s*UNIQUE KEY\s+`[^`]+`\s+\([^)]+\)', '', create_sql)
create_sql = re.sub(r',\s*KEY\s+`[^`]+`\s+\([^)]+\)', '', create_sql)

# 移除CONSTRAINT相关内容
create_sql = re.sub(r',\s*CONSTRAINT.*?REFERENCES.*?\)', '', create_sql)

# 移除PRIMARY KEY语句(因为我们已经在id字段添加了PRIMARY KEY)
create_sql = re.sub(r',\s*PRIMARY KEY\s+\(`id`\)', '', create_sql)

print(f"执行创建表:{create_sql[:50]}...")
cursor.execute(create_sql)
connection.commit()

if insert_data_match:
insert_sql = insert_data_match.group(0)
print(f"执行插入数据:{insert_sql[:50]}...")
cursor.execute(insert_sql)
connection.commit()

except sqlite3.Error as e:
print(f"执行SQL出错:{e}")
continue
finally:
cursor.close()

connection.close()
print("SQLite数据库初始化完成!")
elif DB_ENGINE == "mysql":
# 检验env内容是否完整
if not env("DB_NAME"):
print("DB_NAME不存在,请在.env文件中配置DB_NAME")
exit()
if not env("DB_USER"):
print("DB_USER不存在,请在.env文件中配置DB_USER")
exit()
if not env("DB_PASSWORD"):
print("DB_PASSWORD不存在,请在.env文件中配置DB_PASSWORD")
exit()
if not env("DB_HOST"):
print("DB_HOST不存在,请在.env文件中配置DB_HOST")
exit()

# 连接到MySQL数据库
try:
connection = pymysql.connect(
host=env("DB_HOST"),
user=env("DB_USER"),
password=env("DB_PASSWORD"),
database=env("DB_NAME")
)
cursor = connection.cursor()

# 读取sql文件夹下的所有sql文件并执行
sql_dir = os.path.join(BASE_DIR, "script", "sql")
for file in os.listdir(sql_dir):
print(f"正在处理:{file}")
with open(os.path.join(sql_dir, file), "r") as f:
sql_content = f.read()

# 执行SQL语句
try:
for statement in sql_content.split(';'):
if statement.strip():
print(f"执行SQL:{statement[:50]}...")
cursor.execute(statement)
connection.commit()
except pymysql.MySQLError as e:
print(f"执行SQL出错:{e}")
connection.rollback()
continue

cursor.close()
connection.close()
print("MySQL数据库初始化完成!")

except pymysql.MySQLError as e:
print(f"连接MySQL数据库出错:{e}")
exit()

else:
print("初始化脚本类型目前只支持sqlite3和mysql, 其余类型请自行执行sql文件(文件夹位置:script/sql)")
exit()







51 changes: 51 additions & 0 deletions script/sql/puredrf_auth_group.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- MySQL dump 10.13 Distrib 8.0.40, for macos14 (arm64)
--
-- Host: 47.243.254.174 Database: puredrf
-- ------------------------------------------------------
-- Server version 8.0.41-0ubuntu0.22.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `auth_group`
--

DROP TABLE IF EXISTS `auth_group`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `auth_group` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(150) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `auth_group`
--

LOCK TABLES `auth_group` WRITE;
/*!40000 ALTER TABLE `auth_group` DISABLE KEYS */;
/*!40000 ALTER TABLE `auth_group` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2025-05-02 13:45:52
55 changes: 55 additions & 0 deletions script/sql/puredrf_auth_group_permissions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- MySQL dump 10.13 Distrib 8.0.40, for macos14 (arm64)
--
-- Host: 47.243.254.174 Database: puredrf
-- ------------------------------------------------------
-- Server version 8.0.41-0ubuntu0.22.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `auth_group_permissions`
--

DROP TABLE IF EXISTS `auth_group_permissions`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `auth_group_permissions` (
`id` bigint NOT NULL AUTO_INCREMENT,
`group_id` int NOT NULL,
`permission_id` int NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `auth_group_permissions_group_id_permission_id_0cd325b0_uniq` (`group_id`,`permission_id`),
KEY `auth_group_permissio_permission_id_84c5c92e_fk_auth_perm` (`permission_id`),
CONSTRAINT `auth_group_permissio_permission_id_84c5c92e_fk_auth_perm` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`),
CONSTRAINT `auth_group_permissions_group_id_b120cbf9_fk_auth_group_id` FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `auth_group_permissions`
--

LOCK TABLES `auth_group_permissions` WRITE;
/*!40000 ALTER TABLE `auth_group_permissions` DISABLE KEYS */;
/*!40000 ALTER TABLE `auth_group_permissions` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2025-05-02 13:46:15
55 changes: 55 additions & 0 deletions script/sql/puredrf_auth_permission.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- MySQL dump 10.13 Distrib 8.0.40, for macos14 (arm64)
--
-- Host: 47.243.254.174 Database: puredrf
-- ------------------------------------------------------
-- Server version 8.0.41-0ubuntu0.22.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `auth_permission`
--

DROP TABLE IF EXISTS `auth_permission`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `auth_permission` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content_type_id` int NOT NULL,
`codename` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `auth_permission_content_type_id_codename_01ab375a_uniq` (`content_type_id`,`codename`),
CONSTRAINT `auth_permission_content_type_id_2f476e4b_fk_django_co` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `auth_permission`
--

LOCK TABLES `auth_permission` WRITE;
/*!40000 ALTER TABLE `auth_permission` DISABLE KEYS */;
INSERT INTO `auth_permission` VALUES (1,'Can add permission',1,'add_permission'),(2,'Can change permission',1,'change_permission'),(3,'Can delete permission',1,'delete_permission'),(4,'Can view permission',1,'view_permission'),(5,'Can add group',2,'add_group'),(6,'Can change group',2,'change_group'),(7,'Can delete group',2,'delete_group'),(8,'Can view group',2,'view_group'),(9,'Can add log entry',3,'add_logentry'),(10,'Can change log entry',3,'change_logentry'),(11,'Can delete log entry',3,'delete_logentry'),(12,'Can view log entry',3,'view_logentry'),(13,'Can add content type',4,'add_contenttype'),(14,'Can change content type',4,'change_contenttype'),(15,'Can delete content type',4,'delete_contenttype'),(16,'Can view content type',4,'view_contenttype'),(17,'Can add session',5,'add_session'),(18,'Can change session',5,'change_session'),(19,'Can delete session',5,'delete_session'),(20,'Can view session',5,'view_session'),(21,'Can add user',6,'add_user'),(22,'Can change user',6,'change_user'),(23,'Can delete user',6,'delete_user'),(24,'Can view user',6,'view_user'),(25,'Can add 菜单/权限',7,'add_menu'),(26,'Can change 菜单/权限',7,'change_menu'),(27,'Can delete 菜单/权限',7,'delete_menu'),(28,'Can view 菜单/权限',7,'view_menu'),(29,'Can add Menu meta',8,'add_menumeta'),(30,'Can change Menu meta',8,'change_menumeta'),(31,'Can delete Menu meta',8,'delete_menumeta'),(32,'Can view Menu meta',8,'view_menumeta'),(33,'Can add 角色表',9,'add_role'),(34,'Can change 角色表',9,'change_role'),(35,'Can delete 角色表',9,'delete_role'),(36,'Can view 角色表',9,'view_role'),(37,'Can add 部门',10,'add_deptinfo'),(38,'Can change 部门',10,'change_deptinfo'),(39,'Can delete 部门',10,'delete_deptinfo'),(40,'Can view 部门',10,'view_deptinfo'),(41,'Can add 登录日志',11,'add_loginlog'),(42,'Can change 登录日志',11,'change_loginlog'),(43,'Can delete 登录日志',11,'delete_loginlog'),(44,'Can view 登录日志',11,'view_loginlog'),(45,'Can add 操作日志',12,'add_operationlog'),(46,'Can change 操作日志',12,'change_operationlog'),(47,'Can delete 操作日志',12,'delete_operationlog'),(48,'Can view 操作日志',12,'view_operationlog');
/*!40000 ALTER TABLE `auth_permission` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2025-05-02 13:46:06
Loading