Add migrations helper and refactor DB operations with environment variable support

This commit is contained in:
2025-05-10 22:18:38 +03:30
parent 0533151c1b
commit 5a897b5412
2 changed files with 15 additions and 16 deletions

View File

@@ -5,6 +5,10 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func AddMigrations(db *gorm.DB) {
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
}
func GetExistingPostIds(db *gorm.DB) []string { func GetExistingPostIds(db *gorm.DB) []string {
var existingPostIds []string var existingPostIds []string
db.Model(&models.Post{}).Pluck("id", &existingPostIds) db.Model(&models.Post{}).Pluck("id", &existingPostIds)

27
main.go
View File

@@ -2,17 +2,22 @@ package main
import ( import (
"CatsOfMastodonBotGo/helpers" "CatsOfMastodonBotGo/helpers"
"CatsOfMastodonBotGo/models"
"context" "context"
"log" "log"
"os"
"time" "time"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger"
) )
func main() { func main() {
var tag = "catsofmastodon" var tag = "catsofmastodon"
var instance = "https://haminoa.net" var instance = os.Getenv("INSTANCE")
if instance == "" {
instance = "https://haminoa.net"
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
err, posts := helpers.GetPosts(ctx, tag, instance) err, posts := helpers.GetPosts(ctx, tag, instance)
@@ -21,13 +26,12 @@ func main() {
} }
log.Println("Number of fetched posts: ", len(posts)) log.Println("Number of fetched posts: ", len(posts))
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{Logger: logger.Default.LogMode(logger.Warn)})
if err != nil { if err != nil {
panic("failed to connect database") panic("failed to connect database")
} }
// Migrate the schema helpers.AddMigrations(db)
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
var existingPostIds = helpers.GetExistingPostIds(db) var existingPostIds = helpers.GetExistingPostIds(db)
var existingAccountIds = helpers.GetExistingAccountIds(db) var existingAccountIds = helpers.GetExistingAccountIds(db)
@@ -40,23 +44,14 @@ func main() {
log.Println("Number of new accounts: ", len(newAccounts)) log.Println("Number of new accounts: ", len(newAccounts))
if newAccounts != nil { if newAccounts != nil {
log.Println("Number of inserted accounts: ", db.Create(&newAccounts).RowsAffected) log.Println("Number of inserted accounts: ", helpers.InsertNewAccounts(db, newAccounts))
} else { } else {
log.Println("No new accounts inserted") log.Println("No new accounts inserted")
} }
if newPosts != nil { if newPosts != nil {
log.Println("Number of inserted posts: ", db.Create(&newPosts).RowsAffected) log.Println("Number of inserted posts: ", helpers.InsertNewPosts(db, newPosts))
} else { } else {
log.Println("No new posts inserted") log.Println("No new posts inserted")
} }
} }
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}