2024-05-07 22:03:15 +00:00
|
|
|
package main
|
|
|
|
|
2024-05-10 20:01:05 +00:00
|
|
|
import (
|
2024-05-19 21:00:33 +00:00
|
|
|
"database/sql"
|
2024-05-16 16:26:59 +00:00
|
|
|
"federated.computer/wp-sync-slowtwitch/services/migration"
|
2024-05-21 17:31:59 +00:00
|
|
|
"fmt"
|
2024-05-10 20:01:05 +00:00
|
|
|
)
|
|
|
|
|
2024-05-21 21:15:43 +00:00
|
|
|
// WP Config
|
2024-05-25 15:31:59 +00:00
|
|
|
const baseUrl = "http://go-api-playground.local/"
|
|
|
|
const wordpressKey = "admin"
|
|
|
|
const wordpressSecret = "KStj VH7E 9UZf FV8C ptGQ F4Tl"
|
2024-05-07 22:03:15 +00:00
|
|
|
|
2024-05-21 21:15:43 +00:00
|
|
|
// DB Config
|
2024-05-14 21:59:04 +00:00
|
|
|
const slowtwitchAdminUser = "admin"
|
|
|
|
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
|
|
|
|
const slowtwitchDbName = "slowtwitch"
|
2024-05-25 02:12:40 +00:00
|
|
|
const migrationDbName = "slowtwitch_transfer_threaded_test"
|
2024-05-15 23:37:42 +00:00
|
|
|
const federatedDbUrl = "slowtwitch.northend.network"
|
|
|
|
const federatedDbPort = "3306"
|
2024-05-14 21:59:04 +00:00
|
|
|
|
2024-05-19 21:00:33 +00:00
|
|
|
var slowtwitchDB *sql.DB
|
|
|
|
var resultsDB *sql.DB
|
2024-05-07 22:03:15 +00:00
|
|
|
|
|
|
|
func main() {
|
2024-05-21 17:37:55 +00:00
|
|
|
connectToDatabases()
|
|
|
|
migrateAuthors()
|
|
|
|
migrateCategories()
|
|
|
|
migratePosts()
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectToDatabases() {
|
2024-05-19 21:00:33 +00:00
|
|
|
slowtwitchDatabase, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
|
2024-05-16 16:26:59 +00:00
|
|
|
if slowtwitchDbErr != nil {
|
2024-05-19 21:00:33 +00:00
|
|
|
panic("Could not connect to slowtwitch database.")
|
|
|
|
} else {
|
|
|
|
slowtwitchDB = slowtwitchDatabase
|
2024-05-16 16:26:59 +00:00
|
|
|
}
|
2024-05-19 21:00:33 +00:00
|
|
|
resultsDatabase, resultsDBerr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, migrationDbName)
|
2024-05-16 16:26:59 +00:00
|
|
|
if resultsDBerr != nil {
|
2024-05-19 21:00:33 +00:00
|
|
|
panic("Could not connect to results database.")
|
|
|
|
} else {
|
|
|
|
resultsDB = resultsDatabase
|
2024-05-17 18:49:55 +00:00
|
|
|
}
|
2024-05-25 02:03:56 +00:00
|
|
|
|
|
|
|
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 := `
|
2024-05-25 02:04:26 +00:00
|
|
|
create table if not exists PostResults(
|
2024-05-25 02:03:56 +00:00
|
|
|
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.")
|
|
|
|
}
|
2024-05-25 02:12:40 +00:00
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|
2024-05-21 17:37:55 +00:00
|
|
|
}
|
2024-05-17 19:52:38 +00:00
|
|
|
|
2024-05-21 17:37:55 +00:00
|
|
|
func migrateAuthors() {
|
2024-05-21 17:31:59 +00:00
|
|
|
fmt.Println("Migrating Authors and Editors")
|
2024-05-16 19:48:39 +00:00
|
|
|
editorMigration := migration.MigrateAuthors{
|
|
|
|
SlowtwitchDatabase: slowtwitchDB,
|
|
|
|
ResultsDatabase: resultsDB,
|
|
|
|
WordpressBaseUrl: baseUrl,
|
|
|
|
WordpressUser: wordpressKey,
|
|
|
|
WordpressPassword: wordpressSecret,
|
|
|
|
}
|
2024-05-21 17:31:59 +00:00
|
|
|
editorResults := editorMigration.Execute()
|
|
|
|
fmt.Println("Migrated", len(editorResults), "Editors and Authors")
|
2024-05-21 17:37:55 +00:00
|
|
|
}
|
2024-05-21 17:31:59 +00:00
|
|
|
|
2024-05-21 17:37:55 +00:00
|
|
|
func migrateCategories() {
|
2024-05-21 17:31:59 +00:00
|
|
|
fmt.Println("Migrating Categories")
|
2024-05-16 16:26:59 +00:00
|
|
|
categoryMigration := migration.MigrateCategories{
|
|
|
|
SlowtwitchDatabase: slowtwitchDB,
|
|
|
|
ResultsDatabase: resultsDB,
|
|
|
|
WordpressBaseUrl: baseUrl,
|
|
|
|
WordpressUser: wordpressKey,
|
|
|
|
WordpressPassword: wordpressSecret,
|
2024-05-14 21:59:04 +00:00
|
|
|
}
|
2024-05-21 17:31:59 +00:00
|
|
|
categoryResults := categoryMigration.Execute()
|
|
|
|
fmt.Println("Migrated", len(categoryResults), "Categories")
|
2024-05-21 17:37:55 +00:00
|
|
|
}
|
2024-05-21 17:31:59 +00:00
|
|
|
|
2024-05-21 17:37:55 +00:00
|
|
|
func migratePosts() {
|
2024-05-21 17:31:59 +00:00
|
|
|
fmt.Println("Migrating Posts")
|
2024-05-20 01:35:53 +00:00
|
|
|
postMigration := migration.MigratePosts{
|
|
|
|
SlowtwitchDatabase: slowtwitchDB,
|
|
|
|
ResultsDatabase: resultsDB,
|
|
|
|
WordpressBaseUrl: baseUrl,
|
|
|
|
WordpressUser: wordpressKey,
|
|
|
|
WordpressPassword: wordpressSecret,
|
|
|
|
}
|
2024-05-23 13:41:47 +00:00
|
|
|
results := postMigration.Execute()
|
|
|
|
fmt.Println("Migration complete, migrated", len(results), "Posts")
|
2024-05-10 20:01:05 +00:00
|
|
|
}
|