1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-04 10:06:24 -07:00

Add function to check if Pre2021 migration is already complete

(still need to do kusaba x and tinyboard)
This commit is contained in:
Eggbertx 2022-02-19 14:55:45 -08:00
parent 69fb7c9d31
commit 8840693a8b
6 changed files with 101 additions and 33 deletions

View file

@ -54,8 +54,14 @@ type DBMigrator interface {
// Init sets the variables for connecting to the databases
Init(options MigrationOptions) error
// IsMigrated checks to see if the database has already been migrated and quits if it has
// and returns any errors that aren't "table doesn't exist". if the boolean value is true,
// it can be assumed that the database has already been migrated and gochan-migration
// will exit
IsMigrated() (bool, error)
// MigrateDB migrates the imageboard data (posts, boards, etc) to the new database
MigrateDB() error
MigrateDB() (bool, error)
// MigrateBoards gets info about the old boards in the board table and inserts each one
// into the new database if they don't already exist

View file

@ -6,6 +6,12 @@ import (
)
func (m *Pre2021Migrator) MigrateBoards() error {
if m.oldBoards == nil {
m.oldBoards = map[int]string{}
}
if m.newBoards == nil {
m.newBoards = map[int]string{}
}
// get all boards from new db
boards, err := gcsql.GetAllBoards()
if err != nil {
@ -63,7 +69,9 @@ func (m *Pre2021Migrator) MigrateBoards() error {
var redirect_to_thread bool
var require_file bool
var enable_catalog bool
if err = rows.Scan(&dir,
if err = rows.Scan(
&id,
&dir,
&board_type,
&upload_type,
&title,

View file

@ -1,9 +1,11 @@
package pre2021
import (
"fmt"
"time"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gcsql"
)
type postTable struct {
@ -75,7 +77,7 @@ func (m *Pre2021Migrator) migrateThreads() error {
bumped,
stickied,
locked,
reviewed from DBPREFIXposts WHERE is_deleted = 0`)
reviewed from DBPREFIXposts WHERE deleted_timestamp = NULL`)
if err != nil {
return err
}
@ -111,7 +113,7 @@ func (m *Pre2021Migrator) migrateThreads() error {
&post.locked,
&post.reviewed,
); err != nil {
return err
// return err
}
_, ok := m.oldBoards[post.boardid]
if !ok {
@ -120,12 +122,40 @@ func (m *Pre2021Migrator) migrateThreads() error {
continue
}
// var stmt *sql.Stmt
// var err error
// gcsql.QueryRowSQL(`SELECT id FROM DBPREFIXboards WHERE uri = ?`, []interface{}{})
m.posts = append(m.posts, post)
if post.parentid == 0 {
// post is a thread, save it to the DBPREFIXthreads table
// []interfaceP{{post.newParentID}
if err = gcsql.QueryRowSQL(
`SELECT board_id FROM DBPREFIXthreads ORDER BY board_id LIMIT 1`,
nil,
[]interface{}{&post.newParentID},
); err != nil {
return err
}
fmt.Println("Current board ID:", post.newParentID)
// // stmt, err := db.Prepare("INSERT table SET unique_id=? ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)")
// gcsql.ExecSQL(`INSERT INTO DBPREFIXthreads (board_id) VALUES(?)`, post.newBoardID)
// /*
// id
// board_id
// locked
// stickied
// anchored
// cyclical
// last_bump
// deleted_at
// is_deleted
// */
}
m.posts = append(m.posts, post)
}
return nil
}

View file

@ -9,15 +9,6 @@ import (
"github.com/gochan-org/gochan/pkg/gcsql"
)
const (
// check to see if the old db exists, if the new db exists, and the number of tables
// in the new db
mysqlDbInfoSQL = `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`
)
type Pre2021Config struct {
DBtype string
DBhost string
@ -58,25 +49,52 @@ func (m *Pre2021Migrator) Init(options common.MigrationOptions) error {
return err
}
func (m *Pre2021Migrator) MigrateDB() error {
func (m *Pre2021Migrator) IsMigrated() (bool, error) {
var migrated bool
var err error
if err := m.MigrateBoards(); err != nil {
return err
var query string
switch m.config.DBtype {
case "mysql":
fallthrough
case "postgres":
query = `SELECT COUNT(*) > 0 FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?`
}
if err = m.MigratePosts(); err != nil {
return err
if err = m.db.QueryRowSQL(query,
[]interface{}{m.config.DBprefix + "migrated", m.config.DBname},
[]interface{}{&migrated}); err != nil {
return migrated, err
}
if err = m.MigrateStaff("password"); err != nil {
return err
return migrated, err
}
func (m *Pre2021Migrator) MigrateDB() (bool, error) {
migrated, err := m.IsMigrated()
if err != nil {
return false, err
}
if err = m.MigrateBans(); err != nil {
return err
}
if err = m.MigrateAnnouncements(); err != nil {
return err
if migrated {
// db is already migrated, stop
return true, nil
}
return nil
if err := m.MigrateBoards(); err != nil {
return false, err
}
if err = m.MigratePosts(); err != nil {
return false, err
}
if err = m.MigrateStaff("password"); err != nil {
return false, err
}
if err = m.MigrateBans(); err != nil {
return false, err
}
if err = m.MigrateAnnouncements(); err != nil {
return false, err
}
return true, nil
}
func (m *Pre2021Migrator) MigrateStaff(password string) error {

View file

@ -92,9 +92,15 @@ func main() {
return
}
defer migrator.Close()
if err = migrator.MigrateDB(); err != nil {
var migrated bool
if migrated, err = migrator.MigrateDB(); err != nil {
gclog.Printf(fatalLogFlags, "Error migrating database: ", err.Error())
return
}
if migrated {
gclog.Printf(gclog.LStdLog|gclog.LAccessLog, "Database is already migrated")
return
}
gclog.Println(gclog.LStdLog, migrateCompleteTxt)
}

View file

@ -100,9 +100,9 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
post.Password = gcutil.Md5Sum(password)
// Reverse escapes
nameCookie = strings.ReplaceAll(formName, "&", "&")
nameCookie = strings.ReplaceAll(nameCookie, "\\'", "'")
nameCookie = strings.ReplaceAll(url.QueryEscape(nameCookie), "+", "%20")
nameCookie = strings.Replace(formName, "&", "&", -1)
nameCookie = strings.Replace(nameCookie, "\\'", "'", -1)
nameCookie = strings.Replace(url.QueryEscape(nameCookie), "+", "%20", -1)
// add name and email cookies that will expire in a year (31536000 seconds)
http.SetCookie(writer, &http.Cookie{
@ -238,8 +238,8 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
}
boardDir := _board.Dir
filePath = path.Join(systemCritical.DocumentRoot, boardDir, "src", post.Filename)
thumbPath = path.Join(systemCritical.DocumentRoot, boardDir, "thumb", strings.ReplaceAll(post.Filename, "."+ext, "t."+thumbExt))
catalogThumbPath = path.Join(systemCritical.DocumentRoot, boardDir, "thumb", strings.ReplaceAll(post.Filename, "."+ext, "c."+thumbExt))
thumbPath = path.Join(systemCritical.DocumentRoot, boardDir, "thumb", strings.Replace(post.Filename, "."+ext, "t."+thumbExt, -1))
catalogThumbPath = path.Join(systemCritical.DocumentRoot, boardDir, "thumb", strings.Replace(post.Filename, "."+ext, "c."+thumbExt, -1))
if err = ioutil.WriteFile(filePath, data, 0777); err != nil {
gclog.Printf(gclog.LErrorLog, "Couldn't write file %q: %s", post.Filename, err.Error())