models.go 2.9 KB
Newer Older
X
Xin.Zh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
package models

import (
	"fmt"
	"strconv"
	"time"

	"github.com/jmoiron/sqlx"
	"github.com/taosdata/alert/utils"
	"github.com/taosdata/alert/utils/log"
)

var db *sqlx.DB

func Init() error {
	xdb, e := sqlx.Connect("sqlite3", utils.Cfg.Database)
	if e == nil {
		db = xdb
	}
	return upgrade()
}

func Uninit() error {
	db.Close()
	return nil
}

func getStringOption(tx *sqlx.Tx, name string) (string, error) {
	const qs = "SELECT * FROM `option` WHERE `name`=?"

	var (
		e error
		o struct {
			Name  string `db:"name"`
			Value string `db:"value"`
		}
	)

	if tx != nil {
		e = tx.Get(&o, qs, name)
	} else {
		e = db.Get(&o, qs, name)
	}

	if e != nil {
		return "", e
	}

	return o.Value, nil
}

func getIntOption(tx *sqlx.Tx, name string) (int, error) {
	s, e := getStringOption(tx, name)
	if e != nil {
		return 0, e
	}
	v, e := strconv.ParseInt(s, 10, 64)
	return int(v), e
}

func setOption(tx *sqlx.Tx, name string, value interface{}) error {
	const qs = "REPLACE INTO `option`(`name`, `value`) VALUES(?, ?);"

	var (
		e  error
		sv string
	)

	switch v := value.(type) {
	case time.Time:
		sv = v.Format(time.RFC3339)
	default:
		sv = fmt.Sprint(value)
	}

	if tx != nil {
		_, e = tx.Exec(qs, name, sv)
	} else {
		_, e = db.Exec(qs, name, sv)
	}

	return e
}

var upgradeScripts = []struct {
	ver   int
	stmts []string
}{
	{
		ver: 0,
		stmts: []string{
			"CREATE TABLE `option`( `name` VARCHAR(63) PRIMARY KEY, `value` VARCHAR(255) NOT NULL) WITHOUT ROWID;",
			"CREATE TABLE `rule`( `name` VARCHAR(63) PRIMARY KEY, `enabled` TINYINT(1) NOT NULL, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, `content` TEXT(65535) NOT NULL);",
		},
	},
}

func upgrade() error {
	const dbVersion = "database version"

	ver, e := getIntOption(nil, dbVersion)
	if e != nil { // regards all errors as schema not created
		ver = -1 // set ver to -1 to execute all statements
	}

	tx, e := db.Beginx()
	if e != nil {
		return e
	}

	for _, us := range upgradeScripts {
		if us.ver <= ver {
			continue
		}
		log.Info("upgrading database to version: ", us.ver)
		for _, s := range us.stmts {
			if _, e = tx.Exec(s); e != nil {
				tx.Rollback()
				return e
			}
		}
		ver = us.ver
	}

	if e = setOption(tx, dbVersion, ver); e != nil {
		tx.Rollback()
		return e
	}

	return tx.Commit()
}