Răsfoiți Sursa

Add migrator

Jinzhu 4 ani în urmă
părinte
comite
8eae7e4ab9
6 a modificat fișierele cu 112 adăugiri și 0 ștergeri
  1. 1 0
      .gitignore
  2. 2 0
      go.mod
  3. 46 0
      gorm.go
  4. 5 0
      logger/logger.go
  5. 44 0
      migrator.go
  6. 14 0
      migrator/migrator.go

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+TODO
 documents
 coverage.txt
 _book

+ 2 - 0
go.mod

@@ -1 +1,3 @@
 module github.com/jinzhu/gorm
+
+go 1.13

+ 46 - 0
gorm.go

@@ -0,0 +1,46 @@
+package gorm
+
+import (
+	"time"
+
+	"github.com/jinzhu/gorm/logger"
+)
+
+// Config GORM config
+type Config struct {
+	// Set true to use singular table name, by default, GORM will pluralize your struct's name as table name
+	// Refer https://github.com/jinzhu/inflection for inflection rules
+	SingularTable bool
+
+	// GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
+	// You can cancel it by setting `SkipDefaultTransaction` to true
+	SkipDefaultTransaction bool
+
+	// Logger
+	Logger logger.Interface
+
+	// NowFunc the function to be used when creating a new timestamp
+	NowFunc func() time.Time
+}
+
+// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
+// It may be embeded into your model or you may build your own model without it
+//    type User struct {
+//      gorm.Model
+//    }
+type Model struct {
+	ID        uint `gorm:"primary_key"`
+	CreatedAt time.Time
+	UpdatedAt time.Time
+	DeletedAt *time.Time `gorm:"index"`
+}
+
+// Dialector GORM database dialector
+type Dialector interface {
+	Migrator() Migrator
+}
+
+// DB GORM DB definition
+type DB struct {
+	*Config
+}

+ 5 - 0
logger/logger.go

@@ -0,0 +1,5 @@
+package logger
+
+// Interface logger interface
+type Interface interface {
+}

+ 44 - 0
migrator.go

@@ -0,0 +1,44 @@
+package gorm
+
+import (
+	"database/sql"
+)
+
+// ViewOption view option
+type ViewOption struct {
+	Replace     bool
+	CheckOption string
+	Query       *DB
+}
+
+type Migrator interface {
+	// AutoMigrate
+	AutoMigrate(dst ...interface{}) error
+
+	// Tables
+	CreateTable(dst ...interface{}) error
+	DropTable(dst ...interface{}) error
+	HasTable(dst ...interface{}) error
+	RenameTable(oldName, newName string) error
+
+	// Columns
+	AddColumn(dst interface{}, field string) error
+	DropColumn(dst interface{}, field string) error
+	AlterColumn(dst interface{}, field string) error
+	RenameColumn(dst interface{}, oldName, field string) error
+	ColumnTypes(dst interface{}) ([]*sql.ColumnType, error)
+
+	// Views
+	CreateView(name string, option ViewOption) error
+	DropView(name string) error
+
+	// Constraints
+	CreateConstraint(dst interface{}, name string) error
+	DropConstraint(dst interface{}, name string) error
+
+	// Indexes
+	CreateIndex(dst interface{}, name string) error
+	DropIndex(dst interface{}, name string) error
+	HasIndex(dst interface{}, name string) error
+	RenameIndex(dst interface{}, oldName, newName string) error
+}

+ 14 - 0
migrator/migrator.go

@@ -0,0 +1,14 @@
+package migrator
+
+import "github.com/jinzhu/gorm"
+
+// Migrator migrator struct
+type Migrator struct {
+	*Config
+}
+
+// Config schema config
+type Config struct {
+	CheckExistsBeforeDropping bool
+	DB                        *gorm.DB
+}