Skip to content

Commit

Permalink
impl:db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kavos113 committed Oct 29, 2024
1 parent 58d8bd2 commit 420479a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/db_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
| res_shared_to | char(30) | NO | | administrators | | アンケートの結果を, 運営は見られる ("administrators"), 回答済みの人は見られる ("respondents") 誰でも見られる ("public") |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが作成された日時 |
| modified_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが更新された日時 |
| is_published | bookean | NO | | false | | アンケートが公開かどうか

### respondents

Expand Down
4 changes: 3 additions & 1 deletion model/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

// Migrations is all db migrations
func Migrations() []*gormigrate.Migration {
return []*gormigrate.Migration{}
return []*gormigrate.Migration{
v3(),
}
}

func AllTables() []interface{} {
Expand Down
42 changes: 42 additions & 0 deletions model/v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package model

import (
"time"

"github.com/go-gormigrate/gormigrate/v2"
"gopkg.in/guregu/null.v4"
"gorm.io/gorm"
)

func v3() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "v3",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Targets{}); err != nil {
return err
}
return nil
},
}
}

type v3Questionnaires struct {
ID int `json:"questionnaireID" gorm:"type:int(11) AUTO_INCREMENT;not null;primaryKey"`
Title string `json:"title" gorm:"type:char(50);size:50;not null"`
Description string `json:"description" gorm:"type:text;not null"`
ResTimeLimit null.Time `json:"res_time_limit,omitempty" gorm:"type:TIMESTAMP NULL;default:NULL;"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"type:TIMESTAMP NULL;default:NULL;"`
ResSharedTo string `json:"res_shared_to" gorm:"type:char(30);size:30;not null;default:administrators"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
ModifiedAt time.Time `json:"modified_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
Administrators []Administrators `json:"-" gorm:"foreignKey:QuestionnaireID"`
Targets []Targets `json:"-" gorm:"foreignKey:QuestionnaireID"`
TargetGroups []TargetGroups `json:"-" gorm:"foreignKey:QuestionnaireID"`
Questions []Questions `json:"-" gorm:"foreignKey:QuestionnaireID"`
Respondents []Respondents `json:"-" gorm:"foreignKey:QuestionnaireID"`
IsPublished bool `json:"is_published" gorm:"type:boolean;default:false"`
}

func (*v3Questionnaires) TableName() string {
return "questionnaires"
}

0 comments on commit 420479a

Please sign in to comment.