Skip to content

Commit 9bebda4

Browse files
committed
prune old guest user to remove need to wipe data on upgrade
1 parent 0388829 commit 9bebda4

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

docs/self-hosting/upgrade/v3-to-v4-guide.mdx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ Please note that the following features are no longer supported in v4:
1111
- Unauthorized access to a Sourcebot deployment. Authentication is now required, and unauthorized access requires an unlimited seat [enterprise license](/self-hosting/license-key)
1212
</Warning>
1313

14-
<Warning>
15-
If your Sourcebot deployment doesn't have authentication enabled, or uses multi-tenancy mode, you'll need to reindex all your repos
16-
</Warning>
17-
1814
### If your deployment doesn't have authentication enabled
1915
<Steps>
2016
<Step title="Spin down deployment">
2117
</Step>
22-
<Step title="Reset Sourcebot data">
23-
Delete your Sourcebot cache (`.sourcebot`) directory. If you're using Sourcebot with an external postgres instance, reset the database in your instance used by Sourcebot.
24-
</Step>
2518
<Step title="Set AUTH_URL environment variable if needed">
2619
If your Sourcebot instance is deployed behind a domain (ex. `https://sourcebot.yourcompany.com`) you **must** set the `AUTH_URL` environment variable to your deployment domain.
2720
</Step>
@@ -58,7 +51,7 @@ navigating to **Settings -> Members**. Emails can be sent on organization join r
5851

5952
### If your deployment uses multi-tenancy mode
6053

61-
Unfortunately, multi-tenancy mode is no longer officially supported in v4. To upgrade to v4, you'll need to unset the `SOURCEBOT_TENANCY_MODE` environment variable. You can then follow the [instructions above](/self-hosting/upgrade/v3-to-v4-guide#if-your-deployment-doesnt-have-authentication-enabled-i-e-sourcebot-auth-enabled)
54+
Unfortunately, multi-tenancy mode is no longer officially supported in v4. To upgrade to v4, you'll need to unset the `SOURCEBOT_TENANCY_MODE` environment variable and wipe your Sourcebot cache. You can then follow the [instructions above](/self-hosting/upgrade/v3-to-v4-guide#if-your-deployment-doesnt-have-authentication-enabled-i-e-sourcebot-auth-enabled)
6255
to finish upgrading to v4 in single-tenant mode.
6356

6457
## Troubleshooting

packages/web/src/auth.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,18 @@ const onCreateUser = async ({ user }: { user: AuthJsUser }) => {
156156
throw new Error("Default org not found on single tenant user creation");
157157
}
158158

159+
// We can't use the getOrgMembers action here because we're not authed yet
160+
const members = await prisma.userToOrg.findMany({
161+
where: {
162+
orgId: SINGLE_TENANT_ORG_ID,
163+
role: {
164+
not: OrgRole.GUEST,
165+
}
166+
},
167+
});
168+
159169
// Only the first user to sign up will be an owner of the default org.
160-
const isFirstUser = defaultOrg.members.length === 0;
170+
const isFirstUser = members.length === 0;
161171
if (isFirstUser) {
162172
await prisma.$transaction(async (tx) => {
163173
await tx.org.update({

packages/web/src/initialize.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ConnectionSyncStatus, Prisma, RepoIndexingStatus } from '@sourcebot/db';
1+
import { ConnectionSyncStatus, OrgRole, Prisma, RepoIndexingStatus } from '@sourcebot/db';
22
import { env } from './env.mjs';
33
import { prisma } from "@/prisma";
4-
import { SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_NAME } from './lib/constants';
4+
import { SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_NAME, SOURCEBOT_GUEST_USER_ID } from './lib/constants';
55
import { readFile } from 'fs/promises';
66
import { watch } from 'fs';
77
import stripJsonComments from 'strip-json-comments';
@@ -158,6 +158,31 @@ const syncDeclarativeConfig = async (configPath: string) => {
158158
await syncSearchContexts(config.contexts);
159159
}
160160

161+
const pruneOldGuestUser = async () => {
162+
// The old guest user doesn't have the GUEST role
163+
const guestUser = await prisma.userToOrg.findUnique({
164+
where: {
165+
orgId_userId: {
166+
orgId: SINGLE_TENANT_ORG_ID,
167+
userId: SOURCEBOT_GUEST_USER_ID,
168+
},
169+
role: {
170+
not: OrgRole.GUEST,
171+
}
172+
},
173+
});
174+
175+
if (guestUser) {
176+
await prisma.user.delete({
177+
where: {
178+
id: guestUser.userId,
179+
},
180+
});
181+
182+
console.log(`Deleted old guest user ${guestUser.userId}`);
183+
}
184+
}
185+
161186
const initSingleTenancy = async () => {
162187
await prisma.org.upsert({
163188
where: {
@@ -171,6 +196,10 @@ const initSingleTenancy = async () => {
171196
}
172197
});
173198

199+
// This is needed because v4 introduces the GUEST org role as well as making authentication required.
200+
// To keep things simple, we'll just delete the old guest user if it exists in the DB
201+
await pruneOldGuestUser();
202+
174203
const hasPublicAccessEntitlement = hasEntitlement("public-access");
175204
if (hasPublicAccessEntitlement) {
176205
const res = await createGuestUser(SINGLE_TENANT_ORG_DOMAIN);

packages/web/src/lib/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export const TEAM_FEATURES = [
2424

2525
export const MOBILE_UNSUPPORTED_SPLASH_SCREEN_DISMISSED_COOKIE_NAME = 'sb.mobile-unsupported-splash-screen-dismissed';
2626

27+
// NOTE: changing SOURCEBOT_GUEST_USER_ID may break backwards compatibility since this value is used
28+
// to detect old guest users in the DB. If you change this value ensure it doesn't break upgrade flows
2729
export const SOURCEBOT_GUEST_USER_ID = '1';
2830
export const SOURCEBOT_GUEST_USER_EMAIL = 'guest@sourcebot.dev';
2931
export const SINGLE_TENANT_ORG_ID = 1;

0 commit comments

Comments
 (0)