Skip to content
Permalink
Browse files

[WIP] QOR Admin article

  • Loading branch information
Depado committed Feb 13, 2019
1 parent 1c2f348 commit 39bc0166d2f093211828e1a495f33db18dd78732

This file was deleted.

@@ -0,0 +1,49 @@
package main

import (
"net/http"
"time"

"github.com/Depado/articles/code/qor/migrate"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/qor/admin"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)

type product struct {
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time

Name string
Price int
}

func main() {
var db *gorm.DB
var err error
if db, err = gorm.Open(
"postgres",
"user=qor dbname=qor_tutorial password=password sslmode=disable",
); err != nil {
logrus.WithError(err).Fatal("Couldn't initialize database connection")
}
defer db.Close()
if err = migrate.Start(db); err != nil {
logrus.WithError(err).Fatal("Couldn't run migration")
}

adm := admin.New(&admin.AdminConfig{SiteName: "Admin", DB: db})
mux := http.NewServeMux()
adm.MountTo("/admin", mux)

adm.AddResource(&product{})

r := gin.New()
r.Any("/admin/*resources", gin.WrapH(mux))
r.Run("127.0.0.1:8080")
}
@@ -0,0 +1,28 @@
package migrate

import (
"time"

"github.com/google/uuid"
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

var initial = &gormigrate.Migration{
ID: "initial",
Migrate: func(tx *gorm.DB) error {
type product struct {
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time

Name string
Price int
}
return tx.CreateTable(&product{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("products").Error
},
}
@@ -0,0 +1,15 @@
package migrate

import (
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

// Start starts the migration process
func Start(db *gorm.DB) error {
m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
uuidCheck,
initial,
})
return m.Migrate()
}
@@ -0,0 +1,25 @@
package migrate

import (
"errors"

"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

var uuidCheck = &gormigrate.Migration{
ID: "precheck",
Migrate: func(tx *gorm.DB) error {
var (
requiredext = "uuid-ossp"
count int
)
if err := tx.Table("pg_extension").Where("extname = ?", requiredext).Count(&count).Error; err != nil {
return err
}
if count < 1 {
return errors.New("extension uuid-ossp doesn't exist in the target database but is required")
}
return nil
},
}
@@ -0,0 +1,49 @@
package main

import (
"net/http"
"time"

"github.com/Depado/articles/code/qor/migrate"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/qor/admin"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)

type product struct {
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time

Name string
Price int
}

func main() {
var db *gorm.DB
var err error
if db, err = gorm.Open(
"postgres",
"user=qor dbname=qor_tutorial password=password sslmode=disable",
); err != nil {
logrus.WithError(err).Fatal("Couldn't initialize database connection")
}
defer db.Close()
if err = migrate.Start(db); err != nil {
logrus.WithError(err).Fatal("Couldn't run migration")
}

adm := admin.New(&admin.AdminConfig{SiteName: "Admin", DB: db})
mux := http.NewServeMux()
adm.MountTo("/admin", mux)

adm.AddResource(&product{})

r := gin.New()
r.Any("/admin/*resources", gin.WrapH(mux))
r.Run("127.0.0.1:8080")
}
@@ -0,0 +1,28 @@
package migrate

import (
"time"

"github.com/google/uuid"
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

var initial = &gormigrate.Migration{
ID: "initial",
Migrate: func(tx *gorm.DB) error {
type product struct {
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time

Name string
Price int
}
return tx.CreateTable(&product{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("products").Error
},
}
@@ -0,0 +1,15 @@
package migrate

import (
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

// Start starts the migration process
func Start(db *gorm.DB) error {
m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
uuidCheck,
initial,
})
return m.Migrate()
}
@@ -0,0 +1,25 @@
package migrate

import (
"errors"

"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

var uuidCheck = &gormigrate.Migration{
ID: "precheck",
Migrate: func(tx *gorm.DB) error {
var (
requiredext = "uuid-ossp"
count int
)
if err := tx.Table("pg_extension").Where("extname = ?", requiredext).Count(&count).Error; err != nil {
return err
}
if count < 1 {
return errors.New("extension uuid-ossp doesn't exist in the target database but is required")
}
return nil
},
}
Binary file not shown.
Binary file not shown.

0 comments on commit 39bc016

Please sign in to comment.
You can’t perform that action at this time.