mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-19 08:26:23 -07:00
Exit without errors (but with warning message) if database is empty, and with appropriate message if database_version exists but doesn't have gochan db version
This commit is contained in:
parent
f370c4bd37
commit
61693f4a4a
5 changed files with 37 additions and 6 deletions
2
.github/workflows/update-docs.yml
vendored
2
.github/workflows/update-docs.yml
vendored
|
@ -37,7 +37,7 @@ jobs:
|
|||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.22'
|
||||
go-version: '1.24.1'
|
||||
|
||||
- name: Get gochan-cfgdoc
|
||||
if: env.update_docs == 'true'
|
||||
|
|
|
@ -16,7 +16,8 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
CommentRemover = regexp.MustCompile("--.*\n?")
|
||||
CommentRemover = regexp.MustCompile("--.*\n?")
|
||||
ErrNotInstalled = errors.New("database is empty, run gochan to install and initialize it")
|
||||
)
|
||||
|
||||
// ColumnType returns a string representation of the column's data type. It does not return an error
|
||||
|
|
|
@ -2,6 +2,8 @@ package gcupdate
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -55,6 +57,27 @@ func (dbu *GCDatabaseUpdater) MigrateDB() (migrated bool, err error) {
|
|||
errEv := common.LogError()
|
||||
|
||||
gcsql.SetDB(dbu.db)
|
||||
|
||||
sqlConfig := config.GetSQLConfig()
|
||||
var tableCountQuery string
|
||||
var tableCount int
|
||||
switch sqlConfig.DBtype {
|
||||
case "mysql":
|
||||
tableCountQuery = `SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE ?`
|
||||
case "postgres", "postgresql":
|
||||
tableCountQuery = `SELECT COUNT(*) FROM information_schema.TABLES WHERE table_catalog = CURRENT_DATABASE() AND table_name LIKE ?`
|
||||
case "sqlite3":
|
||||
tableCountQuery = `SELECT COUNT(*) FROM sqlite_master WHERE name LIKE ? AND type = 'table'`
|
||||
default:
|
||||
return false, gcsql.ErrUnsupportedDB
|
||||
}
|
||||
if err = dbu.db.QueryRow(nil, tableCountQuery, []any{sqlConfig.DBprefix + "%"}, []any{&tableCount}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if tableCount == 0 {
|
||||
return false, common.ErrNotInstalled
|
||||
}
|
||||
|
||||
migrated, err = dbu.IsMigrated()
|
||||
defer func() {
|
||||
if a := recover(); a != nil {
|
||||
|
@ -65,6 +88,9 @@ func (dbu *GCDatabaseUpdater) MigrateDB() (migrated bool, err error) {
|
|||
errEv.Discard()
|
||||
}
|
||||
}()
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return migrated, gcsql.ErrInvalidVersion
|
||||
}
|
||||
if err != nil {
|
||||
return migrated, err
|
||||
}
|
||||
|
@ -72,7 +98,6 @@ func (dbu *GCDatabaseUpdater) MigrateDB() (migrated bool, err error) {
|
|||
return migrated, nil
|
||||
}
|
||||
|
||||
sqlConfig := config.GetSQLConfig()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -121,8 +122,12 @@ func main() {
|
|||
}
|
||||
|
||||
var migrated bool
|
||||
if migrated, err = migrator.MigrateDB(); err != nil {
|
||||
fatalEv.Msg("Unable to migrate database")
|
||||
migrated, err = migrator.MigrateDB()
|
||||
if errors.Is(err, common.ErrNotInstalled) {
|
||||
common.LogWarning().Msg(err.Error())
|
||||
return
|
||||
} else if err != nil {
|
||||
fatalEv.Err(err).Msg("Unable to migrate database")
|
||||
}
|
||||
if migrated {
|
||||
common.LogWarning().Msg("Database is already migrated")
|
||||
|
|
|
@ -107,4 +107,4 @@ LEFT JOIN DBPREFIXboards b ON b.id = t.board_id;
|
|||
CREATE VIEW DBPREFIXv_post_reports AS
|
||||
SELECT r.id, handled_by_staff_id AS staff_id, username AS staff_user, post_id, IP_NTOA as ip, reason, is_cleared
|
||||
FROM DBPREFIXreports r LEFT JOIN DBPREFIXstaff s ON handled_by_staff_id = s.id
|
||||
WHERE is_cleared = 0;
|
||||
WHERE is_cleared = FALSE;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue