本项目是基于 Hashicorp 官方的 github.com/hashicorp/hcl/v2 实现一个解析 HCL 文件并检查错误的工具,当前只需实现对数据库 schema 定义的 hcl 文件的解析与校验。
go install e.coding.net/letmelife/open/hclvet@latest
# 基本用法
hclvet <hcl文件路径>
# 列出所有可用的规则
hclvet -list-rules
# 禁用特定规则
hclvet -disable=require-comment,non-zero-datetime <hcl文件路径>
# 仅启用特定规则
hclvet -enable=require-primary-key,varchar-length <hcl文件路径>
hclvet 支持以下检查规则,其中一部分规则默认启用:
| 规则ID | 说明 | 默认启用 |
|---|---|---|
| require-comment | 要求列必须有注释说明 | ❌ |
| require-default-null | 要求可为空的列必须设置默认值 | ✅ |
| non-zero-datetime | 要求datetime类型的列默认值必须非零 | ✅ |
| require-primary-key | 要求表必须定义主键 | ✅ |
| valid-schema-ref | 要求表引用的schema必须存在 | ✅ |
| varchar-length | 要求varchar类型必须指定长度 | ✅ |
| type-lowercase | 要求类型名称必须使用小写字母 | ✅ |
| table-comment | 要求表必须有注释说明 | ❌ |
| column-naming | 要求列名使用snake_case命名规范 | ✅ |
null = true)应该设置默认值datetime 的字段默认值要非零值table "accounting_record" { schema = schema.tcstore comment = "入账记录表" column "id" { null = false type = bigint } column "trade_id" { null = false type = varchar(50) } column "pay_way" { null = false default = -1 type = tinyint comment = "支付方式(0.微信,1.支付宝)" } column "type" { null = false type = tinyint default = 0 } column "goods_flag" { null = false type = tinyint default = 0 comment = "商品类型" } column "amount" { null = false type = decimal(15,2) unsigned = false comment = "实际金额" } column "goods_id" { null = true type = varchar(50) default = "" comment = "商品id" } column "cost_price" { null = true type = decimal(15,2) unsigned = false comment = "成本价" } column "venue_id" { null = true type = varchar(50) default = "" comment = "点位 id" } column "administrator_id" { null = true type = varchar(50) default = "" } column "venue_admin_id" { null = true type = varchar(50) default = "" } column "bd_id" { null = true type = varchar(50) default = "" } column "create_time" { null = true type = datetime } column "update_time" { null = true type = datetime } primary_key { columns = [column.id] } index "accounting_record_trade_id" { columns = [column.trade_id] } } schema "tcstore" { charset = "utf8mb4" collate = "utf8mb4_general_ci" }
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)