Post-Migrator/main.go

146 lines
4.3 KiB
Go

package main
import (
"database/sql"
"federated.computer/wp-sync-slowtwitch/services/migration"
"fmt"
)
// WP Config
const baseUrl = "http://go-api-playground.local/"
const wordpressKey = "admin"
const wordpressSecret = "7ZeY ZCwp 3DwL EBAK vGtf NW9J"
// DB Config
const slowtwitchAdminUser = "admin"
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
const slowtwitchDbName = "slowtwitch"
const migrationDbName = "slowtwitch_transfer_threaded_test"
const federatedDbUrl = "slowtwitch.northend.network"
const federatedDbPort = "3306"
var slowtwitchDB *sql.DB
var resultsDB *sql.DB
func main() {
connectToDatabases()
migrateAuthors()
migrateCategories()
migratePosts()
}
func connectToDatabases() {
slowtwitchDatabase, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
if slowtwitchDbErr != nil {
panic("Could not connect to slowtwitch database.")
} else {
slowtwitchDB = slowtwitchDatabase
}
resultsDatabase, resultsDBerr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, migrationDbName)
if resultsDBerr != nil {
panic("Could not connect to results database.")
} else {
resultsDB = resultsDatabase
}
createEditorsTableSQL := `
create table if not exists EditorsResults(
Id int primary key not null auto_increment,
WordpressId int not null,
Username nvarchar(100) not null,
Email nvarchar(100),
IsSuccess BIT not null
);`
_, editorsTableErr := resultsDB.Exec(createEditorsTableSQL)
if editorsTableErr != nil {
panic("Could not create editors table.")
}
createCategoryResultsTableSQL := `
create table if not exists CategoryResults (
Id int primary key not null auto_increment,
SlowtwitchId int not null,
WordpressId int not null,
OldUrl nvarchar(500) not null,
OldUrlStatus int,
NewUrl nvarchar(500),
IsSuccess BIT not null,
ErrorMessage nvarchar(1500)
);`
_, createCategoryResultsTableErr := resultsDB.Exec(createCategoryResultsTableSQL)
if createCategoryResultsTableErr != nil {
panic("Could not create category results table.")
}
createPostsResultsTableSQL := `
create table if not exists PostResults(
Id int primary key not null auto_increment,
SlowtwitchId int not null,
WordpressId int not null,
OldUrl nvarchar(500) not null,
OldUrlStatus int,
NewUrl nvarchar(500),
IsSuccess BIT not null,
ErrorMessage nvarchar(1500)
);`
_, createPostsResultsTableErr := resultsDB.Exec(createPostsResultsTableSQL)
if createPostsResultsTableErr != nil {
panic("Could not create post results table.")
}
createImageResultsTableSQL := `
create table if not exists ImageResults(
Id int primary key not null auto_increment,
PostId int null,
WordpressId int not null,
OldUrl nvarchar(500) not null,
NewUrl nvarchar(500),
IsSuccess BIT not null,
ErrorMessage nvarchar(1500),
foreign key (PostId) references PostResults(Id)
);`
_, createImageResultsTableErr := resultsDB.Exec(createImageResultsTableSQL)
if createImageResultsTableErr != nil {
panic("Could not create image results table.")
}
}
func migrateAuthors() {
fmt.Println("Migrating Authors and Editors")
editorMigration := migration.MigrateAuthors{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
WordpressBaseUrl: baseUrl,
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
editorResults := editorMigration.Execute()
fmt.Println("Migrated", len(editorResults), "Editors and Authors")
}
func migrateCategories() {
fmt.Println("Migrating Categories")
categoryMigration := migration.MigrateCategories{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
WordpressBaseUrl: baseUrl,
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
categoryResults := categoryMigration.Execute()
fmt.Println("Migrated", len(categoryResults), "Categories")
}
func migratePosts() {
fmt.Println("Migrating Posts")
postMigration := migration.MigratePosts{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
WordpressBaseUrl: baseUrl,
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
results := postMigration.Execute()
fmt.Println("Migration complete, migrated", len(results), "Posts")
}