Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: update all entity fields

LinkinStar80fccefa3d3a
diff --git a/cmd/answer/command.go b/cmd/answer/command.go
index 83669fcc..d0f76408 100644
--- a/cmd/answer/command.go
+++ b/cmd/answer/command.go
@@ -4,7 +4,6 @@ import (
"fmt"
"os"
- "github.com/segmentfault/answer/internal/base/data"
"github.com/segmentfault/answer/internal/cli"
"github.com/segmentfault/answer/internal/migrations"
"github.com/spf13/cobra"
@@ -69,7 +68,7 @@ To run answer, use:
return
}
fmt.Println("read config successfully")
- if err := cli.InitDB(c.Data.Database); err != nil {
+ if err := migrations.InitDB(c.Data.Database); err != nil {
fmt.Println("init database error: ", err.Error())
return
}
@@ -88,13 +87,7 @@ To run answer, use:
fmt.Println("read config failed: ", err.Error())
return
}
- fmt.Println("read config successfully")
- db, err := data.NewDB(false, c.Data.Database)
- if err != nil {
- fmt.Println("new database failed: ", err.Error())
- return
- }
- if err = migrations.Migrate(db); err != nil {
+ if err = migrations.Migrate(c.Data.Database); err != nil {
fmt.Println("migrate failed: ", err.Error())
return
}
diff --git a/internal/entity/activity_entity.go b/internal/entity/activity_entity.go
index a1edac85..7ae96889 100644
--- a/internal/entity/activity_entity.go
+++ b/internal/entity/activity_entity.go
@@ -9,12 +9,12 @@ const (
// Activity activity
type Activity struct {
- ID string `xorm:"not null pk autoincr comment('Activity TagID autoincrement') BIGINT(20) id"`
+ ID string `xorm:"not null pk autoincr comment('activity id') BIGINT(20) id"`
CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
UserID string `xorm:"not null comment('the user ID that generated the activity or affected by the activity') index BIGINT(20) user_id"`
- TriggerUserID int64 `xorm:"not null default 0 comment('the trigger user TagID that generated the activity or affected by the activity') index BIGINT(20) trigger_user_id"`
- ObjectID string `xorm:"not null default 0 comment('the object TagID that affected by the activity') index BIGINT(20) object_id"`
+ TriggerUserID int64 `xorm:"not null default 0 comment('the trigger user ID that generated the activity or affected by the activity') index BIGINT(20) trigger_user_id"`
+ ObjectID string `xorm:"not null default 0 comment('the object ID that affected by the activity') index BIGINT(20) object_id"`
ActivityType int `xorm:"not null comment('activity type, correspond to config id') INT(11) activity_type"`
Cancelled int `xorm:"not null default 0 comment('mark this activity if cancelled or not,default 0(not cancelled)') TINYINT(4) cancelled"`
Rank int `xorm:"not null default 0 comment('rank of current operating user affected') INT(11) rank"`
diff --git a/internal/entity/answer_entity.go b/internal/entity/answer_entity.go
index faf54801..bf828470 100644
--- a/internal/entity/answer_entity.go
+++ b/internal/entity/answer_entity.go
@@ -19,17 +19,17 @@ var CmsAnswerSearchStatus = map[string]int{
// Answer answer
type Answer struct {
ID string `xorm:"not null pk autoincr comment('answer id') BIGINT(20) id"`
- CreatedAt time.Time `xorm:"created not null default CURRENT_TIMESTAMP TIMESTAMP created_at"`
- UpdatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP updated_at"`
+ CreatedAt time.Time `xorm:"created comment('create time') not null default CURRENT_TIMESTAMP TIMESTAMP created_at"`
+ UpdatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP comment('update time') TIMESTAMP updated_at"`
QuestionID string `xorm:"not null default 0 comment('question id') BIGINT(20) question_id"`
- UserID string `xorm:"not null default 0 comment('answer user id') BIGINT(20) user_id"`
- OriginalText string `xorm:"not null comment('original content') MEDIUMTEXT original_text"`
- ParsedText string `xorm:"not null comment('parsed content') MEDIUMTEXT parsed_text"`
- Status int `xorm:"not null default 1 comment(' answer status(available: 1; deleted: 10)') INT(11) status"`
+ UserID string `xorm:"not null default 0 comment('answer user id') BIGINT(20) INDEX user_id"`
+ OriginalText string `xorm:"not null comment('original text') MEDIUMTEXT original_text"`
+ ParsedText string `xorm:"not null comment('parsed text') MEDIUMTEXT parsed_text"`
+ Status int `xorm:"not null default 1 comment('answer status(available: 1; deleted: 10)') INT(11) status"`
Adopted int `xorm:"not null default 1 comment('adopted (1 failed 2 adopted)') INT(11) adopted"`
CommentCount int `xorm:"not null default 0 comment('comment count') INT(11) comment_count"`
VoteCount int `xorm:"not null default 0 comment('vote count') INT(11) vote_count"`
- RevisionID string `xorm:"not null default 0 BIGINT(20) revision_id"`
+ RevisionID string `xorm:"not null default 0 comment('revision id') BIGINT(20) revision_id"`
}
type AnswerSearch struct {
diff --git a/internal/entity/collection_entity.go b/internal/entity/collection_entity.go
index ccef2d43..4ade11e9 100644
--- a/internal/entity/collection_entity.go
+++ b/internal/entity/collection_entity.go
@@ -2,16 +2,14 @@ package entity
import "time"
-const ()
-
// Collection collection
type Collection struct {
ID string `xorm:"not null pk default 0 comment('collection id') BIGINT(20) id"`
- UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) user_id"`
+ CreatedAt time.Time `xorm:"created not null default CURRENT_TIMESTAMP comment('created time') TIMESTAMP created_at"`
+ UpdatedAt time.Time `xorm:"updated not null default CURRENT_TIMESTAMP comment('updated time') TIMESTAMP updated_at"`
+ UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) INDEX user_id"`
ObjectID string `xorm:"not null default 0 comment('object id') BIGINT(20) object_id"`
UserCollectionGroupID string `xorm:"not null default 0 comment('user collection group id') BIGINT(20) user_collection_group_id"`
- CreatedAt time.Time `xorm:"created not null default CURRENT_TIMESTAMP TIMESTAMP created_at"`
- UpdatedAt time.Time `xorm:"updated not null default CURRENT_TIMESTAMP TIMESTAMP updated_at"`
}
type CollectionSearch struct {
diff --git a/internal/entity/collection_group_entity.go b/internal/entity/collection_group_entity.go
index 85fb3e3d..7c9b5d69 100644
--- a/internal/entity/collection_group_entity.go
+++ b/internal/entity/collection_group_entity.go
@@ -4,12 +4,12 @@ import "time"
// CollectionGroup collection group
type CollectionGroup struct {
- ID string `xorm:"not null pk autoincr BIGINT(20) id"`
- UserID string `xorm:"not null default 0 BIGINT(20) user_id"`
+ ID string `xorm:"not null pk autoincr comment('id') BIGINT(20) id"`
+ CreatedAt time.Time `xorm:"created not null default CURRENT_TIMESTAMP comment('created time') TIMESTAMP created_at"`
+ UpdatedAt time.Time `xorm:"updated not null default CURRENT_TIMESTAMP comment('updated time') TIMESTAMP updated_at"`
+ UserID string `xorm:"not null default 0 BIGINT(20) comment('user id') INDEX user_id"`
Name string `xorm:"not null default '' comment('the collection group name') VARCHAR(50) name"`
DefaultGroup int `xorm:"not null default 1 comment('mark this group is default, default 1') INT(11) default_group"`
- CreatedAt time.Time `xorm:"created not null default CURRENT_TIMESTAMP TIMESTAMP created_at"`
- UpdatedAt time.Time `xorm:"updated not null default CURRENT_TIMESTAMP TIMESTAMP updated_at"`
}
// TableName collection group table name
diff --git a/internal/entity/comment_entity.go b/internal/entity/comment_entity.go
index 53f145ae..caebc6c9 100644
--- a/internal/entity/comment_entity.go
+++ b/internal/entity/comment_entity.go
@@ -21,7 +21,7 @@ type Comment struct {
UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) user_id"`
ReplyUserID sql.NullInt64 `xorm:"comment('reply user id') BIGINT(20) reply_user_id"`
ReplyCommentID sql.NullInt64 `xorm:"comment('reply comment id') BIGINT(20) reply_comment_id"`
- ObjectID string `xorm:"not null default 0 comment('object id') BIGINT(20) object_id"`
+ ObjectID string `xorm:"not null default 0 comment('object id') BIGINT(20) INDEX object_id"`
QuestionID string `xorm:"not null default 0 comment('question id') BIGINT(20) question_id"`
VoteCount int `xorm:"not null default 0 comment('user vote amount') INT(11) vote_count"`
Status int `xorm:"not null default 0 comment('comment status(available: 1; deleted: 10)') TINYINT(4) status"`
diff --git a/internal/entity/config_entity.go b/internal/entity/config_entity.go
index 87a27213..f50a4182 100644
--- a/internal/entity/config_entity.go
+++ b/internal/entity/config_entity.go
@@ -4,7 +4,7 @@ package entity
type Config struct {
ID int `xorm:"not null pk autoincr comment('config id') INT(11) id"`
Key string `xorm:"comment('the config key') unique VARCHAR(32) key"`
- Value string `xorm:"comment('the config value, custom data structures and types') VARCHAR(128) value"`
+ Value string `xorm:"comment('the config value, custom data structures and types') TEXT value"`
}
// TableName config table name
diff --git a/internal/entity/meta_entity.go b/internal/entity/meta_entity.go
index 341c1bff..2ab3e5e3 100644
--- a/internal/entity/meta_entity.go
+++ b/internal/entity/meta_entity.go
@@ -14,7 +14,7 @@ type Meta struct {
ID int `xorm:"not null pk autoincr comment('id') INT(10) id"`
CreatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP created comment('created time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP updated comment('updated time') TIMESTAMP updated_at"`
- ObjectID string `xorm:"not null default 0 comment('object id') BIGINT(20) object_id"`
+ ObjectID string `xorm:"not null default 0 comment('object id') INDEX BIGINT(20) object_id"`
Key string `xorm:"not null comment('key') VARCHAR(100) key"`
Value string `xorm:"not null comment('value') MEDIUMTEXT value"`
}
diff --git a/internal/entity/notification_entity.go b/internal/entity/notification_entity.go
index 5f6b5e93..4fb18ad2 100644
--- a/internal/entity/notification_entity.go
+++ b/internal/entity/notification_entity.go
@@ -5,14 +5,14 @@ import "time"
// Notification notification
type Notification struct {
ID string `xorm:"not null pk autoincr comment('notification id') BIGINT(20) id"`
- UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) user_id"`
- ObjectID string `xorm:"not null default 0 comment('object id') index BIGINT(20) object_id"`
+ CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
+ UpdatedAt time.Time `xorm:"comment('update time') TIMESTAMP updated_at"`
+ UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) INDEX user_id"`
+ ObjectID string `xorm:"not null default 0 comment('object id') INDEX BIGINT(20) object_id"`
Content string `xorm:"not null comment('notification content') TEXT content"`
- Type int `xorm:"not null default 0 comment('notification type( 1 inbox 2 achievement)') INT(11) type"`
+ Type int `xorm:"not null default 0 comment('notification type(1:inbox; 2:achievement)') INT(11) type"`
IsRead int `xorm:"not null default 1 comment('read status(unread: 1; read 2)') INT(11) is_read"`
Status int `xorm:"not null default 1 comment('notification status(normal: 1; delete 2)') INT(11) status"`
- CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
- UpdatedAt time.Time `xorm:"comment('update time') TIMESTAMP updated_at"`
}
// TableName notification table name
diff --git a/internal/entity/question_entity.go b/internal/entity/question_entity.go
index 0a02c50d..c434ca8c 100644
--- a/internal/entity/question_entity.go
+++ b/internal/entity/question_entity.go
@@ -32,11 +32,11 @@ type Question struct {
ID string `xorm:"not null pk comment('question id') BIGINT(20) id"`
CreatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP comment('update time') TIMESTAMP updated_at"`
- UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) user_id"`
- Title string `xorm:"not null default '' comment('question title') VARCHAR(255) title"`
- OriginalText string `xorm:"not null comment('original content') MEDIUMTEXT original_text"`
- ParsedText string `xorm:"not null comment('parsed content') MEDIUMTEXT parsed_text"`
- Status int `xorm:"not null default 1 comment(' question status(available: 1; deleted: 10)') INT(11) status"`
+ UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) INDEX user_id"`
+ Title string `xorm:"not null default '' comment('question title') VARCHAR(150) title"`
+ OriginalText string `xorm:"not null comment('original text') MEDIUMTEXT original_text"`
+ ParsedText string `xorm:"not null comment('parsed text') MEDIUMTEXT parsed_text"`
+ Status int `xorm:"not null default 1 comment('question status(available: 1; deleted: 10)') INT(11) status"`
ViewCount int `xorm:"not null default 0 comment('view count') INT(11) view_count"`
UniqueViewCount int `xorm:"not null default 0 comment('unique view count') INT(11) unique_view_count"`
VoteCount int `xorm:"not null default 0 comment('vote count') INT(11) vote_count"`
@@ -46,7 +46,7 @@ type Question struct {
AcceptedAnswerID string `xorm:"not null default 0 comment('accepted answer id') BIGINT(20) accepted_answer_id"`
LastAnswerID string `xorm:"not null default 0 comment('last answer id') BIGINT(20) last_answer_id"`
PostUpdateTime time.Time `xorm:"default CURRENT_TIMESTAMP comment('answer the last update time') TIMESTAMP post_update_time"`
- RevisionID string `xorm:"not null default 0 BIGINT(20) revision_id"`
+ RevisionID string `xorm:"not null default 0 comment('revision id') BIGINT(20) revision_id"`
}
// TableName question table name
diff --git a/internal/entity/report_entity.go b/internal/entity/report_entity.go
index e50ef260..b4dde541 100644
--- a/internal/entity/report_entity.go
+++ b/internal/entity/report_entity.go
@@ -22,14 +22,14 @@ type Report struct {
CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
UserID string `xorm:"not null comment('reporter user id') BIGINT(20) user_id"`
- ReportedUserID string `xorm:"not null comment('reported user id') BIGINT(20) reported_user_id"`
ObjectID string `xorm:"not null comment('object id') BIGINT(20) object_id"`
+ ReportedUserID string `xorm:"not null default 0 comment('reported user id') BIGINT(20) reported_user_id"`
ObjectType int `xorm:"not null default 0 comment('revision type') INT(11) object_type"`
ReportType int `xorm:"not null default 0 comment('report type') INT(11) report_type"`
Content string `xorm:"not null comment('report content') TEXT content"`
- FlaggedType int `xorm:"not null default 0 comment('flaged type') INT(11) flaged_type"`
- FlaggedContent string `xorm:"not null comment('flaged content') TEXT flaged_content"`
- Status int `xorm:"not null default 1 comment('status(normal: 1; delete 2)') INT(11) status"`
+ FlaggedType int `xorm:"not null default 0 comment('flagged type') INT(11) flagged_type"`
+ FlaggedContent string `xorm:"comment('flagged content') TEXT flagged_content"`
+ Status int `xorm:"not null default 1 comment('status(normal: 1; pending:2; delete: 10)') INT(11) status"`
}
// TableName report table name
diff --git a/internal/entity/revision_entity.go b/internal/entity/revision_entity.go
index 35ea1b0c..7c811fb9 100644
--- a/internal/entity/revision_entity.go
+++ b/internal/entity/revision_entity.go
@@ -9,7 +9,7 @@ type Revision struct {
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
UserID string `xorm:"not null default 0 comment('user id') BIGINT(20) user_id"`
ObjectType int `xorm:"not null default 0 comment('revision type(question: 1; answer 2; tag 3)') INT(11) object_type"`
- ObjectID string `xorm:"not null default 0 comment('object id') BIGINT(20) object_id"`
+ ObjectID string `xorm:"not null default 0 comment('object id') BIGINT(20) INDEX object_id"`
Title string `xorm:"not null default '' comment('title') VARCHAR(255) title"`
Content string `xorm:"not null comment('content') TEXT content"`
Log string `xorm:"comment('log') VARCHAR(255) log"`
diff --git a/internal/entity/site_info.go b/internal/entity/site_info.go
index 8e01ee6d..55a43150 100644
--- a/internal/entity/site_info.go
+++ b/internal/entity/site_info.go
@@ -7,7 +7,7 @@ type SiteInfo struct {
ID string `xorm:"not null pk autoincr comment('id') INT(11) id"`
CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
- Type string `xorm:"not null comment('content') VARCHAR(64) type"`
+ Type string `xorm:"not null comment('type') VARCHAR(64) type"`
Content string `xorm:"not null comment('content') MEDIUMTEXT content"`
Status int `xorm:"not null default 1 comment('site info status(available: 1; deleted: 10)') INT(11) status"`
}
diff --git a/internal/entity/tag_entity.go b/internal/entity/tag_entity.go
index 0a682e20..66f45dde 100644
--- a/internal/entity/tag_entity.go
+++ b/internal/entity/tag_entity.go
@@ -9,19 +9,19 @@ const (
// Tag tag
type Tag struct {
- ID string `xorm:"not null comment('tag_id') BIGINT(20) id"`
+ ID string `xorm:"not null pk comment('tag_id') BIGINT(20) id"`
CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
MainTagID int64 `xorm:"not null default 0 comment('main tag id') BIGINT(20) main_tag_id"`
- MainTagSlugName string `xorm:"default '' comment('main_tag_slug_name') VARCHAR(50) main_tag_slug_name"`
- SlugName string `xorm:"default '' comment('slug_name') unique VARCHAR(50) slug_name"`
- DisplayName string `xorm:"not null default '' comment('display_name') VARCHAR(50) display_name"`
+ MainTagSlugName string `xorm:"not null default '' comment('main tag slug name') VARCHAR(35) main_tag_slug_name"`
+ SlugName string `xorm:"not null default '' comment('slug name') unique VARCHAR(35) slug_name"`
+ DisplayName string `xorm:"not null default '' comment('display name') VARCHAR(35) display_name"`
OriginalText string `xorm:"not null comment('original comment content') MEDIUMTEXT original_text"`
ParsedText string `xorm:"not null comment('parsed comment content') MEDIUMTEXT parsed_text"`
FollowCount int `xorm:"not null default 0 comment('associated follow count') INT(11) follow_count"`
QuestionCount int `xorm:"not null default 0 comment('associated question count') INT(11) question_count"`
Status int `xorm:"not null default 1 comment('tag status(available: 1; deleted: 10)') INT(11) status"`
- RevisionID string `xorm:"not null default 0 BIGINT(20) revision_id"`
+ RevisionID string `xorm:"not null default 0 comment('revision id') BIGINT(20) revision_id"`
}
// TableName tag table name
diff --git a/internal/entity/tag_rel_entity.go b/internal/entity/tag_rel_entity.go
index ac778fe8..e66a5318 100644
--- a/internal/entity/tag_rel_entity.go
+++ b/internal/entity/tag_rel_entity.go
@@ -12,8 +12,8 @@ type TagRel struct {
ID int64 `xorm:"not null pk autoincr comment('tag_list_id') BIGINT(20) id"`
CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
- ObjectID string `xorm:"not null comment('object_id') index BIGINT(20) object_id"`
- TagID string `xorm:"not null comment('tag_id') index BIGINT(20) tag_id"`
+ ObjectID string `xorm:"not null comment('object_id') INDEX UNIQUE(s) BIGINT(20) object_id"`
+ TagID string `xorm:"not null comment('tag_id') INDEX UNIQUE(s) BIGINT(20) tag_id"`
Status int `xorm:"not null default 1 comment('tag_list_status(available: 1; deleted: 10)') INT(11) status"`
}
diff --git a/internal/entity/user_entity.go b/internal/entity/user_entity.go
index cdb11bd4..981e5dd5 100644
--- a/internal/entity/user_entity.go
+++ b/internal/entity/user_entity.go
@@ -24,8 +24,8 @@ type User struct {
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
SuspendedAt time.Time `xorm:"comment('suspended time') TIMESTAMP suspended_at"`
DeletedAt time.Time `xorm:"comment('delete time') TIMESTAMP deleted_at"`
- LastLoginDate time.Time `xorm:"comment('last_login_date') TIMESTAMP last_login_date"`
- Username string `xorm:"not null default '' comment('username') VARCHAR(50) username"`
+ LastLoginDate time.Time `xorm:"comment('last login date') TIMESTAMP last_login_date"`
+ Username string `xorm:"not null default '' comment('username') VARCHAR(50) UNIQUE username"`
Pass string `xorm:"not null default '' comment('password') VARCHAR(255) pass"`
EMail string `xorm:"not null comment('email') VARCHAR(100) e_mail"`
MailStatus int `xorm:"not null default 2 comment('mail status(1 pass 2 to be verified)') TINYINT(4) mail_status"`
diff --git a/internal/migrations/init.go b/internal/migrations/init.go
index 97bd8454..84e89ff0 100644
--- a/internal/migrations/init.go
+++ b/internal/migrations/init.go
@@ -3,45 +3,52 @@ package migrations
import (
"fmt"
+ "github.com/segmentfault/answer/internal/base/data"
"github.com/segmentfault/answer/internal/entity"
- "xorm.io/xorm"
)
var (
tables = []interface{}{
- &entity.Activity{},
- &entity.Answer{},
- &entity.Collection{},
- &entity.CollectionGroup{},
- &entity.Comment{},
- &entity.Config{},
- &entity.Meta{},
- &entity.Notification{},
- &entity.Question{},
- &entity.Report{},
- &entity.Revision{},
- &entity.SiteInfo{},
- &entity.Tag{},
- &entity.TagRel{},
- &entity.Uniqid{},
- &entity.User{},
+ &entity.Activity{}, // done
+ &entity.Answer{}, // index
+ &entity.Collection{}, // done
+ &entity.CollectionGroup{}, // updated_at
+ &entity.Comment{}, // done
+ &entity.Config{}, // done
+ &entity.Meta{}, // done
+ &entity.Notification{}, // done
+ &entity.Question{}, // done
+ &entity.Report{}, // reported_user_id
+ &entity.Revision{}, // done
+ &entity.SiteInfo{}, // all
+ &entity.Tag{}, // done
+ &entity.TagRel{}, // index
+ &entity.Uniqid{}, // done
+ &entity.User{}, // index
+ &Version{},
}
)
// InitDB init db
-func InitDB(engine *xorm.Engine) (err error) {
- exist, err := engine.IsTableExist(&Version{})
+func InitDB(dataConf *data.Database) (err error) {
+ engine, err := data.NewDB(false, dataConf)
if err != nil {
+ fmt.Println("new database failed: ", err.Error())
return err
}
+
+ exist, err := engine.IsTableExist(&Version{})
+ if err != nil {
+ return fmt.Errorf("check table exists failed: %s", err)
+ }
if exist {
fmt.Println("[database] already exists")
return nil
}
- err = engine.Sync(tables)
+ err = engine.Sync(tables...)
if err != nil {
- return err
+ return fmt.Errorf("sync table failed: %s", err)
}
return nil
}
diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go
index 9723f840..3c6fea1e 100644
--- a/internal/migrations/migrations.go
+++ b/internal/migrations/migrations.go
@@ -3,6 +3,7 @@ package migrations
import (
"fmt"
+ "github.com/segmentfault/answer/internal/base/data"
"github.com/segmentfault/pacman/log"
"xorm.io/xorm"
)
@@ -81,7 +82,13 @@ func ExpectedVersion() int64 {
}
// Migrate database to current version
-func Migrate(engine *xorm.Engine) error {
+func Migrate(dataConf *data.Database) error {
+ engine, err := data.NewDB(false, dataConf)
+ if err != nil {
+ fmt.Println("new database failed: ", err.Error())
+ return err
+ }
+
currentDBVersion, err := GetCurrentDBVersion(engine)
if err != nil {
return err