59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/go-pg/pg"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
type DB struct {
|
||
|
PgDB *pg.DB
|
||
|
Schema string
|
||
|
}
|
||
|
|
||
|
func Setup(db *DB) error {
|
||
|
_, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`)
|
||
|
if err != nil {
|
||
|
return errors.Wrap(err, "Error creating schema")
|
||
|
}
|
||
|
|
||
|
_, err = db.PgDB.Exec(`CREATE TABLE IF NOT EXISTS users.users (
|
||
|
uid UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||
|
username varchar(64) UNIQUE NOT NULL,
|
||
|
email varchar(100) UNIQUE NOT NULL,
|
||
|
password varchar(256) NOT NULL
|
||
|
)`)
|
||
|
if err != nil {
|
||
|
return errors.Wrap(err, "Error creating users table")
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func UserAdd(db *DB, username, email, pwhash string) error {
|
||
|
stmt, err := db.PgDB.Prepare("INSERT INTO users.users VALUES ($1, $2, $3)")
|
||
|
if err != nil {
|
||
|
panic("Bad statement in UserAdd")
|
||
|
}
|
||
|
_, err = stmt.Exec(username, email, pwhash)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "Error adding user `%s`", email)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
fmt.Println("vim-go")
|
||
|
pgdb, err := pg.Connect(&pg.Options{
|
||
|
User: "panel",
|
||
|
Password: "",
|
||
|
Database: "feeds",
|
||
|
})
|
||
|
if err != nil {
|
||
|
os.Fprintln(os.Stderr, err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
db := DB{PgDB: db}
|
||
|
db.Setup()
|
||
|
}
|