func Initialize() error { // If journey.db does not exist, look for a Ghost database to convert if !helpers.FileExists(filenames.DatabaseFilename) { // Convert Ghost database if available (time format needs to change to be compatible with journey) migration.Ghost() } // Open or create database file var err error readDB, err = sql.Open("sqlite3", filenames.DatabaseFilename) if err != nil { return err } readDB.SetMaxIdleConns(256) // TODO: is this enough? err = readDB.Ping() if err != nil { return err } currentTime := time.Now() _, err = readDB.Exec(stmtInitialization, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime) // TODO: Is Commit()/Rollback() needed for DB.Exec()? if err != nil { return err } err = checkBlogSettings() if err != nil { return err } return nil }
// This function converts all fields in the Ghost db that are not compatible with Journey (only date fields for now. Ghost uses a javascript-specific unix timestamp). func convertGhostDatabase(fileName string) error { // If journey.db exists already, don't convert this file if helpers.FileExists(filenames.DatabaseFilename) { return errors.New(filenames.DatabaseFilename + " already exists.") } log.Println("Trying to convert " + fileName + "...") readDB, err := sql.Open("sqlite3", fileName) if err != nil { log.Println("Error:", err) return err } err = readDB.Ping() if err != nil { log.Println("Error:", err) return err } // Convert posts err = convertPosts(readDB) if err != nil { log.Println("Error:", err) return err } // Convert users err = convertUsers(readDB) if err != nil { log.Println("Error:", err) return err } // Convert tags err = convertDates(readDB, stmtRetrieveGhostTags, stmtUpdateGhostTags) if err != nil { log.Println("Error:", err) return err } // Convert roles err = convertDates(readDB, stmtRetrieveGhostRoles, stmtUpdateGhostRoles) if err != nil { log.Println("Error:", err) return err } // Convert settings err = convertDates(readDB, stmtRetrieveGhostSettings, stmtUpdateGhostSettings) if err != nil { log.Println("Error:", err) return err } // Set default theme err = setDefaultTheme(readDB) if err != nil { log.Println("Error:", err) return err } // Convert permissions (not used by Journey at the moment) err = convertDates(readDB, stmtRetrieveGhostPermissions, stmtUpdateGhostPermissions) if err != nil { log.Println("Error:", err) return err } // Convert clients (not used by Journey at the moment) err = convertDates(readDB, stmtRetrieveGhostClients, stmtUpdateGhostClients) if err != nil { log.Println("Error:", err) return err } // All went well. Close database connection. err = readDB.Close() if err != nil { log.Println("Error:", err) return err } // Rename file to Journey format err = os.Rename(fileName, filenames.DatabaseFilename) if err != nil { log.Println("Error:", err) return err } log.Println("Success!") return nil }