1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 17:30:50 +00:00

adapt migration

This commit is contained in:
Ming Deng 2020-09-02 21:01:54 +08:00
parent 8fc4f8847c
commit bdd8df6751
5 changed files with 367 additions and 28 deletions

View File

@ -66,7 +66,6 @@ const (
var (
// DefaultReadTimeOut is the HTTP read timeout
DefaultReadTimeOut time.Duration
// DefaultWriteTimeOut is the HTTP Write timeout
@ -75,7 +74,6 @@ var (
DefaultMaxHeaderBytes int
// DefaultTimeout is the shutdown server's timeout. default is 60s
DefaultTimeout = grace.DefaultTimeout
)
// NewServer returns a new graceServer.

View File

@ -0,0 +1,198 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package migration
import (
"github.com/astaxie/beego/pkg/client/orm/migration"
)
// Index struct defines the structure of Index Columns
type Index migration.Index
// Unique struct defines a single unique key combination
type Unique migration.Unique
// Column struct defines a single column of a table
type Column migration.Column
// Foreign struct defines a single foreign relationship
type Foreign migration.Foreign
// RenameColumn struct allows renaming of columns
type RenameColumn migration.RenameColumn
// CreateTable creates the table on system
func (m *Migration) CreateTable(tablename, engine, charset string, p ...func()) {
(*migration.Migration)(m).CreateTable(tablename, engine, charset, p...)
}
// AlterTable set the ModifyType to alter
func (m *Migration) AlterTable(tablename string) {
(*migration.Migration)(m).AlterTable(tablename)
}
// NewCol creates a new standard column and attaches it to m struct
func (m *Migration) NewCol(name string) *Column {
return (*Column)((*migration.Migration)(m).NewCol(name))
}
// PriCol creates a new primary column and attaches it to m struct
func (m *Migration) PriCol(name string) *Column {
return (*Column)((*migration.Migration)(m).PriCol(name))
}
// UniCol creates / appends columns to specified unique key and attaches it to m struct
func (m *Migration) UniCol(uni, name string) *Column {
return (*Column)((*migration.Migration)(m).UniCol(uni, name))
}
// ForeignCol creates a new foreign column and returns the instance of column
func (m *Migration) ForeignCol(colname, foreigncol, foreigntable string) (foreign *Foreign) {
return (*Foreign)((*migration.Migration)(m).ForeignCol(colname, foreigncol, foreigntable))
}
// SetOnDelete sets the on delete of foreign
func (foreign *Foreign) SetOnDelete(del string) *Foreign {
(*migration.Foreign)(foreign).SetOnDelete(del)
return foreign
}
// SetOnUpdate sets the on update of foreign
func (foreign *Foreign) SetOnUpdate(update string) *Foreign {
(*migration.Foreign)(foreign).SetOnUpdate(update)
return foreign
}
// Remove marks the columns to be removed.
// it allows reverse m to create the column.
func (c *Column) Remove() {
(*migration.Column)(c).Remove()
}
// SetAuto enables auto_increment of column (can be used once)
func (c *Column) SetAuto(inc bool) *Column {
(*migration.Column)(c).SetAuto(inc)
return c
}
// SetNullable sets the column to be null
func (c *Column) SetNullable(null bool) *Column {
(*migration.Column)(c).SetNullable(null)
return c
}
// SetDefault sets the default value, prepend with "DEFAULT "
func (c *Column) SetDefault(def string) *Column {
(*migration.Column)(c).SetDefault(def)
return c
}
// SetUnsigned sets the column to be unsigned int
func (c *Column) SetUnsigned(unsign bool) *Column {
(*migration.Column)(c).SetUnsigned(unsign)
return c
}
// SetDataType sets the dataType of the column
func (c *Column) SetDataType(dataType string) *Column {
(*migration.Column)(c).SetDataType(dataType)
return c
}
// SetOldNullable allows reverting to previous nullable on reverse ms
func (c *RenameColumn) SetOldNullable(null bool) *RenameColumn {
(*migration.RenameColumn)(c).SetOldNullable(null)
return c
}
// SetOldDefault allows reverting to previous default on reverse ms
func (c *RenameColumn) SetOldDefault(def string) *RenameColumn {
(*migration.RenameColumn)(c).SetOldDefault(def)
return c
}
// SetOldUnsigned allows reverting to previous unsgined on reverse ms
func (c *RenameColumn) SetOldUnsigned(unsign bool) *RenameColumn {
(*migration.RenameColumn)(c).SetOldUnsigned(unsign)
return c
}
// SetOldDataType allows reverting to previous datatype on reverse ms
func (c *RenameColumn) SetOldDataType(dataType string) *RenameColumn {
(*migration.RenameColumn)(c).SetOldDataType(dataType)
return c
}
// SetPrimary adds the columns to the primary key (can only be used any number of times in only one m)
func (c *Column) SetPrimary(m *Migration) *Column {
(*migration.Column)(c).SetPrimary((*migration.Migration)(m))
return c
}
// AddColumnsToUnique adds the columns to Unique Struct
func (unique *Unique) AddColumnsToUnique(columns ...*Column) *Unique {
cls := toNewColumnsArray(columns)
(*migration.Unique)(unique).AddColumnsToUnique(cls...)
return unique
}
// AddColumns adds columns to m struct
func (m *Migration) AddColumns(columns ...*Column) *Migration {
cls := toNewColumnsArray(columns)
(*migration.Migration)(m).AddColumns(cls...)
return m
}
func toNewColumnsArray(columns []*Column) []*migration.Column {
cls := make([]*migration.Column, 0, len(columns))
for _, c := range columns {
cls = append(cls, (*migration.Column)(c))
}
return cls
}
// AddPrimary adds the column to primary in m struct
func (m *Migration) AddPrimary(primary *Column) *Migration {
(*migration.Migration)(m).AddPrimary((*migration.Column)(primary))
return m
}
// AddUnique adds the column to unique in m struct
func (m *Migration) AddUnique(unique *Unique) *Migration {
(*migration.Migration)(m).AddUnique((*migration.Unique)(unique))
return m
}
// AddForeign adds the column to foreign in m struct
func (m *Migration) AddForeign(foreign *Foreign) *Migration {
(*migration.Migration)(m).AddForeign((*migration.Foreign)(foreign))
return m
}
// AddIndex adds the column to index in m struct
func (m *Migration) AddIndex(index *Index) *Migration {
(*migration.Migration)(m).AddIndex((*migration.Index)(index))
return m
}
// RenameColumn allows renaming of columns
func (m *Migration) RenameColumn(from, to string) *RenameColumn {
return (*RenameColumn)((*migration.Migration)(m).RenameColumn(from, to))
}
// GetSQL returns the generated sql depending on ModifyType
func (m *Migration) GetSQL() (sql string) {
return (*migration.Migration)(m).GetSQL()
}

View File

@ -0,0 +1,32 @@
// Package migration enables you to generate migrations back and forth. It generates both migrations.
//
// //Creates a table
// m.CreateTable("tablename","InnoDB","utf8");
//
// //Alter a table
// m.AlterTable("tablename")
//
// Standard Column Methods
// * SetDataType
// * SetNullable
// * SetDefault
// * SetUnsigned (use only on integer types unless produces error)
//
// //Sets a primary column, multiple calls allowed, standard column methods available
// m.PriCol("id").SetAuto(true).SetNullable(false).SetDataType("INT(10)").SetUnsigned(true)
//
// //UniCol Can be used multiple times, allows standard Column methods. Use same "index" string to add to same index
// m.UniCol("index","column")
//
// //Standard Column Initialisation, can call .Remove() after NewCol("") on alter to remove
// m.NewCol("name").SetDataType("VARCHAR(255) COLLATE utf8_unicode_ci").SetNullable(false)
// m.NewCol("value").SetDataType("DOUBLE(8,2)").SetNullable(false)
//
// //Rename Columns , only use with Alter table, doesn't works with Create, prefix standard column methods with "Old" to
// //create a true reversible migration eg: SetOldDataType("DOUBLE(12,3)")
// m.RenameColumn("from","to")...
//
// //Foreign Columns, single columns are only supported, SetOnDelete & SetOnUpdate are available, call appropriately.
// //Supports standard column methods, automatic reverse.
// m.ForeignCol("local_col","foreign_col","foreign_table")
package migration

View File

@ -0,0 +1,111 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package migration is used for migration
//
// The table structure is as follow:
//
// CREATE TABLE `migrations` (
// `id_migration` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'surrogate key',
// `name` varchar(255) DEFAULT NULL COMMENT 'migration name, unique',
// `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'date migrated or rolled back',
// `statements` longtext COMMENT 'SQL statements for this migration',
// `rollback_statements` longtext,
// `status` enum('update','rollback') DEFAULT NULL COMMENT 'update indicates it is a normal migration while rollback means this migration is rolled back',
// PRIMARY KEY (`id_migration`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
package migration
import (
"github.com/astaxie/beego/pkg/client/orm/migration"
)
// const the data format for the bee generate migration datatype
const (
DateFormat = "20060102_150405"
DBDateFormat = "2006-01-02 15:04:05"
)
// Migrationer is an interface for all Migration struct
type Migrationer interface {
Up()
Down()
Reset()
Exec(name, status string) error
GetCreated() int64
}
// Migration defines the migrations by either SQL or DDL
type Migration migration.Migration
// Up implement in the Inheritance struct for upgrade
func (m *Migration) Up() {
(*migration.Migration)(m).Up()
}
// Down implement in the Inheritance struct for down
func (m *Migration) Down() {
(*migration.Migration)(m).Down()
}
// Migrate adds the SQL to the execution list
func (m *Migration) Migrate(migrationType string) {
(*migration.Migration)(m).Migrate(migrationType)
}
// SQL add sql want to execute
func (m *Migration) SQL(sql string) {
(*migration.Migration)(m).SQL(sql)
}
// Reset the sqls
func (m *Migration) Reset() {
(*migration.Migration)(m).Reset()
}
// Exec execute the sql already add in the sql
func (m *Migration) Exec(name, status string) error {
return (*migration.Migration)(m).Exec(name, status)
}
// GetCreated get the unixtime from the Created
func (m *Migration) GetCreated() int64 {
return (*migration.Migration)(m).GetCreated()
}
// Register register the Migration in the map
func Register(name string, m Migrationer) error {
return migration.Register(name, m)
}
// Upgrade upgrade the migration from lasttime
func Upgrade(lasttime int64) error {
return migration.Upgrade(lasttime)
}
// Rollback rollback the migration by the name
func Rollback(name string) error {
return migration.Rollback(name)
}
// Reset reset all migration
// run all migration's down function
func Reset() error {
return migration.Reset()
}
// Refresh first Reset, then Upgrade
func Refresh() error {
return migration.Refresh()
}

View File

@ -31,7 +31,7 @@ type Unique struct {
Columns []*Column
}
//Column struct defines a single column of a table
// Column struct defines a single column of a table
type Column struct {
Name string
Inc string
@ -84,7 +84,7 @@ func (m *Migration) NewCol(name string) *Column {
return col
}
//PriCol creates a new primary column and attaches it to m struct
// PriCol creates a new primary column and attaches it to m struct
func (m *Migration) PriCol(name string) *Column {
col := &Column{Name: name}
m.AddColumns(col)
@ -92,7 +92,7 @@ func (m *Migration) PriCol(name string) *Column {
return col
}
//UniCol creates / appends columns to specified unique key and attaches it to m struct
// UniCol creates / appends columns to specified unique key and attaches it to m struct
func (m *Migration) UniCol(uni, name string) *Column {
col := &Column{Name: name}
m.AddColumns(col)
@ -114,7 +114,7 @@ func (m *Migration) UniCol(uni, name string) *Column {
return col
}
//ForeignCol creates a new foreign column and returns the instance of column
// ForeignCol creates a new foreign column and returns the instance of column
func (m *Migration) ForeignCol(colname, foreigncol, foreigntable string) (foreign *Foreign) {
foreign = &Foreign{ForeignColumn: foreigncol, ForeignTable: foreigntable}
@ -123,25 +123,25 @@ func (m *Migration) ForeignCol(colname, foreigncol, foreigntable string) (foreig
return foreign
}
//SetOnDelete sets the on delete of foreign
// SetOnDelete sets the on delete of foreign
func (foreign *Foreign) SetOnDelete(del string) *Foreign {
foreign.OnDelete = "ON DELETE" + del
return foreign
}
//SetOnUpdate sets the on update of foreign
// SetOnUpdate sets the on update of foreign
func (foreign *Foreign) SetOnUpdate(update string) *Foreign {
foreign.OnUpdate = "ON UPDATE" + update
return foreign
}
//Remove marks the columns to be removed.
//it allows reverse m to create the column.
// Remove marks the columns to be removed.
// it allows reverse m to create the column.
func (c *Column) Remove() {
c.remove = true
}
//SetAuto enables auto_increment of column (can be used once)
// SetAuto enables auto_increment of column (can be used once)
func (c *Column) SetAuto(inc bool) *Column {
if inc {
c.Inc = "auto_increment"
@ -149,7 +149,7 @@ func (c *Column) SetAuto(inc bool) *Column {
return c
}
//SetNullable sets the column to be null
// SetNullable sets the column to be null
func (c *Column) SetNullable(null bool) *Column {
if null {
c.Null = ""
@ -160,13 +160,13 @@ func (c *Column) SetNullable(null bool) *Column {
return c
}
//SetDefault sets the default value, prepend with "DEFAULT "
// SetDefault sets the default value, prepend with "DEFAULT "
func (c *Column) SetDefault(def string) *Column {
c.Default = "DEFAULT " + def
return c
}
//SetUnsigned sets the column to be unsigned int
// SetUnsigned sets the column to be unsigned int
func (c *Column) SetUnsigned(unsign bool) *Column {
if unsign {
c.Unsign = "UNSIGNED"
@ -174,13 +174,13 @@ func (c *Column) SetUnsigned(unsign bool) *Column {
return c
}
//SetDataType sets the dataType of the column
// SetDataType sets the dataType of the column
func (c *Column) SetDataType(dataType string) *Column {
c.DataType = dataType
return c
}
//SetOldNullable allows reverting to previous nullable on reverse ms
// SetOldNullable allows reverting to previous nullable on reverse ms
func (c *RenameColumn) SetOldNullable(null bool) *RenameColumn {
if null {
c.OldNull = ""
@ -191,13 +191,13 @@ func (c *RenameColumn) SetOldNullable(null bool) *RenameColumn {
return c
}
//SetOldDefault allows reverting to previous default on reverse ms
// SetOldDefault allows reverting to previous default on reverse ms
func (c *RenameColumn) SetOldDefault(def string) *RenameColumn {
c.OldDefault = def
return c
}
//SetOldUnsigned allows reverting to previous unsgined on reverse ms
// SetOldUnsigned allows reverting to previous unsgined on reverse ms
func (c *RenameColumn) SetOldUnsigned(unsign bool) *RenameColumn {
if unsign {
c.OldUnsign = "UNSIGNED"
@ -205,19 +205,19 @@ func (c *RenameColumn) SetOldUnsigned(unsign bool) *RenameColumn {
return c
}
//SetOldDataType allows reverting to previous datatype on reverse ms
// SetOldDataType allows reverting to previous datatype on reverse ms
func (c *RenameColumn) SetOldDataType(dataType string) *RenameColumn {
c.OldDataType = dataType
return c
}
//SetPrimary adds the columns to the primary key (can only be used any number of times in only one m)
// SetPrimary adds the columns to the primary key (can only be used any number of times in only one m)
func (c *Column) SetPrimary(m *Migration) *Column {
m.Primary = append(m.Primary, c)
return c
}
//AddColumnsToUnique adds the columns to Unique Struct
// AddColumnsToUnique adds the columns to Unique Struct
func (unique *Unique) AddColumnsToUnique(columns ...*Column) *Unique {
unique.Columns = append(unique.Columns, columns...)
@ -225,7 +225,7 @@ func (unique *Unique) AddColumnsToUnique(columns ...*Column) *Unique {
return unique
}
//AddColumns adds columns to m struct
// AddColumns adds columns to m struct
func (m *Migration) AddColumns(columns ...*Column) *Migration {
m.Columns = append(m.Columns, columns...)
@ -233,38 +233,38 @@ func (m *Migration) AddColumns(columns ...*Column) *Migration {
return m
}
//AddPrimary adds the column to primary in m struct
// AddPrimary adds the column to primary in m struct
func (m *Migration) AddPrimary(primary *Column) *Migration {
m.Primary = append(m.Primary, primary)
return m
}
//AddUnique adds the column to unique in m struct
// AddUnique adds the column to unique in m struct
func (m *Migration) AddUnique(unique *Unique) *Migration {
m.Uniques = append(m.Uniques, unique)
return m
}
//AddForeign adds the column to foreign in m struct
// AddForeign adds the column to foreign in m struct
func (m *Migration) AddForeign(foreign *Foreign) *Migration {
m.Foreigns = append(m.Foreigns, foreign)
return m
}
//AddIndex adds the column to index in m struct
// AddIndex adds the column to index in m struct
func (m *Migration) AddIndex(index *Index) *Migration {
m.Indexes = append(m.Indexes, index)
return m
}
//RenameColumn allows renaming of columns
// RenameColumn allows renaming of columns
func (m *Migration) RenameColumn(from, to string) *RenameColumn {
rename := &RenameColumn{OldName: from, NewName: to}
m.Renames = append(m.Renames, rename)
return rename
}
//GetSQL returns the generated sql depending on ModifyType
// GetSQL returns the generated sql depending on ModifyType
func (m *Migration) GetSQL() (sql string) {
sql = ""
switch m.ModifyType {