-
Notifications
You must be signed in to change notification settings - Fork 8
feat: persist preferred project by account key #1366
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
Open
jonathanlab
wants to merge
1
commit into
03-30-refactor_hook_up_renderer_auth_logic_to_service
Choose a base branch
from
03-30-feat_persist_preferred_project_by_account_key
base: 03-30-refactor_hook_up_renderer_auth_logic_to_service
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
Changes from all commits
Commits
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
This file contains hidden or 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,9 @@ | ||
| CREATE TABLE `auth_preferences` ( | ||
| `account_key` text NOT NULL, | ||
| `cloud_region` text NOT NULL, | ||
| `last_selected_project_id` integer, | ||
| `created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL, | ||
| `updated_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL | ||
| ); | ||
| --> statement-breakpoint | ||
| CREATE INDEX `auth_preferences_account_region_idx` ON `auth_preferences` (`account_key`,`cloud_region`); | ||
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
apps/code/src/main/db/repositories/auth-preference-repository.mock.ts
This file contains hidden or 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,57 @@ | ||
| import type { | ||
| AuthPreference, | ||
| IAuthPreferenceRepository, | ||
| PersistAuthPreferenceInput, | ||
| } from "./auth-preference-repository"; | ||
|
|
||
| export interface MockAuthPreferenceRepository | ||
| extends IAuthPreferenceRepository { | ||
| _preferences: AuthPreference[]; | ||
| } | ||
|
|
||
| export function createMockAuthPreferenceRepository(): MockAuthPreferenceRepository { | ||
| let preferences: AuthPreference[] = []; | ||
|
|
||
| const clone = (value: AuthPreference): AuthPreference => ({ ...value }); | ||
|
|
||
| return { | ||
| get _preferences() { | ||
| return preferences.map(clone); | ||
| }, | ||
| set _preferences(value) { | ||
| preferences = value.map(clone); | ||
| }, | ||
| get: (accountKey, cloudRegion) => { | ||
| const preference = preferences.find( | ||
| (entry) => | ||
| entry.accountKey === accountKey && entry.cloudRegion === cloudRegion, | ||
| ); | ||
| return preference ? clone(preference) : null; | ||
| }, | ||
| save: (input: PersistAuthPreferenceInput) => { | ||
| const timestamp = new Date().toISOString(); | ||
| const existingIndex = preferences.findIndex( | ||
| (entry) => | ||
| entry.accountKey === input.accountKey && | ||
| entry.cloudRegion === input.cloudRegion, | ||
| ); | ||
|
|
||
| const row: AuthPreference = { | ||
| accountKey: input.accountKey, | ||
| cloudRegion: input.cloudRegion, | ||
| lastSelectedProjectId: input.lastSelectedProjectId, | ||
| createdAt: | ||
| existingIndex >= 0 ? preferences[existingIndex].createdAt : timestamp, | ||
| updatedAt: timestamp, | ||
| }; | ||
|
|
||
| if (existingIndex >= 0) { | ||
| preferences[existingIndex] = row; | ||
| } else { | ||
| preferences.push(row); | ||
| } | ||
|
|
||
| return clone(row); | ||
| }, | ||
| }; | ||
| } |
89 changes: 89 additions & 0 deletions
89
apps/code/src/main/db/repositories/auth-preference-repository.ts
This file contains hidden or 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,89 @@ | ||
| import { and, eq } from "drizzle-orm"; | ||
| import { inject, injectable } from "inversify"; | ||
| import { MAIN_TOKENS } from "../../di/tokens"; | ||
| import { authPreferences } from "../schema"; | ||
| import type { DatabaseService } from "../service"; | ||
|
|
||
| export type AuthPreference = typeof authPreferences.$inferSelect; | ||
| export type NewAuthPreference = typeof authPreferences.$inferInsert; | ||
|
|
||
| export interface PersistAuthPreferenceInput { | ||
| accountKey: string; | ||
| cloudRegion: "us" | "eu" | "dev"; | ||
| lastSelectedProjectId: number | null; | ||
| } | ||
|
|
||
| export interface IAuthPreferenceRepository { | ||
| get( | ||
| accountKey: string, | ||
| cloudRegion: "us" | "eu" | "dev", | ||
| ): AuthPreference | null; | ||
| save(input: PersistAuthPreferenceInput): AuthPreference; | ||
| } | ||
|
|
||
| const now = () => new Date().toISOString(); | ||
|
|
||
| @injectable() | ||
| export class AuthPreferenceRepository implements IAuthPreferenceRepository { | ||
| constructor( | ||
| @inject(MAIN_TOKENS.DatabaseService) | ||
| private readonly databaseService: DatabaseService, | ||
| ) {} | ||
|
|
||
| private get db() { | ||
| return this.databaseService.db; | ||
| } | ||
|
|
||
| get( | ||
| accountKey: string, | ||
| cloudRegion: "us" | "eu" | "dev", | ||
| ): AuthPreference | null { | ||
| return ( | ||
| this.db | ||
| .select() | ||
| .from(authPreferences) | ||
| .where( | ||
| and( | ||
| eq(authPreferences.accountKey, accountKey), | ||
| eq(authPreferences.cloudRegion, cloudRegion), | ||
| ), | ||
| ) | ||
| .limit(1) | ||
| .get() ?? null | ||
| ); | ||
| } | ||
|
|
||
| save(input: PersistAuthPreferenceInput): AuthPreference { | ||
| const timestamp = now(); | ||
| const existing = this.get(input.accountKey, input.cloudRegion); | ||
|
|
||
| const row: NewAuthPreference = { | ||
| accountKey: input.accountKey, | ||
| cloudRegion: input.cloudRegion, | ||
| lastSelectedProjectId: input.lastSelectedProjectId, | ||
| createdAt: existing?.createdAt ?? timestamp, | ||
| updatedAt: timestamp, | ||
| }; | ||
|
|
||
| if (existing) { | ||
| this.db | ||
| .update(authPreferences) | ||
| .set(row) | ||
| .where( | ||
| and( | ||
| eq(authPreferences.accountKey, input.accountKey), | ||
| eq(authPreferences.cloudRegion, input.cloudRegion), | ||
| ), | ||
| ) | ||
| .run(); | ||
| } else { | ||
| this.db.insert(authPreferences).values(row).run(); | ||
| } | ||
|
|
||
| const saved = this.get(input.accountKey, input.cloudRegion); | ||
| if (!saved) { | ||
| throw new Error("Failed to persist auth preference"); | ||
| } | ||
| return saved; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
The
auth_preferencestable is missing a PRIMARY KEY or UNIQUE constraint on(account_key, cloud_region). This will allow duplicate rows to be inserted for the same account and region combination, causing unpredictable behavior in thegetandsavemethods.Impact: Multiple preference records can exist for the same account/region, and
LIMIT 1queries will return arbitrary rows. The update logic insave()may also fail to work correctly.Fix: Add a composite primary key:
Spotted by Graphite

Is this helpful? React 👍 or 👎 to let us know.