Skip to content
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

Migrate database to GORM and new schema #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

jessegeens
Copy link

Following ADR-Reva-003, this PR refactors the sharing SQL logic to now use GORM, an ORM for Go. This change also comes with newly written unit tests for all the database logic.

@jessegeens jessegeens marked this pull request as ready for review January 8, 2025 08:35
@jessegeens jessegeens requested a review from glpatcern January 8, 2025 08:35
Copy link

@diocas diocas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions and comments. Haven't had the time to review the tests yet.

share/model.go Outdated Show resolved Hide resolved
share/model.go Outdated Show resolved Hide resolved
share/model.go Outdated Show resolved Hide resolved
share/model.go Outdated Show resolved Hide resolved
share/model.go Outdated Show resolved Hide resolved
share/sql/sql.go Outdated Show resolved Hide resolved
share/sql/sql.go Outdated Show resolved Hide resolved
share/sql/sql.go Outdated Show resolved Hide resolved
share/sql/sql.go Outdated Show resolved Hide resolved
share/sql/sql.go Outdated Show resolved Hide resolved
@jessegeens jessegeens force-pushed the fix/tx-sharing branch 3 times, most recently from 6b8c45a to 7843bfb Compare January 10, 2025 16:03
@jessegeens jessegeens force-pushed the fix/tx-sharing branch 2 times, most recently from f2d54aa to 48683cf Compare January 13, 2025 12:16
Copy link

@diocas diocas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another round of comments... Can we also add the annotations that set the indexes and constraints to the gorm models?

share/model.go Show resolved Hide resolved
UIDInitiator string
ItemType ItemType // file | folder | reference | symlink
InitialPath string
Inode string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being a bit nitpicky, but since the fields are not ordered by name, I would at least order them by "function". I mean to have to kind of logic, like have inode close to instance, for example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a good point about the ordering. But in Go this actually also determines the memory layout, so this also determines the size of your struct. So let me see which order makes the most sense (although, since almost all fields are strings, I don't think it's going to matter much here)

Copy link
Author

@jessegeens jessegeens Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reordering to something that makes more sense actually reduced the size :)

share/model.go Show resolved Hide resolved
Permissions uint8
Instance string
Orphan bool
Description string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prob makes sense for internal sharing. I would expect users to use the "name" as a kind of description of public links.


type ShareState struct {
gorm.Model
ShareID uint `gorm:"foreignKey:ShareID;references:ID"` // Define the foreign key field
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to define "shareid" givens that we already have a share, of type share, bellow? I would expect gorm to internally handle that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this but you do need to explicitly have the foreign key field. And gorm then uses this to fill the actual Share child struct

share/sql/sql.go Show resolved Hide resolved
shareState = model.ShareState{
Share: *share,
Hidden: false,
Synced: false,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe sync should be true by default.. We don't use it now, but when we move the clients to reva, this might be used.. cc @glpatcern

return nil, err
// Does not really matter if it fails, next time the user
// lists his shares this will just be called again
m.db.Save(&shareState)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why are we persisting the defaults? I would argue this is adding unecessary rows to the DB. Also, functionally, we have a get function that does changes...

func (m *mgr) appendUidOwnerFilters(ctx context.Context, query string, params []interface{}) (string, []interface{}, error) {
uidOwnersQuery, uidOwnersParams, err := m.uidOwnerFilters(ctx)
shareState, err := m.getShareState(ctx, &model.Share{
ProtoShare: model.ProtoShare{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You def need a getProtoShare... I really don't like all this repeated code.

if m.isProjectAdminFromCtx(ctx, user) {
return "", []interface{}{}, nil
// Now we do the actual update to the db model
switch rs.State {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we do this in 2 steps instead of doing it in the first switch? It's also because it's making me a bit confused that you can change both state and hidden fileds of a share, but then, the state overwrites the hidden value? Not sure I'm making sense...

@diocas
Copy link

diocas commented Jan 23, 2025

As discussed via MM, I would also like to confirm that null values (via manually inserted rows) do not break anything. (or, we just set annotations with the proper sql configs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants