From 1417d76d0b2881e2d55e56e76519ec5caea1b3e0 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 19:43:59 +0900 Subject: [PATCH 01/11] :recycle: embed Provider into User --- infra/db/model.go | 10 +++----- infra/db/user.go | 4 +-- infra/db/user_test.go | 5 ++-- migration/current.go | 1 + migration/v13.go | 59 +++++++++++++++++++++++++++++++++++++++++++ repository/user.go | 2 -- 6 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 migration/v13.go diff --git a/infra/db/model.go b/infra/db/model.go index d46c13ad..90cb213d 100644 --- a/infra/db/model.go +++ b/infra/db/model.go @@ -10,7 +10,6 @@ import ( var tables = []interface{}{ User{}, Token{}, - Provider{}, Group{}, GroupMember{}, GroupAdmin{}, @@ -58,8 +57,7 @@ type Token struct { } type Provider struct { - UserID uuid.UUID `gorm:"type:char(36); primaryKey"` - Issuer string `gorm:"not null"` + Issuer string `gorm:"not null"` Subject string } @@ -68,9 +66,9 @@ type User struct { // アプリの管理者かどうか Privilege bool `gorm:"not null"` State int - IcalSecret string `gorm:"not null"` - Provider Provider `gorm:"foreignKey:UserID; constraint:OnDelete:CASCADE;"` - Token Token `gorm:"foreignKey:UserID; constraint:OnDelete:CASCADE;"` + IcalSecret string `gorm:"not null"` + Provider + Token Token `gorm:"foreignKey:UserID; constraint:OnDelete:CASCADE;"` } type UserBody struct { diff --git a/infra/db/user.go b/infra/db/user.go index d7b16368..b4dcc6e6 100644 --- a/infra/db/user.go +++ b/infra/db/user.go @@ -8,7 +8,7 @@ import ( ) func userPreload(tx *gorm.DB) *gorm.DB { - return tx.Preload("Provider") + return tx } func (repo *GormRepository) SaveUser(user User) (*User, error) { @@ -73,7 +73,7 @@ func (repo *GormRepository) SyncUsers(users []*User) error { // user.Privilegeは常に更新されません。 func saveUser(db *gorm.DB, user *User) (*User, error) { err := db.Transaction(func(tx *gorm.DB) error { - existingUser, err := getUser(tx.Preload("Provider").Preload("Token"), user.ID) + existingUser, err := getUser(tx.Preload("Token"), user.ID) if errors.Is(err, gorm.ErrRecordNotFound) { return tx.Create(&user).Error } diff --git a/infra/db/user_test.go b/infra/db/user_test.go index 5e0280f6..f3020a6a 100644 --- a/infra/db/user_test.go +++ b/infra/db/user_test.go @@ -20,7 +20,6 @@ func Test_saveUser(t *testing.T) { }, }, Provider: Provider{ - UserID: id, Issuer: "bar", Subject: id.String(), }, @@ -29,7 +28,7 @@ func Test_saveUser(t *testing.T) { t.Run("save user", func(t *testing.T) { _, err := saveUser(r.db, user) assert.NoError(err) - u, err := getUser(r.db.Preload("Provider"), id) + u, err := getUser(r.db, id) assert.NoError(err) assert.Equal(user.Provider.Issuer, u.Provider.Issuer) }) @@ -41,7 +40,7 @@ func Test_saveUser(t *testing.T) { }) assert.NoError(err) - u, err := getUser(r.db.Preload("Token").Preload("Provider"), id) + u, err := getUser(r.db.Preload("Token"), id) assert.NoError(err) // token assert.Equal(user.Token.AccessToken, u.Token.AccessToken) diff --git a/migration/current.go b/migration/current.go index b4790920..f0660043 100644 --- a/migration/current.go +++ b/migration/current.go @@ -19,5 +19,6 @@ func Migrations() []*gormigrate.Migration { v10(), v11(), v12(), + v13(), } } diff --git a/migration/v13.go b/migration/v13.go new file mode 100644 index 00000000..bfeba9fa --- /dev/null +++ b/migration/v13.go @@ -0,0 +1,59 @@ +package migration + +import ( + gormigrate "github.com/go-gormigrate/gormigrate/v2" + "github.com/gofrs/uuid" + "gorm.io/gorm" +) + +type User struct { + ID uuid.UUID `gorm:"type:char(36); primaryKey"` + Privilege bool `gorm:"not null"` + State int + IcalSecret string `gorm:"not null"` + Issuer string `gorm:"not null"` + Subject string +} + +type Provider struct { + UserID uuid.UUID `gorm:"type:char(36); primaryKey"` + Issuer string `gorm:"not null"` + Subject string +} + +func v13() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "13", + Migrate: func(db *gorm.DB) error { + // Step 1: Add Issuer and Subject columns to the User table + if err := db.Migrator().AddColumn(&User{}, "Issuer"); err != nil { + return err + } + if err := db.Migrator().AddColumn(&User{}, "Subject"); err != nil { + return err + } + + // Step 2: Migrate data from Provider to User + providers := make([]*Provider, 0) + if err := db.Find(&providers).Error; err != nil { + return err + } + + for _, provider := range providers { + if err := db.Model(&User{}).Where("id = ?", provider.UserID).Updates(map[string]interface{}{ + "Issuer": provider.Issuer, + "Subject": provider.Subject, + }).Error; err != nil { + return err + } + } + + // Step 3: Drop the Provider table + if err := db.Migrator().DropTable(&Provider{}); err != nil { + return err + } + + return nil + }, + } +} diff --git a/repository/user.go b/repository/user.go index 7d2ac830..065208ed 100644 --- a/repository/user.go +++ b/repository/user.go @@ -33,7 +33,6 @@ func (repo *Repository) SyncUsers(info *domain.ConInfo) error { ID: uid, State: int(u.State), Provider: db.Provider{ - UserID: uid, Issuer: traQIssuerName, Subject: u.GetId(), }, @@ -72,7 +71,6 @@ func (repo *Repository) LoginUser(query, state, codeVerifier string) (*domain.Us }, }, Provider: db.Provider{ - UserID: uid, Issuer: traQIssuerName, Subject: traQUser.GetId(), }, From 2ade71e0e5053e5a4330f385a5cd1ef4635ccb39 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 19:57:26 +0900 Subject: [PATCH 02/11] :art: rename struct name --- migration/v13.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/migration/v13.go b/migration/v13.go index bfeba9fa..dd0084eb 100644 --- a/migration/v13.go +++ b/migration/v13.go @@ -6,7 +6,7 @@ import ( "gorm.io/gorm" ) -type User struct { +type v13newUser struct { ID uuid.UUID `gorm:"type:char(36); primaryKey"` Privilege bool `gorm:"not null"` State int @@ -15,32 +15,40 @@ type User struct { Subject string } -type Provider struct { +func (*v13newUser) TableName() string { + return "users" +} + +type v13currentProvider struct { UserID uuid.UUID `gorm:"type:char(36); primaryKey"` Issuer string `gorm:"not null"` Subject string } +func (*v13currentProvider) TableName() string { + return "providers" +} + func v13() *gormigrate.Migration { return &gormigrate.Migration{ ID: "13", Migrate: func(db *gorm.DB) error { // Step 1: Add Issuer and Subject columns to the User table - if err := db.Migrator().AddColumn(&User{}, "Issuer"); err != nil { + if err := db.Migrator().AddColumn(&v13newUser{}, "Issuer"); err != nil { return err } - if err := db.Migrator().AddColumn(&User{}, "Subject"); err != nil { + if err := db.Migrator().AddColumn(&v13newUser{}, "Subject"); err != nil { return err } // Step 2: Migrate data from Provider to User - providers := make([]*Provider, 0) + providers := make([]*v13currentProvider, 0) if err := db.Find(&providers).Error; err != nil { return err } for _, provider := range providers { - if err := db.Model(&User{}).Where("id = ?", provider.UserID).Updates(map[string]interface{}{ + if err := db.Model(&v13newUser{}).Where("id = ?", provider.UserID).Updates(map[string]interface{}{ "Issuer": provider.Issuer, "Subject": provider.Subject, }).Error; err != nil { @@ -49,7 +57,7 @@ func v13() *gormigrate.Migration { } // Step 3: Drop the Provider table - if err := db.Migrator().DropTable(&Provider{}); err != nil { + if err := db.Migrator().DropTable(&v13currentProvider{}); err != nil { return err } From d9d35f2b2ca968c39d0a07c7493be71d78321c7d Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 21:20:23 +0900 Subject: [PATCH 03/11] :fire: delete tokens table and embed in users --- compose.yml | 4 ++-- infra/db/model.go | 19 +++++++-------- infra/db/user.go | 2 +- infra/db/user_test.go | 20 +++++++--------- main.go | 2 +- migration/v13.go | 55 +++++++++++++++++++++++++++++++++++++++++++ repository/user.go | 15 ++++++------ 7 files changed, 82 insertions(+), 35 deletions(-) diff --git a/compose.yml b/compose.yml index 6b008310..c73dfc6e 100755 --- a/compose.yml +++ b/compose.yml @@ -11,7 +11,7 @@ services: MARIADB_USERNAME: root MARIADB_PASSWORD: password MARIADB_HOSTNAME: db - MARIADB_DATABASE: knoQ + MARIADB_DATABASE: knoq ORIGIN: http://localhost:3000 SESSION_KEY: ${SESSION_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX} TRAQ_CALENDARID: ${TRAQ_CALENDARID} @@ -41,7 +41,7 @@ services: container_name: knoq_db environment: MYSQL_ROOT_PASSWORD: password - MYSQL_DATABASE: knoQ + MYSQL_DATABASE: knoq command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci volumes: - "./_development/mariadb:/var/lib/mysql" diff --git a/infra/db/model.go b/infra/db/model.go index 90cb213d..f9d5e8ac 100644 --- a/infra/db/model.go +++ b/infra/db/model.go @@ -9,7 +9,6 @@ import ( var tables = []interface{}{ User{}, - Token{}, Group{}, GroupMember{}, GroupAdmin{}, @@ -51,8 +50,6 @@ type Oauth2Token struct { Expiry time.Time } type Token struct { - UserID uuid.UUID `gorm:"type:char(36); primaryKey"` - *Oauth2Token } @@ -68,16 +65,16 @@ type User struct { State int IcalSecret string `gorm:"not null"` Provider - Token Token `gorm:"foreignKey:UserID; constraint:OnDelete:CASCADE;"` + *Oauth2Token } -type UserBody struct { - ID uuid.UUID `gorm:"type:char(36); primaryKey;"` - Name string `gorm:"type:varchar(32);"` - DisplayName string `gorm:"type:varchar(32);"` - Icon string - User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` -} +// type UserBody struct { +// ID uuid.UUID `gorm:"type:char(36); primaryKey;"` +// Name string `gorm:"type:varchar(32);"` +// DisplayName string `gorm:"type:varchar(32);"` +// Icon string +// User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` +// } type RoomAdmin struct { UserID uuid.UUID `gorm:"type:char(36); primaryKey"` diff --git a/infra/db/user.go b/infra/db/user.go index b4dcc6e6..b4df2e38 100644 --- a/infra/db/user.go +++ b/infra/db/user.go @@ -73,7 +73,7 @@ func (repo *GormRepository) SyncUsers(users []*User) error { // user.Privilegeは常に更新されません。 func saveUser(db *gorm.DB, user *User) (*User, error) { err := db.Transaction(func(tx *gorm.DB) error { - existingUser, err := getUser(tx.Preload("Token"), user.ID) + existingUser, err := getUser(tx, user.ID) if errors.Is(err, gorm.ErrRecordNotFound) { return tx.Create(&user).Error } diff --git a/infra/db/user_test.go b/infra/db/user_test.go index f3020a6a..6fde5249 100644 --- a/infra/db/user_test.go +++ b/infra/db/user_test.go @@ -13,11 +13,8 @@ func Test_saveUser(t *testing.T) { ID: id, State: 1, IcalSecret: "foo", - Token: Token{ - UserID: id, - Oauth2Token: &Oauth2Token{ - AccessToken: "hoge", - }, + Oauth2Token: &Oauth2Token{ + AccessToken: "hoge", }, Provider: Provider{ Issuer: "bar", @@ -40,10 +37,10 @@ func Test_saveUser(t *testing.T) { }) assert.NoError(err) - u, err := getUser(r.db.Preload("Token"), id) + u, err := getUser(r.db, id) assert.NoError(err) // token - assert.Equal(user.Token.AccessToken, u.Token.AccessToken) + assert.Equal(user.AccessToken, u.AccessToken) // provider assert.Equal(user.Provider.Issuer, u.Provider.Issuer) // icalSecret @@ -54,12 +51,11 @@ func Test_saveUser(t *testing.T) { _, err := saveUser(r.db, &User{ ID: user.ID, State: 2, - Token: Token{ - Oauth2Token: &Oauth2Token{ - AccessToken: "hoge2", - }, + Oauth2Token: &Oauth2Token{ + AccessToken: "hoge2", }, - }) + }, + ) assert.NoError(err) token, err := getToken(r.db, id) assert.NoError(err) diff --git a/main.go b/main.go index 909572ee..aad1bf4f 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ var ( mariadbHost = getenv("MARIADB_HOSTNAME", "db") mariadbUser = getenv("MARIADB_USERNAME", "root") mariadbPassword = getenv("MARIADB_PASSWORD", "password") - mariadbDatabase = getenv("MARIADB_DATABASE", "knoQ") + mariadbDatabase = getenv("MARIADB_DATABASE", "knoq") mariadbPort = getenv("MARIADB_PORT", "3306") tokenKey = getenv("TOKEN_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX") gormLogLevel = getenv("GORM_LOG_LEVEL", "silent") diff --git a/migration/v13.go b/migration/v13.go index dd0084eb..7680bc00 100644 --- a/migration/v13.go +++ b/migration/v13.go @@ -1,11 +1,20 @@ package migration import ( + "time" + gormigrate "github.com/go-gormigrate/gormigrate/v2" "github.com/gofrs/uuid" "gorm.io/gorm" ) +type embeddedToken struct { + AccessToken string `gorm:"type:varbinary(64)"` + TokenType string + RefreshToken string + Expiry time.Time +} + type v13newUser struct { ID uuid.UUID `gorm:"type:char(36); primaryKey"` Privilege bool `gorm:"not null"` @@ -13,6 +22,7 @@ type v13newUser struct { IcalSecret string `gorm:"not null"` Issuer string `gorm:"not null"` Subject string + *embeddedToken } func (*v13newUser) TableName() string { @@ -29,6 +39,15 @@ func (*v13currentProvider) TableName() string { return "providers" } +type v13currentToken struct { + UserID uuid.UUID `gorm:"type:char(36); primaryKey"` + *embeddedToken +} + +func (*v13currentToken) TableName() string { + return "tokens" +} + func v13() *gormigrate.Migration { return &gormigrate.Migration{ ID: "13", @@ -61,6 +80,42 @@ func v13() *gormigrate.Migration { return err } + // Step 1: Add Token fields to the User table + if err := db.Migrator().AddColumn(&v13newUser{}, "AccessToken"); err != nil { + return err + } + if err := db.Migrator().AddColumn(&v13newUser{}, "TokenType"); err != nil { + return err + } + if err := db.Migrator().AddColumn(&v13newUser{}, "RefreshToken"); err != nil { + return err + } + if err := db.Migrator().AddColumn(&v13newUser{}, "Expiry"); err != nil { + return err + } + + // Step 2: Migrate data from Token to User + tokens := make([]*v13currentToken, 0) + if err := db.Find(&tokens).Error; err != nil { + return err + } + + for _, token := range tokens { + if err := db.Model(&v13newUser{}).Where("id = ?", token.UserID).Updates(map[string]interface{}{ + "AccessToken": token.AccessToken, + "TokenType": token.TokenType, + "RefreshToken": token.RefreshToken, + "Expiry": token.Expiry, + }).Error; err != nil { + return err + } + } + + // Step 3: Drop the Token table + if err := db.Migrator().DropTable(&v13currentToken{}); err != nil { + return err + } + return nil }, } diff --git a/repository/user.go b/repository/user.go index 065208ed..74760636 100644 --- a/repository/user.go +++ b/repository/user.go @@ -61,15 +61,14 @@ func (repo *Repository) LoginUser(query, state, codeVerifier string) (*domain.Us user := db.User{ ID: uid, State: 1, - Token: db.Token{ - UserID: uid, - Oauth2Token: &db.Oauth2Token{ - AccessToken: t.AccessToken, - TokenType: t.TokenType, - RefreshToken: t.RefreshToken, - Expiry: t.Expiry, - }, + + Oauth2Token: &db.Oauth2Token{ + AccessToken: t.AccessToken, + TokenType: t.TokenType, + RefreshToken: t.RefreshToken, + Expiry: t.Expiry, }, + Provider: db.Provider{ Issuer: traQIssuerName, Subject: traQUser.GetId(), From f8d253853fb6b4fe12bbf9548c445c1be7fd701b Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 21:21:38 +0900 Subject: [PATCH 04/11] :art: fix waste refactoring --- compose.yml | 4 ++-- main.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compose.yml b/compose.yml index c73dfc6e..6b008310 100755 --- a/compose.yml +++ b/compose.yml @@ -11,7 +11,7 @@ services: MARIADB_USERNAME: root MARIADB_PASSWORD: password MARIADB_HOSTNAME: db - MARIADB_DATABASE: knoq + MARIADB_DATABASE: knoQ ORIGIN: http://localhost:3000 SESSION_KEY: ${SESSION_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX} TRAQ_CALENDARID: ${TRAQ_CALENDARID} @@ -41,7 +41,7 @@ services: container_name: knoq_db environment: MYSQL_ROOT_PASSWORD: password - MYSQL_DATABASE: knoq + MYSQL_DATABASE: knoQ command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci volumes: - "./_development/mariadb:/var/lib/mysql" diff --git a/main.go b/main.go index aad1bf4f..909572ee 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ var ( mariadbHost = getenv("MARIADB_HOSTNAME", "db") mariadbUser = getenv("MARIADB_USERNAME", "root") mariadbPassword = getenv("MARIADB_PASSWORD", "password") - mariadbDatabase = getenv("MARIADB_DATABASE", "knoq") + mariadbDatabase = getenv("MARIADB_DATABASE", "knoQ") mariadbPort = getenv("MARIADB_PORT", "3306") tokenKey = getenv("TOKEN_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX") gormLogLevel = getenv("GORM_LOG_LEVEL", "silent") From b6c5c88721ba7f865db7f55606747763f06b56b6 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 22:15:48 +0900 Subject: [PATCH 05/11] :art: fix column --- migration/v13.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/migration/v13.go b/migration/v13.go index 7680bc00..0a09b8f3 100644 --- a/migration/v13.go +++ b/migration/v13.go @@ -16,13 +16,16 @@ type embeddedToken struct { } type v13newUser struct { - ID uuid.UUID `gorm:"type:char(36); primaryKey"` - Privilege bool `gorm:"not null"` - State int - IcalSecret string `gorm:"not null"` - Issuer string `gorm:"not null"` - Subject string - *embeddedToken + ID uuid.UUID `gorm:"type:char(36); primaryKey"` + Privilege bool `gorm:"not null"` + State int + IcalSecret string `gorm:"not null"` + Issuer string `gorm:"not null"` + Subject string + AccessToken *string `gorm:"type:varbinary(64)"` + TokenType *string + RefreshToken *string + Expiry *time.Time } func (*v13newUser) TableName() string { From ac19028eb35cf623b4b178dda7dd6546ecc49779 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 23:11:14 +0900 Subject: [PATCH 06/11] Revert ":art: fix column" This reverts commit b6c5c88721ba7f865db7f55606747763f06b56b6. --- migration/v13.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/migration/v13.go b/migration/v13.go index 0a09b8f3..7680bc00 100644 --- a/migration/v13.go +++ b/migration/v13.go @@ -16,16 +16,13 @@ type embeddedToken struct { } type v13newUser struct { - ID uuid.UUID `gorm:"type:char(36); primaryKey"` - Privilege bool `gorm:"not null"` - State int - IcalSecret string `gorm:"not null"` - Issuer string `gorm:"not null"` - Subject string - AccessToken *string `gorm:"type:varbinary(64)"` - TokenType *string - RefreshToken *string - Expiry *time.Time + ID uuid.UUID `gorm:"type:char(36); primaryKey"` + Privilege bool `gorm:"not null"` + State int + IcalSecret string `gorm:"not null"` + Issuer string `gorm:"not null"` + Subject string + *embeddedToken } func (*v13newUser) TableName() string { From 0bfb28dbe33e2a0ddb982828ed426e36effddaa5 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 23:11:22 +0900 Subject: [PATCH 07/11] Revert ":art: fix waste refactoring" This reverts commit f8d253853fb6b4fe12bbf9548c445c1be7fd701b. --- compose.yml | 4 ++-- main.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compose.yml b/compose.yml index 6b008310..c73dfc6e 100755 --- a/compose.yml +++ b/compose.yml @@ -11,7 +11,7 @@ services: MARIADB_USERNAME: root MARIADB_PASSWORD: password MARIADB_HOSTNAME: db - MARIADB_DATABASE: knoQ + MARIADB_DATABASE: knoq ORIGIN: http://localhost:3000 SESSION_KEY: ${SESSION_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX} TRAQ_CALENDARID: ${TRAQ_CALENDARID} @@ -41,7 +41,7 @@ services: container_name: knoq_db environment: MYSQL_ROOT_PASSWORD: password - MYSQL_DATABASE: knoQ + MYSQL_DATABASE: knoq command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci volumes: - "./_development/mariadb:/var/lib/mysql" diff --git a/main.go b/main.go index 909572ee..aad1bf4f 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ var ( mariadbHost = getenv("MARIADB_HOSTNAME", "db") mariadbUser = getenv("MARIADB_USERNAME", "root") mariadbPassword = getenv("MARIADB_PASSWORD", "password") - mariadbDatabase = getenv("MARIADB_DATABASE", "knoQ") + mariadbDatabase = getenv("MARIADB_DATABASE", "knoq") mariadbPort = getenv("MARIADB_PORT", "3306") tokenKey = getenv("TOKEN_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX") gormLogLevel = getenv("GORM_LOG_LEVEL", "silent") From dcc891e9d1006ff6273921c70c258b37d943eb44 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 12 Dec 2024 23:11:26 +0900 Subject: [PATCH 08/11] Revert ":fire: delete tokens table and embed in users" This reverts commit d9d35f2b2ca968c39d0a07c7493be71d78321c7d. --- compose.yml | 4 ++-- infra/db/model.go | 19 ++++++++------- infra/db/user.go | 2 +- infra/db/user_test.go | 20 +++++++++------- main.go | 2 +- migration/v13.go | 55 ------------------------------------------- repository/user.go | 15 ++++++------ 7 files changed, 35 insertions(+), 82 deletions(-) diff --git a/compose.yml b/compose.yml index c73dfc6e..6b008310 100755 --- a/compose.yml +++ b/compose.yml @@ -11,7 +11,7 @@ services: MARIADB_USERNAME: root MARIADB_PASSWORD: password MARIADB_HOSTNAME: db - MARIADB_DATABASE: knoq + MARIADB_DATABASE: knoQ ORIGIN: http://localhost:3000 SESSION_KEY: ${SESSION_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX} TRAQ_CALENDARID: ${TRAQ_CALENDARID} @@ -41,7 +41,7 @@ services: container_name: knoq_db environment: MYSQL_ROOT_PASSWORD: password - MYSQL_DATABASE: knoq + MYSQL_DATABASE: knoQ command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci volumes: - "./_development/mariadb:/var/lib/mysql" diff --git a/infra/db/model.go b/infra/db/model.go index f9d5e8ac..90cb213d 100644 --- a/infra/db/model.go +++ b/infra/db/model.go @@ -9,6 +9,7 @@ import ( var tables = []interface{}{ User{}, + Token{}, Group{}, GroupMember{}, GroupAdmin{}, @@ -50,6 +51,8 @@ type Oauth2Token struct { Expiry time.Time } type Token struct { + UserID uuid.UUID `gorm:"type:char(36); primaryKey"` + *Oauth2Token } @@ -65,16 +68,16 @@ type User struct { State int IcalSecret string `gorm:"not null"` Provider - *Oauth2Token + Token Token `gorm:"foreignKey:UserID; constraint:OnDelete:CASCADE;"` } -// type UserBody struct { -// ID uuid.UUID `gorm:"type:char(36); primaryKey;"` -// Name string `gorm:"type:varchar(32);"` -// DisplayName string `gorm:"type:varchar(32);"` -// Icon string -// User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` -// } +type UserBody struct { + ID uuid.UUID `gorm:"type:char(36); primaryKey;"` + Name string `gorm:"type:varchar(32);"` + DisplayName string `gorm:"type:varchar(32);"` + Icon string + User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` +} type RoomAdmin struct { UserID uuid.UUID `gorm:"type:char(36); primaryKey"` diff --git a/infra/db/user.go b/infra/db/user.go index b4df2e38..b4dcc6e6 100644 --- a/infra/db/user.go +++ b/infra/db/user.go @@ -73,7 +73,7 @@ func (repo *GormRepository) SyncUsers(users []*User) error { // user.Privilegeは常に更新されません。 func saveUser(db *gorm.DB, user *User) (*User, error) { err := db.Transaction(func(tx *gorm.DB) error { - existingUser, err := getUser(tx, user.ID) + existingUser, err := getUser(tx.Preload("Token"), user.ID) if errors.Is(err, gorm.ErrRecordNotFound) { return tx.Create(&user).Error } diff --git a/infra/db/user_test.go b/infra/db/user_test.go index 6fde5249..f3020a6a 100644 --- a/infra/db/user_test.go +++ b/infra/db/user_test.go @@ -13,8 +13,11 @@ func Test_saveUser(t *testing.T) { ID: id, State: 1, IcalSecret: "foo", - Oauth2Token: &Oauth2Token{ - AccessToken: "hoge", + Token: Token{ + UserID: id, + Oauth2Token: &Oauth2Token{ + AccessToken: "hoge", + }, }, Provider: Provider{ Issuer: "bar", @@ -37,10 +40,10 @@ func Test_saveUser(t *testing.T) { }) assert.NoError(err) - u, err := getUser(r.db, id) + u, err := getUser(r.db.Preload("Token"), id) assert.NoError(err) // token - assert.Equal(user.AccessToken, u.AccessToken) + assert.Equal(user.Token.AccessToken, u.Token.AccessToken) // provider assert.Equal(user.Provider.Issuer, u.Provider.Issuer) // icalSecret @@ -51,11 +54,12 @@ func Test_saveUser(t *testing.T) { _, err := saveUser(r.db, &User{ ID: user.ID, State: 2, - Oauth2Token: &Oauth2Token{ - AccessToken: "hoge2", + Token: Token{ + Oauth2Token: &Oauth2Token{ + AccessToken: "hoge2", + }, }, - }, - ) + }) assert.NoError(err) token, err := getToken(r.db, id) assert.NoError(err) diff --git a/main.go b/main.go index aad1bf4f..909572ee 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ var ( mariadbHost = getenv("MARIADB_HOSTNAME", "db") mariadbUser = getenv("MARIADB_USERNAME", "root") mariadbPassword = getenv("MARIADB_PASSWORD", "password") - mariadbDatabase = getenv("MARIADB_DATABASE", "knoq") + mariadbDatabase = getenv("MARIADB_DATABASE", "knoQ") mariadbPort = getenv("MARIADB_PORT", "3306") tokenKey = getenv("TOKEN_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX") gormLogLevel = getenv("GORM_LOG_LEVEL", "silent") diff --git a/migration/v13.go b/migration/v13.go index 7680bc00..dd0084eb 100644 --- a/migration/v13.go +++ b/migration/v13.go @@ -1,20 +1,11 @@ package migration import ( - "time" - gormigrate "github.com/go-gormigrate/gormigrate/v2" "github.com/gofrs/uuid" "gorm.io/gorm" ) -type embeddedToken struct { - AccessToken string `gorm:"type:varbinary(64)"` - TokenType string - RefreshToken string - Expiry time.Time -} - type v13newUser struct { ID uuid.UUID `gorm:"type:char(36); primaryKey"` Privilege bool `gorm:"not null"` @@ -22,7 +13,6 @@ type v13newUser struct { IcalSecret string `gorm:"not null"` Issuer string `gorm:"not null"` Subject string - *embeddedToken } func (*v13newUser) TableName() string { @@ -39,15 +29,6 @@ func (*v13currentProvider) TableName() string { return "providers" } -type v13currentToken struct { - UserID uuid.UUID `gorm:"type:char(36); primaryKey"` - *embeddedToken -} - -func (*v13currentToken) TableName() string { - return "tokens" -} - func v13() *gormigrate.Migration { return &gormigrate.Migration{ ID: "13", @@ -80,42 +61,6 @@ func v13() *gormigrate.Migration { return err } - // Step 1: Add Token fields to the User table - if err := db.Migrator().AddColumn(&v13newUser{}, "AccessToken"); err != nil { - return err - } - if err := db.Migrator().AddColumn(&v13newUser{}, "TokenType"); err != nil { - return err - } - if err := db.Migrator().AddColumn(&v13newUser{}, "RefreshToken"); err != nil { - return err - } - if err := db.Migrator().AddColumn(&v13newUser{}, "Expiry"); err != nil { - return err - } - - // Step 2: Migrate data from Token to User - tokens := make([]*v13currentToken, 0) - if err := db.Find(&tokens).Error; err != nil { - return err - } - - for _, token := range tokens { - if err := db.Model(&v13newUser{}).Where("id = ?", token.UserID).Updates(map[string]interface{}{ - "AccessToken": token.AccessToken, - "TokenType": token.TokenType, - "RefreshToken": token.RefreshToken, - "Expiry": token.Expiry, - }).Error; err != nil { - return err - } - } - - // Step 3: Drop the Token table - if err := db.Migrator().DropTable(&v13currentToken{}); err != nil { - return err - } - return nil }, } diff --git a/repository/user.go b/repository/user.go index 74760636..065208ed 100644 --- a/repository/user.go +++ b/repository/user.go @@ -61,14 +61,15 @@ func (repo *Repository) LoginUser(query, state, codeVerifier string) (*domain.Us user := db.User{ ID: uid, State: 1, - - Oauth2Token: &db.Oauth2Token{ - AccessToken: t.AccessToken, - TokenType: t.TokenType, - RefreshToken: t.RefreshToken, - Expiry: t.Expiry, + Token: db.Token{ + UserID: uid, + Oauth2Token: &db.Oauth2Token{ + AccessToken: t.AccessToken, + TokenType: t.TokenType, + RefreshToken: t.RefreshToken, + Expiry: t.Expiry, + }, }, - Provider: db.Provider{ Issuer: traQIssuerName, Subject: traQUser.GetId(), From e68dbea42ac2c8c6969c2c6878d6ea8904cbf864 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 19 Dec 2024 02:47:43 +0900 Subject: [PATCH 09/11] :construction: WIP --- infra/db/converter.go | 78 +++++++++++++++++++++++++------------------ infra/db/hooks.go | 5 ++- infra/db/model.go | 16 ++++----- migration/current.go | 1 + migration/v14.go | 78 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 43 deletions(-) create mode 100644 migration/v14.go diff --git a/infra/db/converter.go b/infra/db/converter.go index 59b142ed..99a2c5b2 100644 --- a/infra/db/converter.go +++ b/infra/db/converter.go @@ -15,16 +15,17 @@ func ConvCreateRoomParamsToRoom(src CreateRoomParams) (dst Room) { dst.Place = src.WriteRoomParams.Place dst.TimeStart = src.WriteRoomParams.TimeStart dst.TimeEnd = src.WriteRoomParams.TimeEnd - dst.Admins = make([]RoomAdmin, len(src.WriteRoomParams.Admins)) + dst.Admins = make([]User, len(src.WriteRoomParams.Admins)) for i := range src.WriteRoomParams.Admins { - dst.Admins[i] = convuuidUUIDToRoomAdmin(src.WriteRoomParams.Admins[i]) + dst.Admins[i] = ConvuuidUUIDToUserMeta(src.WriteRoomParams.Admins[i]) } return } -func ConvEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { - dst.UserID = src.UserID - return -} + +// func ConvEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { +// dst.UserID = src.UserID +// return +// } func ConvEventAdminTodomainUser(src EventAdmin) (dst domain.User) { dst.ID = src.UserID @@ -100,10 +101,10 @@ func ConvGroupTodomainGroup(src Group) (dst domain.Group) { (*dst.Model.DeletedAt) = convgormDeletedAtTotimeTime(src.Model.DeletedAt) return } -func ConvRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { - dst.ID = src.UserID - return -} +// func ConvRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { +// dst.ID = src.UserID +// return +// } func ConvRoomTodomainRoom(src Room) (dst domain.Room) { dst.ID = src.ID @@ -117,7 +118,7 @@ func ConvRoomTodomainRoom(src Room) (dst domain.Room) { } dst.Admins = make([]domain.User, len(src.Admins)) for i := range src.Admins { - dst.Admins[i] = convRoomAdminTodomainUser(src.Admins[i]) + dst.Admins[i] = convUserTodomainUser(src.Admins[i]) } dst.CreatedBy = convUserTodomainUser(src.CreatedBy) dst.Model.CreatedAt = src.Model.CreatedAt @@ -127,10 +128,18 @@ func ConvRoomTodomainRoom(src Room) (dst domain.Room) { return } -func ConvSEventAdminToSRoomAdmin(src []EventAdmin) (dst []RoomAdmin) { - dst = make([]RoomAdmin, len(src)) +// func ConvSEventAdminToSRoomAdmin(src []EventAdmin) (dst []RoomAdmin) { +// dst = make([]RoomAdmin, len(src)) +// for i := range src { +// dst[i] = convEventAdminToRoomAdmin(src[i]) +// } +// return +// } + +func ConvSEventAdminToSUser(src []EventAdmin) (dst []User) { + dst = make([]User, len(src)) for i := range src { - dst[i] = convEventAdminToRoomAdmin(src[i]) + dst[i] = convEventAdminToUser(src[i]) } return } @@ -207,9 +216,9 @@ func ConvUpdateRoomParamsToRoom(src UpdateRoomParams) (dst Room) { dst.Place = src.WriteRoomParams.Place dst.TimeStart = src.WriteRoomParams.TimeStart dst.TimeEnd = src.WriteRoomParams.TimeEnd - dst.Admins = make([]RoomAdmin, len(src.WriteRoomParams.Admins)) + dst.Admins = make([]User, len(src.WriteRoomParams.Admins)) for i := range src.WriteRoomParams.Admins { - dst.Admins[i] = convuuidUUIDToRoomAdmin(src.WriteRoomParams.Admins[i]) + dst.Admins[i] = ConvuuidUUIDToUserMeta(src.WriteRoomParams.Admins[i]) } return } @@ -288,21 +297,26 @@ func ConvuuidUUIDToGroupMember(src uuid.UUID) (dst GroupMember) { dst.UserID = src return } -func ConvuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { - dst.UserID = src - return -} +// func ConvuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { +// dst.UserID = src +// return +// } func ConvuuidUUIDToUserMeta(src uuid.UUID) (dst User) { dst.ID = src return } -func convEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { - dst.UserID = src.UserID +// func convEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { +// dst.UserID = src.UserID +// return +// } + +func convEventAdminTodomainUser(src EventAdmin) (dst domain.User) { + dst.ID = src.UserID return } -func convEventAdminTodomainUser(src EventAdmin) (dst domain.User) { +func convEventAdminToUser(src EventAdmin) (dst User) { dst.ID = src.UserID return } @@ -375,10 +389,10 @@ func convGroupTodomainGroup(src Group) (dst domain.Group) { (*dst.Model.DeletedAt) = convgormDeletedAtTotimeTime(src.Model.DeletedAt) return } -func convRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { - dst.ID = src.UserID - return -} +// func convRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { +// dst.ID = src.UserID +// return +// } func convRoomTodomainRoom(src Room) (dst domain.Room) { dst.ID = src.ID dst.Place = src.Place @@ -391,7 +405,7 @@ func convRoomTodomainRoom(src Room) (dst domain.Room) { } dst.Admins = make([]domain.User, len(src.Admins)) for i := range src.Admins { - dst.Admins[i] = convRoomAdminTodomainUser(src.Admins[i]) + dst.Admins[i] = convUserTodomainUser(src.Admins[i]) } dst.CreatedBy = convUserTodomainUser(src.CreatedBy) dst.Model.CreatedAt = src.Model.CreatedAt @@ -439,7 +453,7 @@ func convuuidUUIDToGroupMember(src uuid.UUID) (dst GroupMember) { return } -func convuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { - dst.UserID = src - return -} +// func convuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { +// dst.UserID = src +// return +// } diff --git a/infra/db/hooks.go b/infra/db/hooks.go index 078c57fb..3c7f6ffe 100644 --- a/infra/db/hooks.go +++ b/infra/db/hooks.go @@ -22,7 +22,7 @@ func (e *Event) BeforeSave(tx *gorm.DB) (err error) { e.Room.TimeStart = e.TimeStart e.Room.TimeEnd = e.TimeEnd e.Room.CreatedByRefer = e.CreatedByRefer - e.Room.Admins = ConvSEventAdminToSRoomAdmin(e.Admins) + e.Room.Admins = ConvSEventAdminToSUser(e.Admins) } else { return NewValueError(ErrRoomUndefined, "roomID", "place") } @@ -180,8 +180,7 @@ func (r *Room) BeforeSave(tx *gorm.DB) (err error) { } func (r *Room) BeforeUpdate(tx *gorm.DB) (err error) { - err = tx.Where("room_id", r.ID).Delete(&RoomAdmin{}).Error - if err != nil { + if err := tx.Model(r).Association("Admins").Clear(); err != nil { return err } return nil diff --git a/infra/db/model.go b/infra/db/model.go index 90cb213d..5bd8e015 100644 --- a/infra/db/model.go +++ b/infra/db/model.go @@ -15,7 +15,7 @@ var tables = []interface{}{ GroupAdmin{}, Tag{}, Room{}, - RoomAdmin{}, + // RoomAdmin{}, Event{}, EventTag{}, // Eventより下にないと、overrideされる EventAdmin{}, @@ -79,12 +79,12 @@ type UserBody struct { User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` } -type RoomAdmin struct { - UserID uuid.UUID `gorm:"type:char(36); primaryKey"` - RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` - User User `gorm:"->; foreignKey:UserID; constraint:OnDelete:CASCADE;" cvt:"->"` - Model `cvt:"-"` -} +// type RoomAdmin struct { +// UserID uuid.UUID `gorm:"type:char(36); primaryKey"` +// RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` +// User User `gorm:"->; foreignKey:UserID; constraint:OnDelete:CASCADE;" cvt:"->"` +// Model `cvt:"-"` +// } //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s Room -d domain.Room -o converter.go . //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s []*Room -d []*domain.Room -o converter.go . @@ -95,7 +95,7 @@ type Room struct { TimeStart time.Time `gorm:"type:DATETIME; index"` TimeEnd time.Time `gorm:"type:DATETIME; index"` Events []Event `gorm:"->; constraint:-"` // readOnly - Admins []RoomAdmin + Admins []User `gorm:"many2many:room_admin_users;"` CreatedByRefer uuid.UUID `gorm:"type:char(36);" cvt:"CreatedBy, <-"` CreatedBy User `gorm:"->; foreignKey:CreatedByRefer; constraint:OnDelete:CASCADE;" cvt:"->"` Model `cvt:"->"` diff --git a/migration/current.go b/migration/current.go index f0660043..e9488bc5 100644 --- a/migration/current.go +++ b/migration/current.go @@ -20,5 +20,6 @@ func Migrations() []*gormigrate.Migration { v11(), v12(), v13(), + v14(), } } diff --git a/migration/v14.go b/migration/v14.go new file mode 100644 index 00000000..7d5b54c7 --- /dev/null +++ b/migration/v14.go @@ -0,0 +1,78 @@ +package migration + +import ( + "time" + + "github.com/go-gormigrate/gormigrate/v2" + "github.com/gofrs/uuid" + "gorm.io/gorm" +) + +type v14Room struct { + ID uuid.UUID `gorm:"type:char(36);primaryKey"` + Place string `gorm:"type:varchar(32);"` + Verified bool + TimeStart time.Time `gorm:"type:DATETIME; index"` + TimeEnd time.Time `gorm:"type:DATETIME; index"` + CreatedByRefer uuid.UUID `gorm:"type:char(36);"` +} + +func (*v14Room) TableName() string { + return "rooms" +} + +type v14RoomUser struct { + RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` + UserID uuid.UUID `gorm:"type:char(36); primaryKey"` +} + +func (*v14RoomUser) TableName() string { + return "room_admin_users" +} + +type v14RoomAdmin struct { + UserID uuid.UUID `gorm:"type:char(36); primaryKey"` + RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` +} + +func (*v14RoomAdmin) TableName() string { + return "room_admins" +} + +func v14() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "14", + Migrate: func(db *gorm.DB) error { + // Step 1: Create the new many-to-many table + err := db.Migrator().CreateTable(&v14RoomUser{}) + if err != nil { + return err + } + + // Step 2: Migrate data from RoomAdmin to RoomUser + roomAdmins := []v14RoomAdmin{} + err = db.Find(&roomAdmins).Error + if err != nil { + return err + } + + roomUsers := make([]v14RoomUser, len(roomAdmins)) + for i, admin := range roomAdmins { + roomUsers[i] = v14RoomUser{ + RoomID: admin.RoomID, + UserID: admin.UserID, + } + } + + if len(roomUsers) > 0 { + err = db.Create(&roomUsers).Error + if err != nil { + return err + } + } + + // Step 3: Drop the RoomAdmin table + return db.Migrator().DropTable(&v14RoomAdmin{}) + }, + } +} From 18616858a5b3f5bc5937c9457586ec24d46e1832 Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 19 Dec 2024 03:06:43 +0900 Subject: [PATCH 10/11] :art: delete some comments --- infra/db/converter.go | 37 ++----------------------------------- infra/db/model.go | 8 -------- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/infra/db/converter.go b/infra/db/converter.go index 99a2c5b2..3e23bae9 100644 --- a/infra/db/converter.go +++ b/infra/db/converter.go @@ -22,11 +22,6 @@ func ConvCreateRoomParamsToRoom(src CreateRoomParams) (dst Room) { return } -// func ConvEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { -// dst.UserID = src.UserID -// return -// } - func ConvEventAdminTodomainUser(src EventAdmin) (dst domain.User) { dst.ID = src.UserID return @@ -101,10 +96,6 @@ func ConvGroupTodomainGroup(src Group) (dst domain.Group) { (*dst.Model.DeletedAt) = convgormDeletedAtTotimeTime(src.Model.DeletedAt) return } -// func ConvRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { -// dst.ID = src.UserID -// return -// } func ConvRoomTodomainRoom(src Room) (dst domain.Room) { dst.ID = src.ID @@ -128,14 +119,6 @@ func ConvRoomTodomainRoom(src Room) (dst domain.Room) { return } -// func ConvSEventAdminToSRoomAdmin(src []EventAdmin) (dst []RoomAdmin) { -// dst = make([]RoomAdmin, len(src)) -// for i := range src { -// dst[i] = convEventAdminToRoomAdmin(src[i]) -// } -// return -// } - func ConvSEventAdminToSUser(src []EventAdmin) (dst []User) { dst = make([]User, len(src)) for i := range src { @@ -297,20 +280,12 @@ func ConvuuidUUIDToGroupMember(src uuid.UUID) (dst GroupMember) { dst.UserID = src return } -// func ConvuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { -// dst.UserID = src -// return -// } + func ConvuuidUUIDToUserMeta(src uuid.UUID) (dst User) { dst.ID = src return } -// func convEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { -// dst.UserID = src.UserID -// return -// } - func convEventAdminTodomainUser(src EventAdmin) (dst domain.User) { dst.ID = src.UserID return @@ -389,10 +364,7 @@ func convGroupTodomainGroup(src Group) (dst domain.Group) { (*dst.Model.DeletedAt) = convgormDeletedAtTotimeTime(src.Model.DeletedAt) return } -// func convRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { -// dst.ID = src.UserID -// return -// } + func convRoomTodomainRoom(src Room) (dst domain.Room) { dst.ID = src.ID dst.Place = src.Place @@ -452,8 +424,3 @@ func convuuidUUIDToGroupMember(src uuid.UUID) (dst GroupMember) { dst.UserID = src return } - -// func convuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { -// dst.UserID = src -// return -// } diff --git a/infra/db/model.go b/infra/db/model.go index 5bd8e015..e5dfd31c 100644 --- a/infra/db/model.go +++ b/infra/db/model.go @@ -15,7 +15,6 @@ var tables = []interface{}{ GroupAdmin{}, Tag{}, Room{}, - // RoomAdmin{}, Event{}, EventTag{}, // Eventより下にないと、overrideされる EventAdmin{}, @@ -79,13 +78,6 @@ type UserBody struct { User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` } -// type RoomAdmin struct { -// UserID uuid.UUID `gorm:"type:char(36); primaryKey"` -// RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` -// User User `gorm:"->; foreignKey:UserID; constraint:OnDelete:CASCADE;" cvt:"->"` -// Model `cvt:"-"` -// } - //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s Room -d domain.Room -o converter.go . //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s []*Room -d []*domain.Room -o converter.go . type Room struct { From 4281d32974ed523d2d7cdbfb1813abda8bca8b2e Mon Sep 17 00:00:00 2001 From: iChemy Date: Thu, 19 Dec 2024 13:48:51 +0900 Subject: [PATCH 11/11] :zap: Room's BeforeUpdate hook --- infra/db/hooks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/db/hooks.go b/infra/db/hooks.go index 3c7f6ffe..6e174fd2 100644 --- a/infra/db/hooks.go +++ b/infra/db/hooks.go @@ -180,7 +180,7 @@ func (r *Room) BeforeSave(tx *gorm.DB) (err error) { } func (r *Room) BeforeUpdate(tx *gorm.DB) (err error) { - if err := tx.Model(r).Association("Admins").Clear(); err != nil { + if err := tx.Model(&Room{ID: r.ID}).Association("Admins").Clear(); err != nil { return err } return nil