Skip to content

Commit

Permalink
fix(connections): add FK to end users (#2883)
Browse files Browse the repository at this point in the history
## Describe your changes

Contributes to
https://linear.app/nango/issue/NAN-1944/connect-end-users-to-connections

- Add Foreign Key from connections to end_users
Implementation will come after

- Add Foreign Key from oauth_session to connect_session to be able to
get back the session on the callback
Implementation will come after
  • Loading branch information
bodinsamuel authored Oct 24, 2024
1 parent 5f5d9e3 commit 7bc3a17
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {import('knex').Knex} knex
*/
exports.up = async function (knex) {
await knex.raw(`ALTER TABLE "_nango_connections" ADD COLUMN "end_user_id" int4`);
await knex.raw(`ALTER TABLE "_nango_connections" ADD FOREIGN KEY ("end_user_id") REFERENCES "end_users" ("id") ON DELETE SET NULL`);

await knex.raw(`ALTER TABLE "_nango_oauth_sessions" ADD COLUMN "connect_session_id" int4`);
await knex.raw(`ALTER TABLE "_nango_oauth_sessions" ADD FOREIGN KEY ("connect_session_id") REFERENCES "connect_sessions" ("id") ON DELETE SET NULL`);
};

/**
* @param {import('knex').Knex} knex
*/
exports.down = async function (knex) {
await knex.raw(`ALTER TABLE "_nango_connections" DROP COLUMN "end_user_id"`);
await knex.raw(`ALTER TABLE "_nango_oauth_sessions" DROP COLUMN "connect_session_id"`);
};
1 change: 1 addition & 0 deletions packages/server/lib/controllers/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class OAuthController {
authMode: provider.auth_mode,
codeVerifier: crypto.randomBytes(24).toString('hex'),
id: uuid.v1(),
connectSessionId: null,
connectionConfig,
environmentId,
webSocketClientId: wsClientId,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/models/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface OAuthSession {
callbackUrl: string;
authMode: AuthModeType;
id: string;
connectSessionId: number | null;
connectionConfig: Record<string, string>;
environmentId: number;
webSocketClientId: string | undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/models/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ConnectionConfig = Record<string, any>;
export interface BaseConnection extends TimestampsAndDeleted {
id?: number;
config_id?: number;
end_user_id?: number;
provider_config_key: string; // TO deprecate
connection_id: string;
connection_config: ConnectionConfig;
Expand Down
1 change: 1 addition & 0 deletions packages/types/lib/connection/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ConnectionConfig = Record<string, any>;
export interface BaseConnection extends TimestampsAndDeleted {
id?: number;
config_id?: number;
end_user_id?: number;
provider_config_key: string; // TO deprecate
connection_id: string;
connection_config: ConnectionConfig;
Expand Down

0 comments on commit 7bc3a17

Please sign in to comment.