From 7bc3a17f2442928721c6a65181bdd53437175668 Mon Sep 17 00:00:00 2001 From: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:50:42 +0200 Subject: [PATCH] fix(connections): add FK to end users (#2883) ## 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 --- .../20241024094439_connections_end_user.cjs | 18 ++++++++++++++++++ .../server/lib/controllers/oauth.controller.ts | 1 + packages/shared/lib/models/Auth.ts | 1 + packages/shared/lib/models/Connection.ts | 1 + packages/types/lib/connection/db.ts | 1 + 5 files changed, 22 insertions(+) create mode 100644 packages/database/lib/migrations/20241024094439_connections_end_user.cjs diff --git a/packages/database/lib/migrations/20241024094439_connections_end_user.cjs b/packages/database/lib/migrations/20241024094439_connections_end_user.cjs new file mode 100644 index 0000000000..88fa40a13f --- /dev/null +++ b/packages/database/lib/migrations/20241024094439_connections_end_user.cjs @@ -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"`); +}; diff --git a/packages/server/lib/controllers/oauth.controller.ts b/packages/server/lib/controllers/oauth.controller.ts index 9a77603da8..0b3032025c 100644 --- a/packages/server/lib/controllers/oauth.controller.ts +++ b/packages/server/lib/controllers/oauth.controller.ts @@ -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, diff --git a/packages/shared/lib/models/Auth.ts b/packages/shared/lib/models/Auth.ts index 19ef5a5c1d..1663565c41 100644 --- a/packages/shared/lib/models/Auth.ts +++ b/packages/shared/lib/models/Auth.ts @@ -24,6 +24,7 @@ export interface OAuthSession { callbackUrl: string; authMode: AuthModeType; id: string; + connectSessionId: number | null; connectionConfig: Record; environmentId: number; webSocketClientId: string | undefined; diff --git a/packages/shared/lib/models/Connection.ts b/packages/shared/lib/models/Connection.ts index 6888621d39..3f3dc5101b 100644 --- a/packages/shared/lib/models/Connection.ts +++ b/packages/shared/lib/models/Connection.ts @@ -7,6 +7,7 @@ export type ConnectionConfig = Record; 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; diff --git a/packages/types/lib/connection/db.ts b/packages/types/lib/connection/db.ts index 66ec6e5866..62150f562d 100644 --- a/packages/types/lib/connection/db.ts +++ b/packages/types/lib/connection/db.ts @@ -10,6 +10,7 @@ export type ConnectionConfig = Record; 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;