create migration script for old data
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
)
|
||||
|
||||
type DatabaseImpl interface {
|
||||
DB() sq.DB
|
||||
|
||||
Migrate(ctx context.Context) error
|
||||
Ping(ctx context.Context) error
|
||||
BeginTx(ctx context.Context) (sq.Tx, error)
|
||||
|
@@ -81,7 +81,7 @@ func (l DBLogger) PostExec(txID *uint16, sqlOriginal string, sqlReal string, par
|
||||
}
|
||||
|
||||
func fmtSQLPrint(sql string) string {
|
||||
if strings.Contains(sql, ";") {
|
||||
if strings.Contains(sql, ";") && len(sql) > 1024 {
|
||||
return "(...multi...)"
|
||||
}
|
||||
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/rext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -37,7 +38,7 @@ type DBPreprocessor struct {
|
||||
cacheQuery map[string]string
|
||||
}
|
||||
|
||||
var regexAlias = regexp.MustCompile("([A-Za-z_\\-0-9]+)\\s+AS\\s+([A-Za-z_\\-0-9]+)")
|
||||
var regexAlias = rext.W(regexp.MustCompile("([A-Za-z_\\-0-9]+)\\s+AS\\s+([A-Za-z_\\-0-9]+)"))
|
||||
|
||||
func NewDBPreprocessor(db sq.DB) (*DBPreprocessor, error) {
|
||||
|
||||
@@ -146,8 +147,8 @@ func (pp *DBPreprocessor) PreQuery(ctx context.Context, txID *uint16, sql *strin
|
||||
newsel := make([]string, 0)
|
||||
|
||||
aliasMap := make(map[string]string)
|
||||
for _, v := range regexAlias.FindAllStringSubmatch(sqlOriginal, idxFrom+len(" FROM")) {
|
||||
aliasMap[strings.TrimSpace(v[2])] = strings.TrimSpace(v[1])
|
||||
for _, v := range regexAlias.MatchAll(sqlOriginal) {
|
||||
aliasMap[strings.TrimSpace(v.GroupByIndex(1).Value())] = strings.TrimSpace(v.GroupByIndex(2).Value())
|
||||
}
|
||||
|
||||
for _, expr := range split {
|
||||
|
@@ -42,7 +42,9 @@ func NewLogsDatabase(cfg server.Config) (*Database, error) {
|
||||
|
||||
qqdb := sq.NewDB(xdb)
|
||||
|
||||
qqdb.AddListener(dbtools.DBLogger{})
|
||||
if conf.EnableLogger {
|
||||
qqdb.AddListener(dbtools.DBLogger{})
|
||||
}
|
||||
|
||||
pp, err := dbtools.NewDBPreprocessor(qqdb)
|
||||
if err != nil {
|
||||
@@ -56,6 +58,10 @@ func NewLogsDatabase(cfg server.Config) (*Database, error) {
|
||||
return scndb, nil
|
||||
}
|
||||
|
||||
func (db *Database) DB() sq.DB {
|
||||
return db.db
|
||||
}
|
||||
|
||||
func (db *Database) Migrate(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 24*time.Second)
|
||||
defer cancel()
|
||||
|
@@ -91,11 +91,12 @@ func (db *Database) CreateChannel(ctx TxContext, userid models.UserID, dispName
|
||||
|
||||
channelid := models.NewChannelID()
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, subscribe_key, send_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :subkey, :sendkey, :ts)", sq.PP{
|
||||
_, err = tx.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, send_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :sendkey, :ts)", sq.PP{
|
||||
"cid": channelid,
|
||||
"ouid": userid,
|
||||
"dnam": dispName,
|
||||
"inam": intName,
|
||||
"hnam": nil,
|
||||
"subkey": subscribeKey,
|
||||
"sendkey": sendKey,
|
||||
"ts": time2DB(now),
|
||||
|
@@ -42,7 +42,9 @@ func NewPrimaryDatabase(cfg server.Config) (*Database, error) {
|
||||
|
||||
qqdb := sq.NewDB(xdb)
|
||||
|
||||
qqdb.AddListener(dbtools.DBLogger{})
|
||||
if conf.EnableLogger {
|
||||
qqdb.AddListener(dbtools.DBLogger{})
|
||||
}
|
||||
|
||||
pp, err := dbtools.NewDBPreprocessor(qqdb)
|
||||
if err != nil {
|
||||
@@ -56,6 +58,10 @@ func NewPrimaryDatabase(cfg server.Config) (*Database, error) {
|
||||
return scndb, nil
|
||||
}
|
||||
|
||||
func (db *Database) DB() sq.DB {
|
||||
return db.db
|
||||
}
|
||||
|
||||
func (db *Database) Migrate(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 24*time.Second)
|
||||
defer cancel()
|
||||
|
@@ -42,7 +42,9 @@ func NewRequestsDatabase(cfg server.Config) (*Database, error) {
|
||||
|
||||
qqdb := sq.NewDB(xdb)
|
||||
|
||||
qqdb.AddListener(dbtools.DBLogger{})
|
||||
if conf.EnableLogger {
|
||||
qqdb.AddListener(dbtools.DBLogger{})
|
||||
}
|
||||
|
||||
pp, err := dbtools.NewDBPreprocessor(qqdb)
|
||||
if err != nil {
|
||||
@@ -56,6 +58,10 @@ func NewRequestsDatabase(cfg server.Config) (*Database, error) {
|
||||
return scndb, nil
|
||||
}
|
||||
|
||||
func (db *Database) DB() sq.DB {
|
||||
return db.db
|
||||
}
|
||||
|
||||
func (db *Database) Migrate(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 24*time.Second)
|
||||
defer cancel()
|
||||
|
Reference in New Issue
Block a user