migrate from govendor to dep

This commit is contained in:
bergquist
2018-01-19 09:48:15 +01:00
parent 5bb22b836d
commit e023f79c5a
7349 changed files with 3153302 additions and 64302 deletions
+4
View File
@@ -45,6 +45,10 @@ func (b *Builder) selectWriteTo(w Writer) error {
}
}
if !b.cond.IsValid() {
return nil
}
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
return err
}
+4 -2
View File
@@ -25,7 +25,9 @@ func And(conds ...Cond) Cond {
func (and condAnd) WriteTo(w Writer) error {
for i, cond := range and {
_, isOr := cond.(condOr)
if isOr {
_, isExpr := cond.(expr)
wrap := isOr || isExpr
if wrap {
fmt.Fprint(w, "(")
}
@@ -34,7 +36,7 @@ func (and condAnd) WriteTo(w Writer) error {
return err
}
if isOr {
if wrap {
fmt.Fprint(w, ")")
}
+2 -4
View File
@@ -23,10 +23,8 @@ func In(col string, values ...interface{}) Cond {
}
func (condIn condIn) handleBlank(w Writer) error {
if _, err := fmt.Fprintf(w, "%s IN ()", condIn.col); err != nil {
return err
}
return nil
_, err := fmt.Fprint(w, "0=1")
return err
}
func (condIn condIn) WriteTo(w Writer) error {
+2 -4
View File
@@ -20,10 +20,8 @@ func NotIn(col string, values ...interface{}) Cond {
}
func (condNotIn condNotIn) handleBlank(w Writer) error {
if _, err := fmt.Fprintf(w, "%s NOT IN ()", condNotIn.col); err != nil {
return err
}
return nil
_, err := fmt.Fprint(w, "0=0")
return err
}
func (condNotIn condNotIn) WriteTo(w Writer) error {
+1
View File
@@ -0,0 +1 @@
*.db
+30
View File
@@ -0,0 +1,30 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
*.db
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.log
.vendor
temp_test.go
.vscode
xorm.test
*.sqlite3
+6
View File
@@ -0,0 +1,6 @@
[submodule "docs/manual-en-US"]
path = docs/manual-en-US
url = https://github.com/go-xorm/manual-en-US.git
[submodule "docs/manual-zh-CN"]
path = docs/manual-zh-CN
url = https://github.com/go-xorm/manual-zh-CN.git
+2
View File
@@ -0,0 +1,2 @@
[target]
path = github.com/go-xorm/xorm
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 238 KiB

+5
View File
@@ -0,0 +1,5 @@
# Xorm Examples
Notice: all the examples will ask you install extra package `github.com/mattn/go-sqlite3`, since it depends on cgo. You have to compile it after you install a c++ compile. Please see [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3).
And then, you can run the examples via `go run xxx.go`. Every go file is a standalone example.
+5 -6
View File
@@ -8,6 +8,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// User describes a user
type User struct {
Id int64
Name string
@@ -22,7 +23,7 @@ func main() {
fmt.Println(err)
return
}
Orm.ShowSQL = true
Orm.ShowSQL(true)
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
Orm.SetDefaultCacher(cacher)
@@ -38,7 +39,7 @@ func main() {
return
}
users := make([]User, 0)
var users []User
err = Orm.Find(&users)
if err != nil {
fmt.Println(err)
@@ -47,8 +48,7 @@ func main() {
fmt.Println("users:", users)
users2 := make([]User, 0)
var users2 []User
err = Orm.Find(&users2)
if err != nil {
fmt.Println(err)
@@ -57,8 +57,7 @@ func main() {
fmt.Println("users2:", users2)
users3 := make([]User, 0)
var users3 []User
err = Orm.Find(&users3)
if err != nil {
fmt.Println(err)
+4 -3
View File
@@ -10,6 +10,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// User describes a user
type User struct {
Id int64
Name string
@@ -24,7 +25,7 @@ func mysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
}
var u *User = &User{}
var u = &User{}
func test(engine *xorm.Engine) {
err := engine.CreateTables(u)
@@ -85,7 +86,7 @@ func main() {
fmt.Println(err)
return
}
engine.ShowSQL = true
engine.ShowSQL(true)
cacher := xorm.NewLRUCacher2(xorm.NewMemoryStore(), time.Hour, 1000)
engine.SetDefaultCacher(cacher)
fmt.Println(engine)
@@ -95,7 +96,7 @@ func main() {
fmt.Println("-----start mysql go routines-----")
engine, err = mysqlEngine()
engine.ShowSQL = true
engine.ShowSQL(true)
cacher = xorm.NewLRUCacher2(xorm.NewMemoryStore(), time.Hour, 1000)
engine.SetDefaultCacher(cacher)
if err != nil {
+15 -11
View File
@@ -9,35 +9,39 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// Status describes a status
type Status struct {
Name string
Color string
}
// defines some statuses
var (
Registed Status = Status{"Registed", "white"}
Approved Status = Status{"Approved", "green"}
Removed Status = Status{"Removed", "red"}
Statuses map[string]Status = map[string]Status{
Registed.Name: Registed,
Approved.Name: Approved,
Removed.Name: Removed,
Registered = Status{"Registered", "white"}
Approved = Status{"Approved", "green"}
Removed = Status{"Removed", "red"}
Statuses = map[string]Status{
Registered.Name: Registered,
Approved.Name: Approved,
Removed.Name: Removed,
}
)
// FromDB implemented xorm.Conversion convent database data to self
func (s *Status) FromDB(bytes []byte) error {
if r, ok := Statuses[string(bytes)]; ok {
*s = r
return nil
} else {
return errors.New("no this data")
}
return errors.New("no this data")
}
// ToDB implemented xorm.Conversion convent to database data
func (s *Status) ToDB() ([]byte, error) {
return []byte(s.Name), nil
}
// User describes a user
type User struct {
Id int64
Name string
@@ -53,14 +57,14 @@ func main() {
fmt.Println(err)
return
}
Orm.ShowSQL = true
Orm.ShowSQL(true)
err = Orm.CreateTables(&User{})
if err != nil {
fmt.Println(err)
return
}
_, err = Orm.Insert(&User{1, "xlw", Registed})
_, err = Orm.Insert(&User{1, "xlw", Registered})
if err != nil {
fmt.Println(err)
return
+10 -7
View File
@@ -8,17 +8,20 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// User describes a user
type User struct {
Id int64
Name string
}
// LoginInfo describes a login information
type LoginInfo struct {
Id int64
IP string
UserId int64
}
// LoginInfo1 describes a login information
type LoginInfo1 struct {
LoginInfo `xorm:"extends"`
UserName string
@@ -28,27 +31,27 @@ func main() {
f := "derive.db"
os.Remove(f)
Orm, err := xorm.NewEngine("sqlite3", f)
orm, err := xorm.NewEngine("sqlite3", f)
if err != nil {
fmt.Println(err)
return
}
defer Orm.Close()
Orm.ShowSQL = true
err = Orm.CreateTables(&User{}, &LoginInfo{})
defer orm.Close()
orm.ShowSQL(true)
err = orm.CreateTables(&User{}, &LoginInfo{})
if err != nil {
fmt.Println(err)
return
}
_, err = Orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1})
_, err = orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1})
if err != nil {
fmt.Println(err)
return
}
info := LoginInfo{}
_, err = Orm.Id(1).Get(&info)
_, err = orm.Id(1).Get(&info)
if err != nil {
fmt.Println(err)
return
@@ -56,7 +59,7 @@ func main() {
fmt.Println(info)
infos := make([]LoginInfo1, 0)
err = Orm.Sql(`select *, (select name from user where id = login_info.user_id) as user_name from
err = orm.Sql(`select *, (select name from user where id = login_info.user_id) as user_name from
login_info limit 10`).Find(&infos)
if err != nil {
fmt.Println(err)
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"fmt"
"os"
"time"
"github.com/go-xorm/xorm"
)
// User describes a user
type User struct {
Id int64
Name string
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
func main() {
f := "conversion.db"
os.Remove(f)
orm, err := xorm.NewEngine("sqlite3", f)
if err != nil {
fmt.Println(err)
return
}
orm.ShowSQL(true)
err = orm.CreateTables(&User{})
if err != nil {
fmt.Println(err)
return
}
_, err = orm.Insert(&User{Id: 1, Name: "xlw"})
if err != nil {
fmt.Println(err)
return
}
users := make([]User, 0)
err = orm.Find(&users)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(users)
}
+2 -1
View File
@@ -10,6 +10,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// User describes a user
type User struct {
Id int64
Name string
@@ -24,7 +25,7 @@ func mysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
}
var u *User = &User{}
var u = &User{}
func test(engine *xorm.Engine) {
err := engine.CreateTables(u)
+4 -3
View File
@@ -10,6 +10,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// User describes a user
type User struct {
Id int64
Name string
@@ -24,7 +25,7 @@ func mysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
}
var u *User = &User{}
var u = &User{}
func test(engine *xorm.Engine) {
err := engine.CreateTables(u)
@@ -33,7 +34,7 @@ func test(engine *xorm.Engine) {
return
}
engine.ShowSQL = true
engine.ShowSQL(true)
engine.SetMaxOpenConns(5)
size := 1000
@@ -91,7 +92,7 @@ func main() {
fmt.Println(err)
return
}
engine.ShowSQL = true
engine.ShowSQL(true)
fmt.Println(engine)
test(engine)
fmt.Println("------------------------")
+7 -5
View File
@@ -8,11 +8,13 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// User describes a user
type User struct {
Id int64
Name string
}
// LoginInfo describes a login information
type LoginInfo struct {
Id int64
IP string
@@ -27,26 +29,26 @@ func main() {
f := "singleMapping.db"
os.Remove(f)
Orm, err := xorm.NewEngine("sqlite3", f)
orm, err := xorm.NewEngine("sqlite3", f)
if err != nil {
fmt.Println(err)
return
}
Orm.ShowSQL = true
err = Orm.CreateTables(&User{}, &LoginInfo{})
orm.ShowSQL(true)
err = orm.CreateTables(&User{}, &LoginInfo{})
if err != nil {
fmt.Println(err)
return
}
_, err = Orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1, "", 23})
_, err = orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1, "", 23})
if err != nil {
fmt.Println(err)
return
}
info := LoginInfo{}
_, err = Orm.Id(1).Get(&info)
_, err = orm.Id(1).Get(&info)
if err != nil {
fmt.Println(err)
return
+4 -2
View File
@@ -9,6 +9,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)
// SyncUser2 describes a user
type SyncUser2 struct {
Id int64
Name string `xorm:"unique"`
@@ -20,6 +21,7 @@ type SyncUser2 struct {
Date int
}
// SyncLoginInfo2 describes a login information
type SyncLoginInfo2 struct {
Id int64
IP string `xorm:"index"`
@@ -60,12 +62,12 @@ func main() {
engines := []engineFunc{postgresEngine}
for _, enginefunc := range engines {
Orm, err := enginefunc()
fmt.Println("--------", Orm.DriverName, "----------")
fmt.Println("--------", Orm.DriverName(), "----------")
if err != nil {
fmt.Println(err)
return
}
Orm.ShowSQL = true
Orm.ShowSQL(true)
err = sync(Orm)
if err != nil {
fmt.Println(err)
+1 -1
View File
@@ -20,7 +20,7 @@ func main() {
return
}
defer orm.Close()
orm.ShowSQL = true
orm.ShowSQL(true)
tables, err := orm.DBMetas()
if err != nil {
+214
View File
@@ -0,0 +1,214 @@
package migrate
import (
"errors"
"fmt"
"github.com/go-xorm/xorm"
)
// MigrateFunc is the func signature for migrating.
type MigrateFunc func(*xorm.Engine) error
// RollbackFunc is the func signature for rollbacking.
type RollbackFunc func(*xorm.Engine) error
// InitSchemaFunc is the func signature for initializing the schema.
type InitSchemaFunc func(*xorm.Engine) error
// Options define options for all migrations.
type Options struct {
// TableName is the migration table.
TableName string
// IDColumnName is the name of column where the migration id will be stored.
IDColumnName string
}
// Migration represents a database migration (a modification to be made on the database).
type Migration struct {
// ID is the migration identifier. Usually a timestamp like "201601021504".
ID string
// Migrate is a function that will br executed while running this migration.
Migrate MigrateFunc
// Rollback will be executed on rollback. Can be nil.
Rollback RollbackFunc
}
// Migrate represents a collection of all migrations of a database schema.
type Migrate struct {
db *xorm.Engine
options *Options
migrations []*Migration
initSchema InitSchemaFunc
}
var (
// DefaultOptions can be used if you don't want to think about options.
DefaultOptions = &Options{
TableName: "migrations",
IDColumnName: "id",
}
// ErrRollbackImpossible is returned when trying to rollback a migration
// that has no rollback function.
ErrRollbackImpossible = errors.New("It's impossible to rollback this migration")
// ErrNoMigrationDefined is returned when no migration is defined.
ErrNoMigrationDefined = errors.New("No migration defined")
// ErrMissingID is returned when the ID od migration is equal to ""
ErrMissingID = errors.New("Missing ID in migration")
// ErrNoRunnedMigration is returned when any runned migration was found while
// running RollbackLast
ErrNoRunnedMigration = errors.New("Could not find last runned migration")
)
// New returns a new Gormigrate.
func New(db *xorm.Engine, options *Options, migrations []*Migration) *Migrate {
return &Migrate{
db: db,
options: options,
migrations: migrations,
}
}
// InitSchema sets a function that is run if no migration is found.
// The idea is preventing to run all migrations when a new clean database
// is being migrating. In this function you should create all tables and
// foreign key necessary to your application.
func (m *Migrate) InitSchema(initSchema InitSchemaFunc) {
m.initSchema = initSchema
}
// Migrate executes all migrations that did not run yet.
func (m *Migrate) Migrate() error {
if err := m.createMigrationTableIfNotExists(); err != nil {
return err
}
if m.initSchema != nil && m.isFirstRun() {
if err := m.runInitSchema(); err != nil {
return err
}
return nil
}
for _, migration := range m.migrations {
if err := m.runMigration(migration); err != nil {
return err
}
}
return nil
}
// RollbackLast undo the last migration
func (m *Migrate) RollbackLast() error {
if len(m.migrations) == 0 {
return ErrNoMigrationDefined
}
lastRunnedMigration, err := m.getLastRunnedMigration()
if err != nil {
return err
}
if err := m.RollbackMigration(lastRunnedMigration); err != nil {
return err
}
return nil
}
func (m *Migrate) getLastRunnedMigration() (*Migration, error) {
for i := len(m.migrations) - 1; i >= 0; i-- {
migration := m.migrations[i]
if m.migrationDidRun(migration) {
return migration, nil
}
}
return nil, ErrNoRunnedMigration
}
// RollbackMigration undo a migration.
func (m *Migrate) RollbackMigration(mig *Migration) error {
if mig.Rollback == nil {
return ErrRollbackImpossible
}
if err := mig.Rollback(m.db); err != nil {
return err
}
sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName)
if _, err := m.db.Exec(sql, mig.ID); err != nil {
return err
}
return nil
}
func (m *Migrate) runInitSchema() error {
if err := m.initSchema(m.db); err != nil {
return err
}
for _, migration := range m.migrations {
if err := m.insertMigration(migration.ID); err != nil {
return err
}
}
return nil
}
func (m *Migrate) runMigration(migration *Migration) error {
if len(migration.ID) == 0 {
return ErrMissingID
}
if !m.migrationDidRun(migration) {
if err := migration.Migrate(m.db); err != nil {
return err
}
if err := m.insertMigration(migration.ID); err != nil {
return err
}
}
return nil
}
func (m *Migrate) createMigrationTableIfNotExists() error {
exists, err := m.db.IsTableExist(m.options.TableName)
if err != nil {
return err
}
if exists {
return nil
}
sql := fmt.Sprintf("CREATE TABLE %s (%s VARCHAR(255) PRIMARY KEY)", m.options.TableName, m.options.IDColumnName)
if _, err := m.db.Exec(sql); err != nil {
return err
}
return nil
}
func (m *Migrate) migrationDidRun(mig *Migration) bool {
row := m.db.DB().QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName), mig.ID)
var count int
row.Scan(&count)
return count > 0
}
func (m *Migrate) isFirstRun() bool {
row := m.db.DB().QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", m.options.TableName))
var count int
row.Scan(&count)
return count == 0
}
func (m *Migrate) insertMigration(id string) error {
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?)", m.options.TableName, m.options.IDColumnName)
_, err := m.db.Exec(sql, id)
return err
}