From e502692105c706d4c3f66ddef6d92279b6bd255e Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Fri, 24 May 2024 20:03:56 -0600 Subject: [PATCH] post relationships + create table if not exists --- main.go | 61 +++++++++++++++++++++ services/migration/migrate-posts.go | 4 +- services/slowtwitch/get-related-post-ids.go | 2 +- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index f0c6f03..fc954a0 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,67 @@ func connectToDatabases() { } 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.") + } + + 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.") + } + + createPostsResultsTableSQL := ` + create table 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.") + } } func migrateAuthors() { diff --git a/services/migration/migrate-posts.go b/services/migration/migrate-posts.go index 84c46d9..57a0a91 100644 --- a/services/migration/migrate-posts.go +++ b/services/migration/migrate-posts.go @@ -243,7 +243,7 @@ func updatePostRelationships(postResults []PostResult, migration MigratePosts) { relatedSlowtwitchIds, slowtwitchIdsErr := slowtwitch.GetRelatedArticleIds(postResult.SlowtwitchId, migration.SlowtwitchDatabase) - if slowtwitchIdsErr != nil { + if slowtwitchIdsErr != nil || len(relatedSlowtwitchIds) == 0 { continue } for _, slowtwitchRelatedId := range relatedSlowtwitchIds { @@ -251,7 +251,7 @@ func updatePostRelationships(postResults []PostResult, migration MigratePosts) { if wordpressIdErr != nil { continue } - relatedSlowtwitchIds = append(relatedWordpressIds, wordpressRelatedId) + relatedWordpressIds = append(relatedWordpressIds, wordpressRelatedId) } if len(relatedWordpressIds) > 0 { diff --git a/services/slowtwitch/get-related-post-ids.go b/services/slowtwitch/get-related-post-ids.go index a3a898a..cf79ca1 100644 --- a/services/slowtwitch/get-related-post-ids.go +++ b/services/slowtwitch/get-related-post-ids.go @@ -15,7 +15,7 @@ func GetRelatedArticleIds(postId int, db *sql.DB) ([]int, error) { } var ids []int - idsAsStrings := strings.Split(spaceSeparatedIds, " ") + idsAsStrings := strings.Split(spaceSeparatedIds, "\r\n") for _, stringId := range idsAsStrings { id, err := strconv.Atoi(stringId) if err == nil {