Skip to content

Commit

Permalink
feat: database migrate (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
huaxiabuluo authored Oct 9, 2023
1 parent c5ee5cf commit e3ed564
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nebula-graph-studio",
"version": "3.7.0",
"version": "3.8.0",
"description": "nebula-graph-studio",
"private": true,
"repository": {
Expand Down
8 changes: 7 additions & 1 deletion server/api/studio/internal/model/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ func InitDB(config *config.Config, db *gorm.DB) {
}

if config.DB.AutoMigrate {
err := db.AutoMigrate(
migrateTables := []string{"task_infos", "task_effects", "sketches", "schema_snapshots", "favorites", "files", "datasources"}
err := MigrateAlterBID(db, migrateTables)
if err != nil {
zap.L().Fatal(fmt.Sprintf("migrate tables fail: %s", err))
panic(err)
}
err = db.AutoMigrate(
&Datasource{},
&TaskInfo{},
&TaskEffect{},
Expand Down
30 changes: 30 additions & 0 deletions server/api/studio/internal/model/db_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package db

import (
"gorm.io/gorm"
)

// Add the `b_id“ field to certain database tables for versions prior to v3.7 (inclusive).
// We need to perform this operation manually, otherwise the `db.AutoMigrate` method will throw an exception
func MigrateAlterBID(db *gorm.DB, tables []string) error {
for _, table := range tables {
isTableExist := db.Migrator().HasTable(table)
// if table not exists, skip
if !isTableExist {
continue
}
isColumnExist := db.Migrator().HasColumn(table, "b_id")
if isColumnExist {
continue
}
err := db.Exec("ALTER TABLE `" + table + "` ADD COLUMN `b_id` CHAR(32) NOT NULL DEFAULT ''").Error
if err != nil {
return err
}
err = db.Exec("UPDATE `" + table + "` SET `b_id` = `id`").Error
if err != nil {
return err
}
}
return nil
}

0 comments on commit e3ed564

Please sign in to comment.