Add code to generate redirects output
This commit is contained in:
parent
388ae5a23d
commit
619e24ddd8
3 changed files with 102 additions and 0 deletions
55
db.go
Normal file
55
db.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AccountMigration struct {
|
||||
AccountID int64 `gorm:"account_id"`
|
||||
Acct string `gorm:"acct"`
|
||||
FollowersCount string `gorm:"followers_count"`
|
||||
TargetAccountID int64 `gorm:"target_account_id"`
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
ID int64 `gorm:"id"`
|
||||
Username string `gorm:"username"`
|
||||
Domain string `gorm:"domain"`
|
||||
MovedToAccountID int64 `gorm:"moved_to_account_id"`
|
||||
}
|
||||
|
||||
func findMigration(db *gorm.DB, acct int64) *Account {
|
||||
acc := &Account{ID: acct}
|
||||
db.Model(&Account{}).Find(&acc)
|
||||
if acc.MovedToAccountID != 0 {
|
||||
return findMigration(db, acc.MovedToAccountID)
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
func getAccount(db *gorm.DB, acct int64) *Account {
|
||||
acc := &Account{ID: acct}
|
||||
db.Model(&Account{}).Find(&acc)
|
||||
return acc
|
||||
}
|
||||
|
||||
func generateOutput() {
|
||||
dsn := "host= user= password= dbname= port= sslmode="
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal("Unable to connect to database")
|
||||
}
|
||||
|
||||
var accts = []AccountMigration{}
|
||||
|
||||
db.Model(&AccountMigration{}).Where("target_account_id IS NOT NULL").Find(&accts)
|
||||
for _, acct := range accts {
|
||||
a := findMigration(db, acct.TargetAccountID)
|
||||
ghacct := getAccount(db, acct.AccountID)
|
||||
fmt.Printf("redirects[\"%s@gearheads.social\"] = []string{\"%s\", \"%s\"}\n", ghacct.Username, a.Username, a.Domain)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue