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

Started work on migration script for old database

This commit is contained in:
comraderat 2020-05-30 22:43:01 +02:00
parent f0c1fbba94
commit c84b27d7ce
4 changed files with 86 additions and 4 deletions

40
oldDBMigration.sql Normal file
View file

@ -0,0 +1,40 @@
/*
Migrates the pre-refactor database of april 2020 to database verion 1
rename all tables to table_old
run version 1 population script
filter, copy change data
*/
INSERT INTO DBPREFIXsections (id, name, abbreviation, position, hidden)
SELECT id, name, abbreviation, list_order, hidden > 0
FROM DBPREFIXsections_old;
INSERT INTO DBPREFIXboards (
id,
section_id,
uri,
dir,
navbar_position,
title,
subtitle,
description,
max_file_size,
max_threads,
default_style,
locked,
created_at,
anonymous_name,
force_anonymous,
autosage_after,
no_images_after,
max_message_length,
min_message_length,
allow_embeds,
redirect_to_thread,
require_file,
enable_catalog)
SELECT id, section, dir, dir, list_order, title, subtitle, description, max_file_size, 1000,
default_style, locked, created_on, anonymous, forced_anon, autosage_after, no_images_after, max_message_length, 0, embeds_allowed,
redirect_to_thread, require_file, enable_catalog
FROM DBPREFIXboards_old;

View file

@ -69,8 +69,11 @@ func initDB(initFile string) *gcutil.GcError {
return gcutil.NewError(fmt.Sprintf( return gcutil.NewError(fmt.Sprintf(
"SQL database initialization file (%s) missing. Please reinstall gochan", initFile), false) "SQL database initialization file (%s) missing. Please reinstall gochan", initFile), false)
} }
return runSQLFile(filePath)
}
sqlBytes, err := ioutil.ReadFile(filePath) func runSQLFile(path string) *gcutil.GcError {
sqlBytes, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return gcutil.FromError(err, false) return gcutil.FromError(err, false)
} }

View file

@ -1056,3 +1056,15 @@ func doesGochanPrefixTableExist() (bool, *gcutil.GcError) {
} }
return count > 0, nil return count > 0, nil
} }
func renameTable(tablename string, tableNameNew string) *gcutil.GcError {
var sql = "ALTER TABLE DBPREFIX" + tablename + " RENAME TO DBPREFIX" + tableNameNew
_, err := ExecSQL(sql)
return err
}
func dropTable(tablename string) *gcutil.GcError {
var sql = "DROP TABLE DBPREFIX" + tablename
_, err := ExecSQL(sql)
return err
}

View file

@ -39,7 +39,7 @@ func handleVersioning(dbType string) *gcutil.GcError {
return err return err
} }
if isOldDesign { if isOldDesign {
return migratePreApril2020Database() return migratePreApril2020Database(dbType)
} }
//No old or current database versioning tables found. //No old or current database versioning tables found.
if config.Config.DBprefix != "" { if config.Config.DBprefix != "" {
@ -96,8 +96,35 @@ func versionHandler(foundDatabaseVersion int) *gcutil.GcError {
} }
func migratePreApril2020Database() *gcutil.GcError { func migratePreApril2020Database(dbType string) *gcutil.GcError {
return notImplemented var tables = []string{"announcements", "appeals", "banlist", "boards", "embeds", "info", "links", "posts", "reports", "sections", "sessions", "staff", "wordfilters"}
for _, i := range tables {
err := renameTable(i, i+"_old")
if err != nil {
return err
}
}
var buildfile = "initdb_" + dbType + ".sql"
err := runSQLFile(gcutil.FindResource(buildfile,
"/usr/local/share/gochan/"+buildfile,
"/usr/share/gochan/"+buildfile))
if err != nil {
return err
}
const migrFile = "oldDBMigration.sql"
err = runSQLFile(gcutil.FindResource(migrFile,
"/usr/local/share/gochan/"+migrFile,
"/usr/share/gochan/"+migrFile))
if err != nil {
return err
}
for _, i := range tables {
err := dropTable(i + "_old")
if err != nil {
return err
}
}
return nil
} }
var migrations = map[int]func() *gcutil.GcError{} var migrations = map[int]func() *gcutil.GcError{}