Files
dogapi/db/dbmigrate/migrate.go
2021-01-31 19:49:23 -05:00

79 lines
2.3 KiB
Go

/*
Simple API the serves pictures of dogs
Copyright (C) 2021 Pin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"log"
_ "github.com/lib/pq"
"database/sql"
"os"
"fmt"
_ "github.com/joho/godotenv/autoload"
_ "github.com/go-sql-driver/mysql"
)
type DogRow struct {
Id int
Author string
Title string
Format string
Hash string
Type string
}
func checkerr(err error) {
if err != nil {
log.Println(err)
}
}
func dsn(dbName string) string {
return fmt.Sprintf("%s:%s@tcp(%s)/%s", os.Getenv("MYSQLUSERNAME"), os.Getenv("MYSQLPASS"), os.Getenv("MYSQLHOSTNAME"), dbName)
}
func main() {
log.Println("Log")
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("DBUSER"), os.Getenv("PASSWD"), os.Getenv("DBNAME"))
pgdb, err := sql.Open("postgres", psqlInfo)
checkerr(err)
mysqldb, err := sql.Open("mysql", dsn(os.Getenv("MYSQLDBNAME")))
checkerr(err)
rows, err := mysqldb.Query(`select * from pug;`)
checkerr(err)
defer rows.Close()
for rows.Next() {
var rowsel DogRow
if err := rows.Scan(&rowsel.Id, &rowsel.Author, &rowsel.Title, &rowsel.Format, &rowsel.Hash, &rowsel.Type); err != nil {
log.Fatal(err)
}
_, err = pgdb.Exec(`insert into dog_pictures (breed_id, author, title, picture_format,
picture_hash, picture_url) values ((select breed_id from breed_manifest where breed = $1),
$2, $3, $4, $5, $6)`, rowsel.Type, rowsel.Author, rowsel.Title, rowsel.Format, rowsel.Hash,
os.Getenv("DOGURL") + rowsel.Type + "/" + rowsel.Hash + "." + rowsel.Format)
if err != nil {
log.Println(rowsel.Hash)
}
}
}