-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🔥 Db refactoring #577
Open
iChemy
wants to merge
11
commits into
main
Choose a base branch
from
db-refactoring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
🔥 Db refactoring #577
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1417d76
:recycle: embed Provider into User
iChemy 2ade71e
:art: rename struct name
iChemy d9d35f2
:fire: delete tokens table and embed in users
iChemy f8d2538
:art: fix waste refactoring
iChemy b6c5c88
:art: fix column
iChemy ac19028
Revert ":art: fix column"
iChemy 0bfb28d
Revert ":art: fix waste refactoring"
iChemy dcc891e
Revert ":fire: delete tokens table and embed in users"
iChemy e68dbea
:construction: WIP
iChemy 1861685
:art: delete some comments
iChemy 4281d32
:zap: Room's BeforeUpdate hook
iChemy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,7 @@ func Migrations() []*gormigrate.Migration { | |
v10(), | ||
v11(), | ||
v12(), | ||
v13(), | ||
v14(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package migration | ||
|
||
import ( | ||
gormigrate "github.com/go-gormigrate/gormigrate/v2" | ||
"github.com/gofrs/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
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 | ||
} | ||
|
||
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(&v13newUser{}, "Issuer"); err != nil { | ||
return err | ||
} | ||
if err := db.Migrator().AddColumn(&v13newUser{}, "Subject"); err != nil { | ||
return err | ||
} | ||
|
||
// Step 2: Migrate data from Provider to User | ||
providers := make([]*v13currentProvider, 0) | ||
if err := db.Find(&providers).Error; err != nil { | ||
return err | ||
} | ||
|
||
for _, provider := range providers { | ||
if err := db.Model(&v13newUser{}).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(&v13currentProvider{}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{}) | ||
}, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model(r)
みたいなことをしているとr
のAdmins
がnil
になってしまうみたい.(その仕様が書かれているドキュメントを見つけられてないが...)