Skip to content

Instantly share code, notes, and snippets.

@mutuadavid93
Created August 24, 2020 14:28
Show Gist options
  • Save mutuadavid93/3ca3870d13d76443057e9b6e740848cb to your computer and use it in GitHub Desktop.
Save mutuadavid93/3ca3870d13d76443057e9b6e740848cb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type User struct {
// Embed Model struct promoting it's fields to User struct
gorm.Model
// Other User specific fields
Name string
Email *string `gorm:"unique_index;not null"`
}
// Reference :: https://pkg.go.dev/github.com/lib/pq?tab=doc
// Create all PostgreSQL Database Configs
const (
host = "localhost"
port = 5432
dbname = "lenslocked_dev"
user = "streetmoney"
)
func main() {
driverName := "postgres"
// Note :: Remove the password database field in the psqlinfo below if it's empty
// otherwise psql will behave abnormally
psqlinfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable",
host, port, user, dbname)
// Note :: sql.OPen() doesn't connect to database, it only ensures the
// driver and datasource are valid
db, err := gorm.Open(driverName, psqlinfo)
if err != nil {
panic(err)
}
defer db.Close()
// db.DropTableIfExists(&User{})
//
// Migrate the schema creating the User table for us
// db.AutoMigrate(&User{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment