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

Add organization ID, and remove FKs and sequences #2896

Merged
merged 6 commits into from Aug 11, 2022
Merged

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Aug 9, 2022

Summary

This PR cherry-picks 501cd0d, the data parts of 02cd02c, and a bit of 0d1faf9 for merging into main. Best viewed by individual commit.

I made a few changes as well:

  • use interfaces instead of reflection for working with models
  • use easier to read migration IDs, we started using these recently for new migraiotns
  • use single statements for migrations
  • remove double quotes from queries, and use caps consistently for sql keywords
  • make the selector ByOrgID instead of ByOrg
  • move the OrganizationID to an OrganizationMember so that tables that don't use it don't receive an org_id
  • continue to require not-null for primary keys
  • cascade the deletes of sequences to remove the other sequence data

I had to cherry-pick a couple of changes from #2862 so that AutoMigrate wouldn't create the wrong schema.

@dnephin dnephin changed the title Dnephin/add org Add organization ID, and remove FKs and sequences Aug 9, 2022
Comment on lines +133 to +127
member, ok := model.(orgMember)
if !ok {
return
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we are ready to, we can make SetOrganizationID a required method, and implement no-op implementations on the other tables. That gives us the same safeguards as putting it in the base model, without adding unused organization_id fields.

* feat: add migration for org ids

* maintain: make the linter happy
@dnephin dnephin force-pushed the dnephin/add-org-id branch 3 times, most recently from 2dc7e90 to 3d4691f Compare August 10, 2022 03:52
@@ -17,6 +18,7 @@ func CreatePasswordResetToken(db *gorm.DB, user *models.Identity) (*models.Passw
}

prt := &models.PasswordResetToken{
ID: uid.New(),
Copy link
Contributor Author

@dnephin dnephin Aug 10, 2022

Choose a reason for hiding this comment

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

I forgot to say in the commit message, but this was to fix a test failure. On the org_collab branch we removed all the required not-null for primary keys, so this was not failing I guess? I did not include that part, so we need to set an primary key for this type before saving it. It does not use Model, so it doesn't get set that way.

Co-Authored-By: Steven Soroka <steven@infrahq.com>
internal/server/data/data.go Show resolved Hide resolved
internal/server/data/data.go Outdated Show resolved Hide resolved
internal/server/data/data.go Outdated Show resolved Hide resolved
internal/server/data/data.go Outdated Show resolved Hide resolved
internal/server/data/data.go Show resolved Hide resolved
internal/server/data/data.go Show resolved Hide resolved
internal/server/data/organization.go Show resolved Hide resolved

ALTER SEQUENCE settings_id_seq OWNED BY settings.id;

ALTER TABLE ONLY access_keys ALTER COLUMN id SET DEFAULT nextval('access_keys_id_seq'::regclass);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we still have these alter table statements? We manually set the ID by they seem ok to have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll let @ssoroka comment as well, because I think he removed them originally.

I like removing them because we don't use them, and I think having these sequences could hide bugs from us in testing. I think it's better off if the column is set to NOT NULL without a sequence to force us to set values.

internal/server/data/data.go Outdated Show resolved Hide resolved
internal/server/data/data.go Show resolved Hide resolved
internal/server/data/data.go Outdated Show resolved Hide resolved
internal/server/data/migrations_test.go Show resolved Hide resolved
@dnephin dnephin mentioned this pull request Aug 10, 2022
13 tasks
BruceMacD and others added 4 commits August 10, 2022 15:24
Co-Authored-By: Daniel Nephin <dnephin@infrahq.com>
To indicate lack of org scope.

Cherry-picked from org_collab branch.

Co-Authored-By: Steven Soroka <steven@infrahq.com>
Move SaveSettings out of the common helper, and instead move it to the two tests that need
it.
Copy link
Collaborator

@BruceMacD BruceMacD left a comment

Choose a reason for hiding this comment

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

Looks good to me pending Steven's comments

@dnephin dnephin merged commit 56952ac into main Aug 11, 2022
@dnephin dnephin deleted the dnephin/add-org-id branch August 11, 2022 16:53
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.

None yet

4 participants