migrate users - pre refactor to send errs to callers
This commit is contained in:
parent
7931a859f3
commit
b3ef5a42ad
8
main.go
8
main.go
@ -31,6 +31,14 @@ func main() {
|
|||||||
fmt.Println(resultsDBerr)
|
fmt.Println(resultsDBerr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
editorMigration := migration.MigrateAuthors{
|
||||||
|
SlowtwitchDatabase: slowtwitchDB,
|
||||||
|
ResultsDatabase: resultsDB,
|
||||||
|
WordpressBaseUrl: baseUrl,
|
||||||
|
WordpressUser: wordpressKey,
|
||||||
|
WordpressPassword: wordpressSecret,
|
||||||
|
}
|
||||||
|
editorMigration.Execute()
|
||||||
categoryMigration := migration.MigrateCategories{
|
categoryMigration := migration.MigrateCategories{
|
||||||
SlowtwitchDatabase: slowtwitchDB,
|
SlowtwitchDatabase: slowtwitchDB,
|
||||||
ResultsDatabase: resultsDB,
|
ResultsDatabase: resultsDB,
|
||||||
|
54
services/migration/editor-result.go
Normal file
54
services/migration/editor-result.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EditorResult struct {
|
||||||
|
WordpressId int
|
||||||
|
Username string
|
||||||
|
Email string
|
||||||
|
IsSuccess bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateEditorResult(result EditorResult, db *sql.DB) error {
|
||||||
|
_, err := db.Exec("insert into EditorsResults(WordpressId, Username, Email, IsSuccess) values (?, ?, ?, ?)", result.WordpressId, result.Username, result.Email, result.IsSuccess)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetEditor(username string, db *sql.DB) (EditorResult, error) {
|
||||||
|
rows, err := db.Query("select WordpressId, Username, Email, (IsSuccess = b'1') from EditorsResults where Username = ?", username)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
editor := EditorResult{}
|
||||||
|
|
||||||
|
err := rows.Scan(&editor.WordpressId, &editor.Username, &editor.Email, &editor.IsSuccess)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if editor.Username == username {
|
||||||
|
return editor, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EditorResult{}, errors.New("Could not find editor.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditorHasBeenMigrated(username string, db *sql.DB) bool {
|
||||||
|
_, err := GetEditor(username, db)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,50 @@
|
|||||||
package migration
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
||||||
|
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MigrateAuthors struct {
|
||||||
|
SlowtwitchDatabase *sql.DB
|
||||||
|
ResultsDatabase *sql.DB
|
||||||
|
WordpressBaseUrl string
|
||||||
|
WordpressUser string
|
||||||
|
WordpressPassword string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (migration *MigrateAuthors) Execute() []EditorResult {
|
||||||
|
editors := slowtwitch.GetUsers(migration.SlowtwitchDatabase)
|
||||||
|
var output []EditorResult
|
||||||
|
|
||||||
|
for _, editor := range editors {
|
||||||
|
hasBeenMigrated := EditorHasBeenMigrated(editor.Username, migration.ResultsDatabase)
|
||||||
|
|
||||||
|
if hasBeenMigrated == false {
|
||||||
|
createUser := wordpress.CreateUser{
|
||||||
|
Username: editor.Username,
|
||||||
|
Email: editor.Email,
|
||||||
|
Password: editor.Password,
|
||||||
|
Roles: "editor",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||||
|
editorResult := EditorResult{
|
||||||
|
Username: result.Username,
|
||||||
|
Email: result.Username,
|
||||||
|
WordpressId: result.Id,
|
||||||
|
IsSuccess: true,
|
||||||
|
}
|
||||||
|
err := CreateEditorResult(editorResult, migration.ResultsDatabase)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
output = append(output, editorResult)
|
||||||
|
fmt.Println("Created user:", result.Username)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
36
services/slowtwitch/get-users.go
Normal file
36
services/slowtwitch/get-users.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package slowtwitch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SlowtwitchUser struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Email string
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUsers(db *sql.DB) []SlowtwitchUser {
|
||||||
|
rows, err := db.Query("select Username, Password, Email from glinks_Users;")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var users []SlowtwitchUser
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
user := SlowtwitchUser{}
|
||||||
|
err := rows.Scan(&user.Username, &user.Password, &user.Email)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
users = append(users, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
return users
|
||||||
|
}
|
@ -7,9 +7,6 @@ import (
|
|||||||
|
|
||||||
type CreateUser struct {
|
type CreateUser struct {
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
FirstName string `json:"first_name"`
|
|
||||||
LastName string `json:"last_name"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Roles string `json:"roles"`
|
Roles string `json:"roles"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
|
Loading…
Reference in New Issue
Block a user