diff --git a/main.go b/main.go index a160cbd3..c5d7ab8e 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,6 @@ func main() { object.InitFlag() object.InitAdapter() object.CreateTables() - object.DoMigration() object.InitDb() object.InitFromFile() diff --git a/object/migrator.go b/object/migrator.go deleted file mode 100644 index 68766412..00000000 --- a/object/migrator.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2023 The Casdoor Authors. 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 object - -import "github.com/xorm-io/xorm/migrate" - -type Migrator interface { - IsMigrationNeeded() bool - DoMigration() *migrate.Migration -} - -func DoMigration() { - migrators := []Migrator{ - &Migrator_1_101_0_PR_1083{}, - &Migrator_1_235_0_PR_1530{}, - &Migrator_1_240_0_PR_1539{}, - &Migrator_1_314_0_PR_1841{}, - // more migrators add here in chronological order... - } - - migrations := []*migrate.Migration{} - - for _, migrator := range migrators { - if migrator.IsMigrationNeeded() { - migrations = append(migrations, migrator.DoMigration()) - } - } - - options := &migrate.Options{ - TableName: "migration", - IDColumnName: "id", - } - - m := migrate.New(ormer.Engine, options, migrations) - err := m.Migrate() - if err != nil { - panic(err) - } -} diff --git a/object/migrator_1_101_0_PR_1083.go b/object/migrator_1_101_0_PR_1083.go deleted file mode 100644 index a778e3c2..00000000 --- a/object/migrator_1_101_0_PR_1083.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2023 The Casdoor Authors. 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 object - -import ( - "strings" - - "github.com/xorm-io/xorm" - "github.com/xorm-io/xorm/migrate" -) - -type Migrator_1_101_0_PR_1083 struct{} - -func (*Migrator_1_101_0_PR_1083) IsMigrationNeeded() bool { - exist1, _ := ormer.Engine.IsTableExist("model") - exist2, _ := ormer.Engine.IsTableExist("permission") - exist3, _ := ormer.Engine.IsTableExist("permission_rule") - - if exist1 && exist2 && exist3 { - return true - } - return false -} - -func (*Migrator_1_101_0_PR_1083) DoMigration() *migrate.Migration { - migration := migrate.Migration{ - ID: "20230209MigratePermissionRule--Use V5 instead of V1 to store permissionID", - Migrate: func(engine *xorm.Engine) error { - models := []*Model{} - err := engine.Table("model").Find(&models, &Model{}) - if err != nil { - panic(err) - } - - isHit := false - for _, model := range models { - if strings.Contains(model.ModelText, "permission") { - // update model table - model.ModelText = strings.Replace(model.ModelText, "permission,", "", -1) - UpdateModel(model.GetId(), model) - isHit = true - } - } - - if isHit { - // update permission_rule table - sql := "UPDATE `permission_rule`SET V0 = V1, V1 = V2, V2 = V3, V3 = V4, V4 = V5 WHERE V0 IN (SELECT CONCAT(owner, '/', name) AS permission_id FROM `permission`)" - _, err = engine.Exec(sql) - if err != nil { - return err - } - } - return err - }, - } - - return &migration -} diff --git a/object/migrator_1_235_0_PR_1530.go b/object/migrator_1_235_0_PR_1530.go deleted file mode 100644 index f43aef39..00000000 --- a/object/migrator_1_235_0_PR_1530.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2023 The Casdoor Authors. 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 object - -import ( - xormadapter "github.com/casdoor/xorm-adapter/v3" - "github.com/xorm-io/xorm" - "github.com/xorm-io/xorm/migrate" -) - -type Migrator_1_235_0_PR_1530 struct{} - -func (*Migrator_1_235_0_PR_1530) IsMigrationNeeded() bool { - exist, _ := ormer.Engine.IsTableExist("casbin_rule") - - return exist -} - -func (*Migrator_1_235_0_PR_1530) DoMigration() *migrate.Migration { - migration := migrate.Migration{ - ID: "20221015CasbinRule--fill ptype field with p", - Migrate: func(engine *xorm.Engine) error { - _, err := engine.Cols("ptype").Update(&xormadapter.CasbinRule{ - Ptype: "p", - }) - return err - }, - Rollback: func(engine *xorm.Engine) error { - return engine.DropTables(&xormadapter.CasbinRule{}) - }, - } - - return &migration -} diff --git a/object/migrator_1_240_0_PR_1539.go b/object/migrator_1_240_0_PR_1539.go deleted file mode 100644 index c99b11c6..00000000 --- a/object/migrator_1_240_0_PR_1539.go +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2023 The Casdoor Authors. 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 object - -import ( - "errors" - - "github.com/xorm-io/xorm" - "github.com/xorm-io/xorm/migrate" -) - -type Migrator_1_240_0_PR_1539 struct{} - -func (*Migrator_1_240_0_PR_1539) IsMigrationNeeded() bool { - exist, _ := ormer.Engine.IsTableExist("session") - err := ormer.Engine.Table("session").Find(&[]*Session{}) - - if exist && err != nil { - return true - } - return false -} - -func (*Migrator_1_240_0_PR_1539) DoMigration() *migrate.Migration { - migration := migrate.Migration{ - ID: "20230211MigrateSession--Create a new field 'application' for table `session`", - Migrate: func(engine *xorm.Engine) error { - if alreadyCreated, _ := engine.IsTableExist("session_tmp"); alreadyCreated { - return errors.New("there is already a table called 'session_tmp', please rename or delete it for casdoor version migration and restart") - } - - type oldSession struct { - Owner string `xorm:"varchar(100) notnull pk" json:"owner"` - Name string `xorm:"varchar(100) notnull pk" json:"name"` - CreatedTime string `xorm:"varchar(100)" json:"createdTime"` - - SessionId []string `json:"sessionId"` - } - - tx := engine.NewSession() - - defer tx.Close() - - err := tx.Begin() - if err != nil { - return err - } - - err = tx.Table("session_tmp").CreateTable(&Session{}) - if err != nil { - return err - } - - oldSessions := []*oldSession{} - newSessions := []*Session{} - - err = tx.Table("session").Find(&oldSessions) - if err != nil { - return err - } - - for _, oldSession := range oldSessions { - newApplication := "null" - if oldSession.Owner == "built-in" { - newApplication = "app-built-in" - } - newSessions = append(newSessions, &Session{ - Owner: oldSession.Owner, - Name: oldSession.Name, - Application: newApplication, - CreatedTime: oldSession.CreatedTime, - SessionId: oldSession.SessionId, - }) - } - - rollbackFlag := false - _, err = tx.Table("session_tmp").Insert(newSessions) - count1, _ := tx.Table("session_tmp").Count() - count2, _ := tx.Table("session").Count() - - if err != nil || count1 != count2 { - rollbackFlag = true - } - - delete := &Session{ - Application: "null", - } - _, err = tx.Table("session_tmp").Delete(*delete) - if err != nil { - rollbackFlag = true - } - - if rollbackFlag { - tx.DropTable("session_tmp") - return errors.New("there is something wrong with data migration for table `session`, if there is a table called `session_tmp` not created by you in casdoor, please drop it, then restart anyhow") - } - - err = tx.DropTable("session") - if err != nil { - return errors.New("fail to drop table `session` for casdoor, please drop it and rename the table `session_tmp` to `session` manually and restart") - } - - // Already drop table `session` - // Can't find an api from xorm for altering table name - err = tx.Table("session").CreateTable(&Session{}) - if err != nil { - return errors.New("there is something wrong with data migration for table `session`, please restart") - } - - sessions := []*Session{} - tx.Table("session_tmp").Find(&sessions) - _, err = tx.Table("session").Insert(sessions) - if err != nil { - return errors.New("there is something wrong with data migration for table `session`, please drop table `session` and rename table `session_tmp` to `session` and restart") - } - - err = tx.DropTable("session_tmp") - if err != nil { - return errors.New("fail to drop table `session_tmp` for casdoor, please drop it manually and restart") - } - - tx.Commit() - - return nil - }, - } - - return &migration -} diff --git a/object/migrator_1_314_0_PR_1841.go b/object/migrator_1_314_0_PR_1841.go deleted file mode 100644 index 75bedace..00000000 --- a/object/migrator_1_314_0_PR_1841.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2023 The Casdoor Authors. 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 object - -import ( - "github.com/xorm-io/xorm" - "github.com/xorm-io/xorm/migrate" -) - -type Migrator_1_314_0_PR_1841 struct{} - -func (*Migrator_1_314_0_PR_1841) IsMigrationNeeded() bool { - count, err := ormer.Engine.Where("password_type=?", "").Count(&User{}) - if err != nil { - // table doesn't exist - return false - } - - return count > 100 -} - -func (*Migrator_1_314_0_PR_1841) DoMigration() *migrate.Migration { - migration := migrate.Migration{ - ID: "20230515MigrateUser--Create a new field 'passwordType' for table `user`", - Migrate: func(engine *xorm.Engine) error { - tx := engine.NewSession() - - defer tx.Close() - - err := tx.Begin() - if err != nil { - return err - } - - organizations := []*Organization{} - err = tx.Table("organization").Find(&organizations) - if err != nil { - return err - } - - for _, organization := range organizations { - user := &User{PasswordType: organization.PasswordType} - _, err = tx.Where("owner = ?", organization.Name).Cols("password_type").Update(user) - if err != nil { - return err - } - } - - tx.Commit() - - return nil - }, - } - - return &migration -} diff --git a/object/ormer.go b/object/ormer.go index 604db4ca..645f72a3 100644 --- a/object/ormer.go +++ b/object/ormer.go @@ -64,7 +64,6 @@ func InitConfig() { InitAdapter() CreateTables() - DoMigration() } func InitAdapter() {