2021-06-24 16:41:26 -07:00
|
|
|
// used for migrating pre-refactor gochan databases to the new schema
|
|
|
|
package pre2021
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
|
2021-07-11 11:51:29 -07:00
|
|
|
"github.com/gochan-org/gochan/pkg/config"
|
2021-06-24 16:41:26 -07:00
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
)
|
|
|
|
|
2021-07-11 11:51:29 -07:00
|
|
|
const (
|
|
|
|
// check to see if the old db exists, if the new db exists, and the number of tables
|
|
|
|
// in the new db
|
|
|
|
dbInfoSQL = `SELECT
|
|
|
|
(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?) AS olddb,
|
|
|
|
(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?) as newdb,
|
|
|
|
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?) as num_tables`
|
|
|
|
)
|
|
|
|
|
2021-06-24 16:41:26 -07:00
|
|
|
type Pre2021Migrator struct {
|
|
|
|
db *gcsql.GCDB
|
|
|
|
options common.DBOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Pre2021Migrator) Init(options common.DBOptions) error {
|
|
|
|
m.options = options
|
|
|
|
var err error
|
|
|
|
m.db, err = gcsql.Open(
|
2021-07-11 11:51:29 -07:00
|
|
|
m.options.Host, m.options.DBType, "", m.options.Username,
|
|
|
|
m.options.Password, options.TablePrefix)
|
2021-06-24 16:41:26 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Pre2021Migrator) MigrateDB() error {
|
2021-07-11 11:51:29 -07:00
|
|
|
chkDbStmt, err := m.db.PrepareSQL(dbInfoSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer chkDbStmt.Close()
|
|
|
|
var olddb []byte
|
|
|
|
var newdb []byte
|
|
|
|
var numTables int
|
|
|
|
|
|
|
|
if err = chkDbStmt.QueryRow(m.options.OldDBName, m.options.NewDBName, m.options.NewDBName).Scan(&olddb, &newdb, &numTables); err != nil {
|
|
|
|
return common.NewMigrationError("pre2021", err.Error())
|
|
|
|
}
|
|
|
|
if olddb == nil {
|
|
|
|
return common.NewMigrationError("pre2021", "old database doesn't exist")
|
|
|
|
}
|
|
|
|
if newdb == nil {
|
|
|
|
return common.NewMigrationError("pre2021", "new database doesn't exist")
|
|
|
|
}
|
|
|
|
if numTables > 0 {
|
|
|
|
return common.NewMigrationError("pre2021", "new database must be empty")
|
|
|
|
}
|
|
|
|
gcsql.ConnectToDB(
|
|
|
|
m.options.Host, m.options.DBType, m.options.NewDBName,
|
|
|
|
m.options.Username, m.options.Password, m.options.TablePrefix)
|
|
|
|
cfg := config.GetSystemCriticalConfig()
|
|
|
|
gcsql.CheckAndInitializeDatabase(cfg.DBtype)
|
|
|
|
|
2021-08-15 22:19:51 -07:00
|
|
|
GetPosts(m.db)
|
|
|
|
|
2021-06-24 16:41:26 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Pre2021Migrator) Close() error {
|
|
|
|
if m.db != nil {
|
|
|
|
return m.db.Close()
|
|
|
|
}
|
2021-07-11 11:51:29 -07:00
|
|
|
return gcsql.Close()
|
2021-06-24 16:41:26 -07:00
|
|
|
}
|