From 4247d4e486e6a4ad1ec7d1ab470ab0a2a7fd8588 Mon Sep 17 00:00:00 2001
From: ning <710leo@gmail.com>
Date: Mon, 2 Dec 2024 11:09:47 +0800
Subject: [PATCH] docs: fix n9e init sql

---
 docker/initsql/a-n9e.sql    | 125 +++++++++++++++++++-----------------
 models/builtin_component.go |   2 +-
 models/builtin_metrics.go   |   8 +--
 models/migrate/migrate.go   |  15 +++--
 4 files changed, 80 insertions(+), 70 deletions(-)

diff --git a/docker/initsql/a-n9e.sql b/docker/initsql/a-n9e.sql
index d768567d1..836d9877d 100644
--- a/docker/initsql/a-n9e.sql
+++ b/docker/initsql/a-n9e.sql
@@ -15,8 +15,8 @@ CREATE TABLE `users` (
     `roles` varchar(255) not null comment 'Admin | Standard | Guest, split by space',
     `contacts` varchar(1024) comment 'json e.g. {wecom:xx, dingtalk_robot_token:yy}',
     `maintainer` tinyint(1) not null default 0,
-    `belong` varchar(16) not null default '' comment 'belong',
-    `last_active_time` bigint not null default 0,
+    `belong` varchar(191) DEFAULT '' COMMENT 'belong',
+    `last_active_time` bigint DEFAULT 0 COMMENT 'last_active_time',
     `create_at` bigint not null default 0,
     `create_by` varchar(64) not null default '',
     `update_at` bigint not null default 0,
@@ -56,14 +56,14 @@ insert into user_group_member(group_id, user_id) values(1, 1);
 CREATE TABLE `configs` (
     `id` bigint unsigned not null auto_increment,
     `ckey` varchar(191) not null,
-    `cval` text not null,
-    `note` varchar(1024) not null default '',
-    `external` tinyint(1) not null default 0,
-    `encrypted` tinyint(1) not null default 0,
-    `create_at` bigint not null default 0,
-    `create_by` varchar(64) not null default '',
-    `update_at` bigint not null default 0,
-    `update_by` varchar(64) not null default '',
+    `note` varchar(1024) NOT NULL DEFAULT '' COMMENT 'note',
+    `cval` text COMMENT 'config value',
+    `external` bigint NOT NULL DEFAULT 0 COMMENT '0 means built-in 1 means external',
+    `encrypted` bigint DEFAULT 0 COMMENT '0 means plaintext 1 means ciphertext',
+    `create_at` bigint DEFAULT 0 COMMENT 'create_at',
+    `create_by` varchar(64) NOT NULL DEFAULT '' COMMENT 'cerate_by',
+    `update_at` bigint DEFAULT 0 COMMENT 'update_at',
+    `update_by` varchar(64) NOT NULL DEFAULT '' COMMENT 'update_by',
     PRIMARY KEY (`id`)
 ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
 
@@ -198,6 +198,7 @@ CREATE TABLE `board` (
     `create_by` varchar(64) not null default '',
     `update_at` bigint not null default 0,
     `update_by` varchar(64) not null default '',
+    `public_cate` bigint NOT NULL NOT NULL DEFAULT 0 COMMENT '0 anonymous 1 login 2 busi',
     PRIMARY KEY (`id`),
     UNIQUE KEY (`group_id`, `name`),
     KEY(`ident`)
@@ -249,7 +250,7 @@ CREATE TABLE `chart` (
 CREATE TABLE `chart_share` (
     `id` bigint unsigned not null auto_increment,
     `cluster` varchar(128) not null,
-    `datasource_id` bigint unsigned not null default 0,
+    `datasource_id` bigint NOT NULL NOT NULL DEFAULT 0 COMMENT 'datasource id',
     `configs` text,
     `create_at` bigint not null default 0,
     `create_by` varchar(64) not null default '',
@@ -294,6 +295,7 @@ CREATE TABLE `alert_rule` (
     `create_by` varchar(64) not null default '',
     `update_at` bigint not null default 0,
     `update_by` varchar(64) not null default '',
+    `datasource_queries` text,
     PRIMARY KEY (`id`),
     KEY (`group_id`),
     KEY (`update_at`)
@@ -334,6 +336,7 @@ CREATE TABLE `alert_subscribe` (
     `datasource_ids` varchar(255) not null default '' comment 'datasource ids',
     `cluster` varchar(128) not null,
     `rule_id` bigint not null default 0,
+    `rule_ids` varchar(1024),
     `severities` varchar(32) not null default '',
     `tags` varchar(4096) not null default '' comment 'json,map,tagkey->regexp|value',
     `redefine_severity` tinyint(1) default 0 comment 'is redefine severity?',
@@ -341,9 +344,8 @@ CREATE TABLE `alert_subscribe` (
     `redefine_channels` tinyint(1) default 0 comment 'is redefine channels?',
     `new_channels` varchar(255) not null default '' comment 'split by space: sms voice email dingtalk wecom',
     `user_group_ids` varchar(250) not null comment 'split by space 1 34 5, notify cc to user_group_ids',
-    `busi_groups` VARCHAR(4096) NOT NULL DEFAULT '[]',
+    `busi_groups` varchar(4096) NOT NULL,
     `note` VARCHAR(1024) DEFAULT '' COMMENT 'note',
-    `rule_ids` VARCHAR(1024) DEFAULT '' COMMENT 'rule_ids',
     `webhooks` text not null,
     `extra_config` text not null comment 'extra_config',
     `redefine_webhooks` tinyint(1) default 0,
@@ -363,15 +365,19 @@ CREATE TABLE `target` (
     `ident` varchar(191) not null comment 'target id',
     `note` varchar(255) not null default '' comment 'append to alert event as field',
     `tags` varchar(512) not null default '' comment 'append to series data as tags, split by space, append external space at suffix',
-    `host_tags` varchar(512) not null default '' comment 'append to series data as tags, split by space, append external space at suffix',
+    `host_tags` text COMMENT 'global labels set in conf file',
     `host_ip` varchar(15) default '' COMMENT 'IPv4 string',
     `agent_version` varchar(255) default '' COMMENT 'agent version',
-    `engine_name` varchar(255) default '' COMMENT 'engine_name',
+    `engine_name` varchar(255) DEFAULT '' COMMENT 'engine name',
     `os` VARCHAR(31) DEFAULT '' COMMENT 'os type',
     `update_at` bigint not null default 0,
     PRIMARY KEY (`id`),
     UNIQUE KEY (`ident`),
-    KEY (`group_id`)
+    KEY (`group_id`),
+    INDEX `idx_host_ip` (`host_ip`),
+    INDEX `idx_agent_version` (`agent_version`),
+    INDEX `idx_engine_name` (`engine_name`),
+    INDEX `idx_os` (`os`)
 ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
 
 
@@ -401,11 +407,12 @@ CREATE TABLE `recording_rule` (
     `prom_eval_interval` int not null comment 'evaluate interval',
     `cron_pattern` varchar(255) default '' comment 'cron pattern',
     `append_tags` varchar(255) default '' comment 'split by space: service=n9e mod=api',
-    `query_configs` text not null comment 'query configs',
+    `query_configs` text NOT NULL,
     `create_at` bigint default '0',
     `create_by` varchar(64) default '',
     `update_at` bigint default '0',
     `update_by` varchar(64) default '',
+    `datasource_queries` text,
     PRIMARY KEY (`id`),
     KEY `group_id` (`group_id`),
     KEY `update_at` (`update_at`)
@@ -502,7 +509,7 @@ CREATE TABLE `alert_his_event` (
     `annotations` text not null comment 'annotations',
     `rule_config` text not null comment 'annotations',
     PRIMARY KEY (`id`),
-    KEY (`last_eval_time`),
+    INDEX `idx_last_eval_time` (`last_eval_time`),
     KEY (`hash`),
     KEY (`rule_id`),
     KEY (`trigger_time`, `group_id`)
@@ -515,21 +522,22 @@ CREATE TABLE `board_busigroup` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
 CREATE TABLE `builtin_components` (
-  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '''unique identifier''',
-  `ident` varchar(191) NOT NULL COMMENT '''identifier of component''',
+  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'unique identifier',
+  `ident` varchar(191) NOT NULL,
   `logo` varchar(191) NOT NULL COMMENT '''logo of component''',
   `readme` text NOT NULL COMMENT '''readme of component''',
-  `created_at` bigint(20) NOT NULL DEFAULT 0 COMMENT '''create time''',
+  `created_at` bigint NOT NULL DEFAULT 0 COMMENT '''create time''',
   `created_by` varchar(191) NOT NULL DEFAULT '' COMMENT '''creator''',
-  `updated_at` bigint(20) NOT NULL DEFAULT 0 COMMENT '''update time''',
+  `updated_at` bigint NOT NULL DEFAULT 0 COMMENT '''update time''',
   `updated_by` varchar(191) NOT NULL DEFAULT '' COMMENT '''updater''',
   PRIMARY KEY (`id`),
-  KEY `idx_ident` (`ident`)
+  UNIQUE KEY `idx_ident` (`ident`) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
+
 CREATE TABLE `builtin_payloads` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '''unique identifier''',
-  `component_id` bigint(20) NOT NULL DEFAULT 0 COMMENT 'component_id',
+  `component_id` bigint NOT NULL DEFAULT 0 COMMENT '''component_id of payload''',
   `uuid` bigint(20) NOT NULL COMMENT '''uuid of payload''',
   `type` varchar(191) NOT NULL COMMENT '''type of payload''',
   `component` varchar(191) NOT NULL COMMENT '''component of payload''',
@@ -551,13 +559,13 @@ CREATE TABLE `builtin_payloads` (
 
 CREATE TABLE notification_record (
     `id` BIGINT PRIMARY KEY AUTO_INCREMENT,
-    `event_id` BIGINT NOT NULL,
-    `sub_id` BIGINT NOT NULL,
-    `channel` VARCHAR(255) NOT NULL,
-    `status` TINYINT NOT NULL DEFAULT 0,
-    `target` VARCHAR(1024) NOT NULL,
-    `details` VARCHAR(2048),
-    `created_at` BIGINT NOT NULL,
+    `event_id`  bigint NOT NULL COMMENT 'event history id',
+    `sub_id`  bigint COMMENT 'subscribed rule id',
+    `channel` varchar(255) NOT NULL COMMENT 'notification channel name',
+    `status` bigint COMMENT 'notification status',
+    `target` varchar(1024) NOT NULL COMMENT 'notification target',
+    `details` varchar(2048) DEFAULT '' COMMENT 'notification other info',
+    `created_at` bigint NOT NULL COMMENT 'create time',
     INDEX idx_evt (event_id)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
@@ -612,7 +620,7 @@ CREATE TABLE `task_record`
     PRIMARY KEY (`id`),
     KEY (`create_at`, `group_id`),
     KEY (`create_by`),
-    KEY (`event_id`)
+    INDEX `idx_event_id` (`event_id`)
 ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
 
 CREATE TABLE `alerting_engines`
@@ -639,7 +647,7 @@ CREATE TABLE `datasource`
     `status` varchar(255) not null default '',
     `http` varchar(4096) not null default '',
     `auth` varchar(8192) not null default '',
-    `is_default` tinyint not null default 0,
+    `is_default` boolean COMMENT 'is default datasource',
     `created_at` bigint not null default 0,
     `created_by` varchar(64) not null default '',
     `updated_at` bigint not null default 0,
@@ -660,10 +668,10 @@ CREATE TABLE `notify_tpl` (
     `channel` varchar(32) not null,
     `name` varchar(255) not null,
     `content` text not null,
-    `create_at` bigint not null default 0,
-    `create_by` varchar(64) not null default '',
-    `update_at` bigint not null default 0,
-    `update_by` varchar(64) not null default '',
+    `create_at` bigint DEFAULT 0 COMMENT 'create_at',
+    `create_by` varchar(64) DEFAULT '' COMMENT 'cerate_by',
+    `update_at` bigint DEFAULT 0 COMMENT 'update_at',
+    `update_by` varchar(64) DEFAULT '' COMMENT 'update_by',
     PRIMARY KEY (`id`),
     UNIQUE KEY (`channel`)
 ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
@@ -672,7 +680,7 @@ CREATE TABLE `sso_config` (
     `id` bigint unsigned not null auto_increment,
     `name` varchar(191) not null,
     `content` text not null,
-    `update_at` bigint not null default 0,
+    `update_at` bigint DEFAULT 0 COMMENT 'update_at',
     PRIMARY KEY (`id`),
     UNIQUE KEY (`name`)
 ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
@@ -694,36 +702,37 @@ CREATE TABLE `es_index_pattern` (
 
 CREATE TABLE `builtin_metrics` (
     `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'unique identifier',
-    `collector` varchar(191) NOT NULL COMMENT 'type of collector',
-    `typ` varchar(191) NOT NULL COMMENT 'type of metric',
-    `name` varchar(191) NOT NULL COMMENT 'name of metric',
-    `unit` varchar(191) NOT NULL COMMENT 'unit of metric',
-    `lang` varchar(191) NOT NULL DEFAULT '' COMMENT 'language of metric',
-    `note` varchar(4096) NOT NULL COMMENT 'description of metric in Chinese',
-    `expression` varchar(4096) NOT NULL COMMENT 'expression of metric',
-    `created_at` bigint NOT NULL DEFAULT 0 COMMENT 'create time',
-    `created_by` varchar(191) NOT NULL DEFAULT '' COMMENT 'creator',
-    `updated_at` bigint NOT NULL DEFAULT 0 COMMENT 'update time',
-    `updated_by` varchar(191) NOT NULL DEFAULT '' COMMENT 'updater',
+    `collector` varchar(191) NOT NULL COMMENT '''type of collector''',
+    `typ` varchar(191) NOT NULL COMMENT '''type of metric''',
+    `name` varchar(191) NOT NULL COMMENT '''name of metric''',
+    `unit` varchar(191) NOT NULL COMMENT '''unit of metric''',
+    `lang` varchar(191) NOT NULL DEFAULT 'zh' COMMENT '''language''',
+    `note` varchar(4096) NOT NULL COMMENT '''description of metric''',
+    `expression` varchar(4096) NOT NULL COMMENT '''expression of metric''',
+    `created_at` bigint NOT NULL DEFAULT 0 COMMENT '''create time''',
+    `created_by` varchar(191) NOT NULL DEFAULT '' COMMENT '''creator''',
+    `updated_at` bigint NOT NULL DEFAULT 0 COMMENT '''update time''',
+    `updated_by` varchar(191) NOT NULL DEFAULT '' COMMENT '''updater''',
+    `uuid` bigint NOT NULL DEFAULT 0 COMMENT '''uuid''',
     PRIMARY KEY (`id`),
     UNIQUE KEY `idx_collector_typ_name` (`lang`,`collector`, `typ`, `name`),
     INDEX `idx_collector` (`collector`),
     INDEX `idx_typ` (`typ`),
-    INDEX `idx_name` (`name`),
+    INDEX `idx_builtinmetric_name` (`name` ASC),
     INDEX `idx_lang` (`lang`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
 CREATE TABLE `metric_filter` (
   `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'unique identifier',
-  `name` varchar(191) NOT NULL COMMENT 'name of metric filter',
-  `configs` varchar(4096) NOT NULL COMMENT 'configuration of metric filter',
+  `name`  varchar(191) NOT NULL COMMENT '''name of metric filter''',
+  `configs`  varchar(4096) NOT NULL COMMENT '''configuration of metric filter''',
   `groups_perm` text,
-  `create_at` bigint NOT NULL DEFAULT '0' COMMENT 'create time',
-  `create_by` varchar(191) NOT NULL DEFAULT '' COMMENT 'creator',
-  `update_at` bigint NOT NULL DEFAULT '0' COMMENT 'update time',
-  `update_by` varchar(191) NOT NULL DEFAULT '' COMMENT 'updater',
+  `create_at` bigint NOT NULL DEFAULT 0 COMMENT '''create time''',
+  `create_by` varchar(191) NOT NULL DEFAULT '' COMMENT '''creator''',
+  `update_at` bigint NOT NULL DEFAULT 0 COMMENT '''update time''',
+  `update_by` varchar(191) NOT NULL DEFAULT '' COMMENT '''updater''',
   PRIMARY KEY (`id`),
-  KEY `idx_name` (`name`)
+  INDEX `idx_metricfilter_name` (`name` ASC)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
 CREATE TABLE `target_busi_group` (
@@ -2091,4 +2100,4 @@ CREATE TABLE task_host_99
     UNIQUE KEY (`id`, `host`),
     PRIMARY KEY (`ii`)
 ) ENGINE = InnoDB
-  DEFAULT CHARSET = utf8mb4;
+  DEFAULT CHARSET = utf8mb4;
\ No newline at end of file
diff --git a/models/builtin_component.go b/models/builtin_component.go
index 3e5e1a7a4..229bf3b63 100644
--- a/models/builtin_component.go
+++ b/models/builtin_component.go
@@ -11,7 +11,7 @@ import (
 // BuiltinComponent represents a builtin component along with its metadata.
 type BuiltinComponent struct {
 	ID        uint64 `json:"id" gorm:"primaryKey;type:bigint;autoIncrement;comment:'unique identifier'"`
-	Ident     string `json:"ident" gorm:"type:varchar(191);not null;uniqueIndex:idx_ident,sort:asc;comment:'identifier of component'"`
+	Ident     string `json:"ident" gorm:"type:varchar(191);not null;uniqueIndex:idx_ident,sort:asc"`
 	Logo      string `json:"logo" gorm:"type:varchar(191);not null;comment:'logo of component'"`
 	Readme    string `json:"readme" gorm:"type:text;not null;comment:'readme of component'"`
 	CreatedAt int64  `json:"created_at" gorm:"type:bigint;not null;default:0;comment:'create time'"`
diff --git a/models/builtin_metrics.go b/models/builtin_metrics.go
index 38715a7a2..751b11a5d 100644
--- a/models/builtin_metrics.go
+++ b/models/builtin_metrics.go
@@ -14,12 +14,12 @@ import (
 type BuiltinMetric struct {
 	ID         int64  `json:"id" gorm:"primaryKey;type:bigint;autoIncrement;comment:'unique identifier'"`
 	UUID       int64  `json:"uuid" gorm:"type:bigint;not null;default:0;comment:'uuid'"`
-	Collector  string `json:"collector" gorm:"type:varchar(191);not null;index:idx_collector,sort:asc;comment:'type of collector'"` // Type of collector (e.g., 'categraf', 'telegraf')
-	Typ        string `json:"typ" gorm:"type:varchar(191);not null;index:idx_typ,sort:asc;comment:'type of metric'"`                // Type of metric (e.g., 'host', 'mysql', 'redis')
-	Name       string `json:"name" gorm:"type:varchar(191);not null;index:idx_builtinmetric_name,sort:asc;comment:'name of metric'"`
+	Collector  string `json:"collector" gorm:"uniqueIndex:idx_collector_typ_name;type:varchar(191);not null;index:idx_collector,sort:asc;comment:'type of collector'"`
+	Typ        string `json:"typ" gorm:"uniqueIndex:idx_collector_typ_name;type:varchar(191);not null;index:idx_typ,sort:asc;comment:'type of metric'"`
+	Name       string `json:"name" gorm:"uniqueIndex:idx_collector_typ_name;type:varchar(191);not null;index:idx_builtinmetric_name,sort:asc;comment:'name of metric'"`
 	Unit       string `json:"unit" gorm:"type:varchar(191);not null;comment:'unit of metric'"`
 	Note       string `json:"note" gorm:"type:varchar(4096);not null;comment:'description of metric'"`
-	Lang       string `json:"lang" gorm:"type:varchar(191);not null;default:'zh';index:idx_lang,sort:asc;comment:'language'"`
+	Lang       string `json:"lang" gorm:"uniqueIndex:idx_collector_typ_name;type:varchar(191);not null;default:'zh';index:idx_lang,sort:asc;comment:'language'"`
 	Expression string `json:"expression" gorm:"type:varchar(4096);not null;comment:'expression of metric'"`
 	CreatedAt  int64  `json:"created_at" gorm:"type:bigint;not null;default:0;comment:'create time'"`
 	CreatedBy  string `json:"created_by" gorm:"type:varchar(191);not null;default:'';comment:'creator'"`
diff --git a/models/migrate/migrate.go b/models/migrate/migrate.go
index d7243342a..d849d2844 100644
--- a/models/migrate/migrate.go
+++ b/models/migrate/migrate.go
@@ -186,16 +186,17 @@ func InsertPermPoints(db *gorm.DB) {
 }
 
 type AlertRule struct {
-	ExtraConfig       string                   `gorm:"type:text;column:extra_config"`                                          // extra config
-	DatasourceQueries []models.DatasourceQuery `json:"datasource_queries" gorm:"datasource_queries;type:text;serializer:json"` // datasource queries
+	ExtraConfig       string                   `gorm:"type:text;column:extra_config;not null;comment:extra_config"` // extra config
+	DatasourceQueries []models.DatasourceQuery `gorm:"datasource_queries;type:text;serializer:json"`                // datasource queries
 }
 
 type AlertSubscribe struct {
-	ExtraConfig string       `gorm:"type:text;column:extra_config"` // extra config
+	ExtraConfig string       `gorm:"type:text;column:extra_config;not null;comment:extra_config"` // extra config
 	Severities  string       `gorm:"column:severities;type:varchar(32);not null;default:''"`
-	BusiGroups  ormx.JSONArr `gorm:"column:busi_groups;type:varchar(4096);not null;default:'[]'"`
+	RuleID      int64        `gorm:"type:bigint;not null;default:0"`
+	RuleIds     []int64      `gorm:"column:rule_ids;type:varchar(1024);"`
+	BusiGroups  ormx.JSONArr `gorm:"column:busi_groups;type:varchar(4096);not null;"`
 	Note        string       `gorm:"column:note;type:varchar(1024);default:'';comment:note"`
-	RuleIds     []int64      `gorm:"column:rule_ids;type:varchar(1024);default:'';comment:rule_ids"`
 }
 
 type AlertMute struct {
@@ -245,8 +246,8 @@ type Configs struct {
 	Note string `gorm:"column:note;type:varchar(1024);default:'';comment:note"`
 	Cval string `gorm:"column:cval;type:text;comment:config value"`
 	//mysql tinyint//postgresql smallint
-	External  int    `gorm:"column:external;type:int;default:0;comment:0\\:built-in 1\\:external"`
-	Encrypted int    `gorm:"column:encrypted;type:int;default:0;comment:0\\:plaintext 1\\:ciphertext"`
+	External  int64  `gorm:"column:external;type:bigint;default:0;comment:0 means built-in 1 means external"`
+	Encrypted int    `gorm:"column:encrypted;type:int;default:0;comment:0 means plaintext 1 means ciphertext"`
 	CreateAt  int64  `gorm:"column:create_at;type:int;default:0;comment:create_at"`
 	CreateBy  string `gorm:"column:create_by;type:varchar(64);default:'';comment:cerate_by"`
 	UpdateAt  int64  `gorm:"column:update_at;type:int;default:0;comment:update_at"`