chore: Make SQL queries in more readable

This commit is contained in:
Cristian Ditaputratama 2024-05-20 03:08:48 +07:00
parent d99f8583c1
commit fae32d9a74
Signed by: ditatompel
GPG key ID: 31D3D06D77950979
2 changed files with 135 additions and 84 deletions

View file

@ -44,9 +44,10 @@ func MigrateDb(db *DB) error {
} }
func getSchemaVersion(db *DB) int { func getSchemaVersion(db *DB) int {
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS tbl_schema_ver ( _, err := db.Exec(`
version INT(5) UNSIGNED NOT NULL CREATE TABLE IF NOT EXISTS tbl_schema_ver (
)`) version INT(5) UNSIGNED NOT NULL
)`)
if err != nil { if err != nil {
return -1 return -1
} }
@ -68,14 +69,15 @@ func v1(db *DB) error {
slog.Debug("[DB] Migrating database schema version 1") slog.Debug("[DB] Migrating database schema version 1")
// table: tbl_admin // table: tbl_admin
slog.Debug("[DB] Creating table: tbl_admin") slog.Debug("[DB] Creating table: tbl_admin")
_, err := db.Exec(`CREATE TABLE tbl_admin ( _, err := db.Exec(`
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, CREATE TABLE tbl_admin (
username VARCHAR(255) NOT NULL, id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
password VARCHAR(255) NOT NULL, username VARCHAR(255) NOT NULL,
lastactive_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, password VARCHAR(255) NOT NULL,
created_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, lastactive_ts INT(11) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id) created_ts INT(11) UNSIGNED NOT NULL DEFAULT 0,
)`) PRIMARY KEY (id)
)`)
if err != nil { if err != nil {
return err return err
} }
@ -87,73 +89,86 @@ func v1(db *DB) error {
// table: tbl_cron // table: tbl_cron
slog.Debug("[DB] Creating table: tbl_cron") slog.Debug("[DB] Creating table: tbl_cron")
_, err = db.Exec(`CREATE TABLE tbl_cron ( _, err = db.Exec(`
id INT(8) UNSIGNED NOT NULL AUTO_INCREMENT, CREATE TABLE tbl_cron (
title VARCHAR(255) NOT NULL DEFAULT '', id INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
slug VARCHAR(255) NOT NULL DEFAULT '', title VARCHAR(255) NOT NULL DEFAULT '',
description VARCHAR(255) NOT NULL DEFAULT '', slug VARCHAR(255) NOT NULL DEFAULT '',
run_every INT(8) UNSIGNED NOT NULL DEFAULT 60 COMMENT 'in seconds', description VARCHAR(255) NOT NULL DEFAULT '',
last_run INT(11) UNSIGNED NOT NULL DEFAULT 0, run_every INT(8) UNSIGNED NOT NULL DEFAULT 60 COMMENT 'in seconds',
next_run INT(11) UNSIGNED NOT NULL DEFAULT 0, last_run INT(11) UNSIGNED NOT NULL DEFAULT 0,
run_time FLOAT(7,3) UNSIGNED NOT NULL DEFAULT 0.000, next_run INT(11) UNSIGNED NOT NULL DEFAULT 0,
cron_state TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, run_time FLOAT(7,3) UNSIGNED NOT NULL DEFAULT 0.000,
is_enabled TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, cron_state TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id) is_enabled TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,
)`) PRIMARY KEY (id)
)`)
if err != nil { if err != nil {
return err return err
} }
slog.Debug("[DB] Adding default cron jobs to table: tbl_cron") slog.Debug("[DB] Adding default cron jobs to table: tbl_cron")
_, err = db.Exec(`INSERT INTO tbl_cron (title, slug, description, run_every) _, err = db.Exec(`
VALUES ('Delete old probe logs', 'delete_old_probe_logs', 'Delete old probe log from the database',120);`) INSERT INTO tbl_cron (
title,
slug,
description,
run_every
) VALUES (
'Delete old probe logs',
'delete_old_probe_logs',
'Delete old probe log from the database',
120
);`)
if err != nil { if err != nil {
return err return err
} }
// table: tbl_node // table: tbl_node
slog.Debug("[DB] Creating table: tbl_node") slog.Debug("[DB] Creating table: tbl_node")
_, err = db.Exec(`CREATE TABLE tbl_node ( _, err = db.Exec(`
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, CREATE TABLE tbl_node (
protocol VARCHAR(6) NOT NULL DEFAULT 'http' COMMENT 'http | https', id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
hostname VARCHAR(255) NOT NULL, protocol VARCHAR(6) NOT NULL DEFAULT 'http' COMMENT 'http | https',
port INT(6) UNSIGNED NOT NULL DEFAULT 0, hostname VARCHAR(255) NOT NULL,
is_tor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, port INT(6) UNSIGNED NOT NULL DEFAULT 0,
is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, is_tor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
nettype VARCHAR(100) NOT NULL COMMENT 'mainnet | stagenet | testnet', is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, nettype VARCHAR(100) NOT NULL COMMENT 'mainnet | stagenet | testnet',
adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
version VARCHAR(200) NOT NULL DEFAULT '', difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
uptime float(5,2) UNSIGNED NOT NULL DEFAULT 0.00, version VARCHAR(200) NOT NULL DEFAULT '',
estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0, uptime float(5,2) UNSIGNED NOT NULL DEFAULT 0.00,
ip_addr VARCHAR(200) NOT NULL, estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0,
asn INT(9) UNSIGNED NOT NULL DEFAULT 0, ip_addr VARCHAR(200) NOT NULL,
asn_name VARCHAR(255) NOT NULL DEFAULT '', asn INT(9) UNSIGNED NOT NULL DEFAULT 0,
country VARCHAR(100) NOT NULL DEFAULT '', asn_name VARCHAR(255) NOT NULL DEFAULT '',
country_name VARCHAR(255) NOT NULL DEFAULT '', country VARCHAR(100) NOT NULL DEFAULT '',
city VARCHAR(255) NOT NULL DEFAULT '', country_name VARCHAR(255) NOT NULL DEFAULT '',
lat FLOAT NOT NULL DEFAULT 0 COMMENT 'latitude', city VARCHAR(255) NOT NULL DEFAULT '',
lon FLOAT NOT NULL DEFAULT 0 COMMENT 'longitude', lat FLOAT NOT NULL DEFAULT 0 COMMENT 'latitude',
date_entered INT(11) UNSIGNED NOT NULL DEFAULT 0, lon FLOAT NOT NULL DEFAULT 0 COMMENT 'longitude',
last_checked INT(11) UNSIGNED NOT NULL DEFAULT 0, date_entered INT(11) UNSIGNED NOT NULL DEFAULT 0,
last_check_status TEXT DEFAULT NULL, last_checked INT(11) UNSIGNED NOT NULL DEFAULT 0,
cors_capable TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, last_check_status TEXT DEFAULT NULL,
PRIMARY KEY (id) cors_capable TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
)`) PRIMARY KEY (id)
)`)
if err != nil { if err != nil {
return err return err
} }
// table: tbl_prober // table: tbl_prober
slog.Debug("[DB] Creating table: tbl_prober") slog.Debug("[DB] Creating table: tbl_prober")
_, err = db.Exec(`CREATE TABLE tbl_prober ( _, err = db.Exec(`
id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT, CREATE TABLE tbl_prober (
name VARCHAR(255) NOT NULL, id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
api_key VARCHAR(36) NOT NULL, name VARCHAR(255) NOT NULL,
last_submit_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, api_key VARCHAR(36) NOT NULL,
PRIMARY KEY (id) last_submit_ts INT(11) UNSIGNED NOT NULL DEFAULT 0,
)`) PRIMARY KEY (id)
)`)
if err != nil { if err != nil {
return err return err
} }
@ -166,21 +181,22 @@ func v1(db *DB) error {
// table: tbl_probe_log // table: tbl_probe_log
slog.Debug("[DB] Creating table: tbl_probe_log") slog.Debug("[DB] Creating table: tbl_probe_log")
_, err = db.Exec(`CREATE TABLE tbl_probe_log ( _, err = db.Exec(`
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, CREATE TABLE tbl_probe_log (
node_id INT(11) UNSIGNED NOT NULL DEFAULT 0, id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
prober_id INT(9) UNSIGNED NOT NULL DEFAULT 0, node_id INT(11) UNSIGNED NOT NULL DEFAULT 0,
is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, prober_id INT(9) UNSIGNED NOT NULL DEFAULT 0,
height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0, difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
date_checked INT(11) UNSIGNED NOT NULL DEFAULT 0, estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0,
failed_reason TEXT NOT NULL DEFAULT '', date_checked INT(11) UNSIGNED NOT NULL DEFAULT 0,
fetch_runtime FLOAT(5,2) UNSIGNED NOT NULL DEFAULT 0.00, failed_reason TEXT NOT NULL DEFAULT '',
PRIMARY KEY (id) fetch_runtime FLOAT(5,2) UNSIGNED NOT NULL DEFAULT 0.00,
)`) PRIMARY KEY (id)
)`)
if err != nil { if err != nil {
return err return err
} }

View file

@ -69,24 +69,52 @@ func (repo *CronRepo) RunCronProcess() {
} }
func (repo *CronRepo) Crons() ([]Cron, error) { func (repo *CronRepo) Crons() ([]Cron, error) {
query := `SELECT id, title, slug, description, run_every, last_run, next_run, run_time, cron_state, is_enabled FROM tbl_cron`
var tasks []Cron var tasks []Cron
err := repo.db.Select(&tasks, query) err := repo.db.Select(&tasks, `
SELECT
id,
title,
slug,
description,
run_every,
last_run,
next_run,
run_time,
cron_state,
is_enabled
FROM
tbl_cron`)
return tasks, err return tasks, err
} }
func (repo *CronRepo) queueList() ([]Cron, error) { func (repo *CronRepo) queueList() ([]Cron, error) {
tasks := []Cron{} tasks := []Cron{}
query := `SELECT id, run_every, last_run, slug, next_run, cron_state FROM tbl_cron query := `
WHERE is_enabled = ? AND next_run <= ?` SELECT
id,
run_every,
last_run,
slug,
next_run,
cron_state
FROM
tbl_cron
WHERE
is_enabled = ?
AND next_run <= ?`
err := repo.db.Select(&tasks, query, 1, time.Now().Unix()) err := repo.db.Select(&tasks, query, 1, time.Now().Unix())
return tasks, err return tasks, err
} }
func (repo *CronRepo) preRunTask(id int, lastRunTs int64) { func (repo *CronRepo) preRunTask(id int, lastRunTs int64) {
query := `UPDATE tbl_cron SET cron_state = ?, last_run = ? WHERE id = ?` query := `
UPDATE tbl_cron
SET
cron_state = ?,
last_run = ?
WHERE
id = ?`
row, err := repo.db.Query(query, 1, lastRunTs, id) row, err := repo.db.Query(query, 1, lastRunTs, id)
if err != nil { if err != nil {
slog.Error(fmt.Sprintf("[CRON] Failed to update pre cron state: %s", err)) slog.Error(fmt.Sprintf("[CRON] Failed to update pre cron state: %s", err))
@ -95,7 +123,14 @@ func (repo *CronRepo) preRunTask(id int, lastRunTs int64) {
} }
func (repo *CronRepo) postRunTask(id int, nextRun int64, runtime float64) { func (repo *CronRepo) postRunTask(id int, nextRun int64, runtime float64) {
query := `UPDATE tbl_cron SET cron_state = ?, next_run = ?, run_time = ? WHERE id = ?` query := `
UPDATE tbl_cron
SET
cron_state = ?,
next_run = ?,
run_time = ?
WHERE
id = ?`
row, err := repo.db.Query(query, 0, nextRun, runtime, id) row, err := repo.db.Query(query, 0, nextRun, runtime, id)
if err != nil { if err != nil {
slog.Error(fmt.Sprintf("[CRON] Failed to update post cron state: %s", err)) slog.Error(fmt.Sprintf("[CRON] Failed to update post cron state: %s", err))