Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Lhussiez
committed
Feb 13, 2019
1 parent
1c2f348
commit 39bc016
Showing
12 changed files
with
337 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.