Skip to content

Commit

Permalink
feat: 创建数据库脚本与model
Browse files Browse the repository at this point in the history
  • Loading branch information
minibear2333 committed Jun 12, 2021
1 parent 2f64350 commit 9c23f23
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# db tmp data
storage/*
17 changes: 17 additions & 0 deletions create-dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

# configs:配置文件。
# docs:文档集合。
# global:全局变量。
# internal:内部模块。
# dao:数据访问层(Database Access Object),所有与数据相关的操作都会在 dao 层进行,例如 MySQL、ElasticSearch 等。
# middleware:HTTP 中间件。
# model:模型层,用于存放 model 对象。
# routers:路由相关逻辑处理。
# service:项目核心业务逻辑。
# pkg:项目相关的模块包。
# storage:项目生成的临时文件。
# scripts:各类构建,安装,分析等操作的脚本。
# third_party:第三方的资源工具,例如 Swagger UI。

mkdir {configs,docs,global,internal,pkg,storage,scripts,third_party}
mkdir internal/{dao,middleware,model,routers,service}
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.7'
services:
blog_service:
image: mysql:5.7
ports:
- 3306:3306
command: --init-file /data/application/init.sql
volumes:
- ./storage/dbdata:/var/lib/mysql
- ./scripts/init.sql:/data/application/init.sql
container_name: blog_service
environment:
- PUID=1000
- PGID=1000
- MYSQL_ROOT_PASSWORD=root
- TZ=Asia/Shanghai
- MYSQL_DATABASE=blog_service
- MYSQL_USER=blog_service
- MYSQL_PASSWORD=blog_service
restart: unless-stopped
14 changes: 14 additions & 0 deletions internal/model/article.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

type Article struct {
*Model
Title string `json:"title"`
Desc string `json:"desc"`
Content string `json:"content"`
CoverImageUrl string `json:"cover_image_url"`
State uint8 `json:"state"`
}

func (a Article) TableName() string {
return "blog_article"
}
11 changes: 11 additions & 0 deletions internal/model/article_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

type ArticleTag struct {
*Model
TagID uint32 `json:"tag_id"`
ArticleID uint32 `json:"article_id"`
}

func (a ArticleTag) TableName() string {
return "blog_article_tag"
}
12 changes: 12 additions & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

// Model 作为公共部分字段被其他model引入
type Model struct {
ID uint32 `gorm:"primary_key" json:"id"`
CreatedBy string `json:"created_by"`
ModifiedBy string `json:"modified_by"`
CreatedOn uint32 `json:"created_on"`
ModifiedOn uint32 `json:"modified_on"`
DeletedOn uint32 `json:"deleted_on"`
IsDel uint8 `json:"is_del"`
}
11 changes: 11 additions & 0 deletions internal/model/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

type Tag struct {
*Model
Name string `json:"name"`
State uint8 `json:"state"`
}

func (t Tag) TableName() string {
return "blog_tag"
}
54 changes: 54 additions & 0 deletions scripts/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
CREATE
DATABASE
IF
NOT EXISTS blog_service DEFAULT CHARACTER
SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;

use blog_service;
-- 创建标签表
CREATE TABLE IF NOT EXISTS `blog_tag`
(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT '' COMMENT '标签名称',
`created_on` int(10) unsigned DEFAULT '0' COMMENT '创建时间',
`created_by` varchar(100) DEFAULT '' COMMENT '创建人',
`modified_on` int(10) unsigned DEFAULT '0' COMMENT '修改时间',
`modified_by` varchar(100) DEFAULT '' COMMENT '修改人',
`deleted_on` int(10) unsigned DEFAULT '0' COMMENT '删除时间',
`is_del` tinyint(3) unsigned DEFAULT '0' COMMENT '是否删除 0 为未删除、1 为已删除',
`state` tinyint(3) unsigned DEFAULT '1' COMMENT '状态 0 为禁用、1 为启用',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='标签管理';

-- 创建文章表
CREATE TABLE IF NOT EXISTS `blog_article`
(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) DEFAULT '' COMMENT '文章标题',
`desc` varchar(255) DEFAULT '' COMMENT '文章简述',
`cover_image_url` varchar(255) DEFAULT '' COMMENT '封面图片地址',
`content` longtext COMMENT '文章内容',
`created_on` int(10) unsigned DEFAULT '0' COMMENT '创建时间',
`created_by` varchar(100) DEFAULT '' COMMENT '创建人',
`modified_on` int(10) unsigned DEFAULT '0' COMMENT '修改时间',
`modified_by` varchar(100) DEFAULT '' COMMENT '修改人',
`deleted_on` int(10) unsigned DEFAULT '0' COMMENT '删除时间',
`is_del` tinyint(3) unsigned DEFAULT '0' COMMENT '是否删除 0 为未删除、1 为已删除',
`state` tinyint(3) unsigned DEFAULT '1' COMMENT '状态 0 为禁用、1 为启用',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文章管理';

-- 创建文章标签关联表
CREATE TABLE IF NOT EXISTS `blog_article_tag`
(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(11) NOT NULL COMMENT '文章 ID',
`tag_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '标签 ID',
`created_on` int(10) unsigned DEFAULT '0' COMMENT '创建时间',
`created_by` varchar(100) DEFAULT '' COMMENT '创建人',
`modified_on` int(10) unsigned DEFAULT '0' COMMENT '修改时间',
`modified_by` varchar(100) DEFAULT '' COMMENT '修改人',
`deleted_on` int(10) unsigned DEFAULT '0' COMMENT '删除时间',
`is_del` tinyint(3) unsigned DEFAULT '0' COMMENT '是否删除 0 为未删除、1 为已删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文章标签关联';
1 change: 1 addition & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose up -d

0 comments on commit 9c23f23

Please sign in to comment.