From 3d0a916a50c2f70cdc822ccb8a7a6ee9d5a5d0fe Mon Sep 17 00:00:00 2001 From: Flavien David Date: Thu, 5 Dec 2024 22:36:35 +0100 Subject: [PATCH] Biigint ftw wave 2 (#9170) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * BigInt for connectors DB * ✨ * tmp * Fix connectors foreign key constraint * Fix safety flag on migration * 👕 --- connectors/migrations/db/migration_40.sql | 62 ++++++++++ connectors/src/lib/models/confluence.ts | 52 ++------- connectors/src/lib/models/github.ts | 76 ++---------- connectors/src/lib/models/google_drive.ts | 110 +++++------------- connectors/src/lib/models/intercom.ts | 76 ++---------- connectors/src/lib/models/microsoft.ts | 70 +++-------- connectors/src/lib/models/notion.ts | 76 ++---------- connectors/src/lib/models/remote_databases.ts | 43 +------ connectors/src/lib/models/slack.ts | 83 +++---------- connectors/src/lib/models/snowflake.ts | 21 +--- connectors/src/lib/models/webcrawler.ts | 54 ++------- connectors/src/lib/models/zendesk.ts | 76 ++---------- .../storage/models/connector_model.ts | 20 +--- connectors/src/resources/storage/wrappers.ts | 51 ++++++++ front/lib/resources/storage/wrappers.ts | 21 ++-- front/migrations/db/migration_125.sql | 16 +-- 16 files changed, 262 insertions(+), 645 deletions(-) create mode 100644 connectors/migrations/db/migration_40.sql create mode 100644 connectors/src/resources/storage/wrappers.ts diff --git a/connectors/migrations/db/migration_40.sql b/connectors/migrations/db/migration_40.sql new file mode 100644 index 000000000000..d091cc74c588 --- /dev/null +++ b/connectors/migrations/db/migration_40.sql @@ -0,0 +1,62 @@ +\set ON_ERROR_STOP on + +-- Check if the safety flag variable exists +\if :i_know_what_i_do +-- your migration code here +\else +\echo 'Safety flag not set. Run with: psql -v i_know_what_i_do=true -f migration_40.sql' +\q +\endif + +DO $$ +DECLARE + r RECORD; +BEGIN + -- First convert all PKs + FOR r IN ( + SELECT + t.table_name, + c.column_name + FROM information_schema.tables t + JOIN information_schema.columns c + ON t.table_name = c.table_name + AND t.table_schema = c.table_schema + JOIN information_schema.key_column_usage k + ON c.column_name = k.column_name + AND c.table_name = k.table_name + WHERE c.data_type = 'integer' + AND t.table_schema = 'public' + AND EXISTS ( + SELECT 1 + FROM information_schema.table_constraints tc + WHERE tc.constraint_type = 'PRIMARY KEY' + AND tc.table_name = t.table_name + AND tc.constraint_name = k.constraint_name + ) + ) LOOP + RAISE NOTICE 'Converting PK column % in table % to bigint', r.column_name, r.table_name; + EXECUTE format('ALTER TABLE %I ALTER COLUMN %I TYPE bigint', r.table_name, r.column_name); + END LOOP; + + -- Then convert all FKs + FOR r IN ( + SELECT DISTINCT + t.table_name, + c.column_name + FROM information_schema.tables t + JOIN information_schema.columns c + ON t.table_name = c.table_name + AND t.table_schema = c.table_schema + JOIN information_schema.key_column_usage k + ON c.column_name = k.column_name + AND c.table_name = k.table_name + JOIN information_schema.table_constraints tc + ON tc.constraint_name = k.constraint_name + WHERE c.data_type = 'integer' + AND t.table_schema = 'public' + AND tc.constraint_type = 'FOREIGN KEY' + ) LOOP + RAISE NOTICE 'Converting FK column % in table % to bigint', r.column_name, r.table_name; + EXECUTE format('ALTER TABLE %I ALTER COLUMN %I TYPE bigint', r.table_name, r.column_name); + END LOOP; +END $$; diff --git a/connectors/src/lib/models/confluence.ts b/connectors/src/lib/models/confluence.ts index c0b55d10a5df..18ae9c09e17f 100644 --- a/connectors/src/lib/models/confluence.ts +++ b/connectors/src/lib/models/confluence.ts @@ -1,19 +1,11 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class ConfluenceConfiguration extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ConfluenceConfiguration extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -25,11 +17,6 @@ export class ConfluenceConfiguration extends Model< } ConfluenceConfiguration.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, cloudId: { type: DataTypes.STRING, allowNull: false, @@ -65,11 +52,7 @@ ConfluenceConfiguration.init( ConnectorModel.hasOne(ConfluenceConfiguration); // ConfluenceSpace stores the global spaces selected by the user to sync. -export class ConfluenceSpace extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ConfluenceSpace extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -79,15 +62,6 @@ export class ConfluenceSpace extends Model< } ConfluenceSpace.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -117,14 +91,13 @@ ConfluenceSpace.init( indexes: [{ fields: ["connectorId", "spaceId"], unique: true }], } ); -ConnectorModel.hasOne(ConfluenceSpace); +ConnectorModel.hasOne(ConfluenceSpace, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); // ConfluencePages stores the pages. -export class ConfluencePage extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ConfluencePage extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastVisitedAt: CreationOptional; @@ -141,11 +114,6 @@ export class ConfluencePage extends Model< } ConfluencePage.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/github.ts b/connectors/src/lib/models/github.ts index 12496229afe5..64f91d072ef5 100644 --- a/connectors/src/lib/models/github.ts +++ b/connectors/src/lib/models/github.ts @@ -1,19 +1,11 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class GithubConnectorState extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GithubConnectorState extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -25,11 +17,6 @@ export class GithubConnectorState extends Model< } GithubConnectorState.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -65,11 +52,7 @@ GithubConnectorState.init( ); ConnectorModel.hasOne(GithubConnectorState); -export class GithubIssue extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GithubIssue extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -82,11 +65,6 @@ export class GithubIssue extends Model< } GithubIssue.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -122,11 +100,7 @@ GithubIssue.init( ); ConnectorModel.hasMany(GithubIssue); -export class GithubDiscussion extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GithubDiscussion extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -137,11 +111,6 @@ export class GithubDiscussion extends Model< } GithubDiscussion.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -173,11 +142,7 @@ GithubDiscussion.init( ); ConnectorModel.hasMany(GithubDiscussion); -export class GithubCodeRepository extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GithubCodeRepository extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastSeenAt: CreationOptional; @@ -194,11 +159,6 @@ export class GithubCodeRepository extends Model< } GithubCodeRepository.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -249,11 +209,7 @@ GithubCodeRepository.init( ); ConnectorModel.hasMany(GithubCodeRepository); -export class GithubCodeFile extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GithubCodeFile extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastSeenAt: CreationOptional; @@ -271,11 +227,6 @@ export class GithubCodeFile extends Model< } GithubCodeFile.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -332,11 +283,7 @@ GithubCodeFile.init( ); ConnectorModel.hasMany(GithubCodeFile); -export class GithubCodeDirectory extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GithubCodeDirectory extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastSeenAt: CreationOptional; @@ -353,11 +300,6 @@ export class GithubCodeDirectory extends Model< } GithubCodeDirectory.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/google_drive.ts b/connectors/src/lib/models/google_drive.ts index 0ccf37f2eac9..8c93265d5888 100644 --- a/connectors/src/lib/models/google_drive.ts +++ b/connectors/src/lib/models/google_drive.ts @@ -1,19 +1,11 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class GoogleDriveConfig extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GoogleDriveConfig extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -23,11 +15,6 @@ export class GoogleDriveConfig extends Model< } GoogleDriveConfig.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -38,10 +25,6 @@ GoogleDriveConfig.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, pdfEnabled: { type: DataTypes.BOOLEAN, allowNull: false, @@ -64,14 +47,13 @@ GoogleDriveConfig.init( indexes: [{ fields: ["connectorId"], unique: true }], } ); -ConnectorModel.hasOne(GoogleDriveConfig); +ConnectorModel.hasOne(GoogleDriveConfig, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); // GoogleDriveFolders stores the folders selected by the user to sync. -export class GoogleDriveFolders extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GoogleDriveFolders extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -79,11 +61,6 @@ export class GoogleDriveFolders extends Model< } GoogleDriveFolders.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -94,10 +71,6 @@ GoogleDriveFolders.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, folderId: { type: DataTypes.STRING, allowNull: false, @@ -109,14 +82,13 @@ GoogleDriveFolders.init( indexes: [{ fields: ["connectorId", "folderId"], unique: true }], } ); -ConnectorModel.hasOne(GoogleDriveFolders); +ConnectorModel.hasOne(GoogleDriveFolders, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); // GoogleDriveFiles stores files and folders synced from Google Drive. -export class GoogleDriveFiles extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GoogleDriveFiles extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastSeenTs: Date | null; @@ -131,11 +103,6 @@ export class GoogleDriveFiles extends Model< } GoogleDriveFiles.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -158,10 +125,6 @@ GoogleDriveFiles.init( type: DataTypes.STRING, allowNull: true, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, dustFileId: { type: DataTypes.STRING, allowNull: false, @@ -194,13 +157,12 @@ GoogleDriveFiles.init( ], } ); -ConnectorModel.hasOne(GoogleDriveFiles); +ConnectorModel.hasOne(GoogleDriveFiles, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); -export class GoogleDriveSheet extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GoogleDriveSheet extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -210,11 +172,6 @@ export class GoogleDriveSheet extends Model< } GoogleDriveSheet.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -225,10 +182,6 @@ GoogleDriveSheet.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, driveFileId: { type: DataTypes.STRING, allowNull: false, @@ -250,15 +203,14 @@ GoogleDriveSheet.init( ], } ); -ConnectorModel.hasOne(GoogleDriveSheet); +ConnectorModel.hasOne(GoogleDriveSheet, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); // Sync Token are the equivalent of a timestamp for syncing the delta // between the last sync and the current sync. -export class GoogleDriveSyncToken extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class GoogleDriveSyncToken extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; // The driveId is the Google Drive Id of the user's drive. @@ -271,11 +223,6 @@ export class GoogleDriveSyncToken extends Model< } GoogleDriveSyncToken.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -286,10 +233,6 @@ GoogleDriveSyncToken.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, driveId: { type: DataTypes.STRING, allowNull: false, @@ -305,4 +248,7 @@ GoogleDriveSyncToken.init( indexes: [{ fields: ["connectorId", "driveId"], unique: true }], } ); -ConnectorModel.hasOne(GoogleDriveSyncToken); +ConnectorModel.hasOne(GoogleDriveSyncToken, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); diff --git a/connectors/src/lib/models/intercom.ts b/connectors/src/lib/models/intercom.ts index c6a21fbce6eb..9223430160ae 100644 --- a/connectors/src/lib/models/intercom.ts +++ b/connectors/src/lib/models/intercom.ts @@ -1,20 +1,12 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import type { IntercomSyncAllConversationsStatus } from "@connectors/connectors/intercom/lib/types"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class IntercomWorkspace extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class IntercomWorkspace extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -30,11 +22,6 @@ export class IntercomWorkspace extends Model< } IntercomWorkspace.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -88,11 +75,7 @@ IntercomWorkspace.init( ); ConnectorModel.hasMany(IntercomWorkspace); -export class IntercomHelpCenter extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class IntercomHelpCenter extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -110,11 +93,6 @@ export class IntercomHelpCenter extends Model< } IntercomHelpCenter.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -171,11 +149,7 @@ IntercomHelpCenter.init( ); ConnectorModel.hasMany(IntercomHelpCenter); -export class IntercomCollection extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class IntercomCollection extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -197,11 +171,6 @@ export class IntercomCollection extends Model< IntercomCollection.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -265,11 +234,7 @@ IntercomCollection.init( ); ConnectorModel.hasMany(IntercomCollection); -export class IntercomArticle extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class IntercomArticle extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -293,11 +258,6 @@ export class IntercomArticle extends Model< IntercomArticle.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -369,11 +329,7 @@ IntercomArticle.init( ); ConnectorModel.hasMany(IntercomArticle); -export class IntercomTeam extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class IntercomTeam extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -388,11 +344,6 @@ export class IntercomTeam extends Model< IntercomTeam.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -434,11 +385,7 @@ IntercomTeam.init( ); ConnectorModel.hasMany(IntercomTeam); -export class IntercomConversation extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class IntercomConversation extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -453,11 +400,6 @@ export class IntercomConversation extends Model< IntercomConversation.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/microsoft.ts b/connectors/src/lib/models/microsoft.ts index 5114b54d89d7..b4cfd37d97c4 100644 --- a/connectors/src/lib/models/microsoft.ts +++ b/connectors/src/lib/models/microsoft.ts @@ -1,20 +1,12 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import type { MicrosoftNodeType } from "@connectors/connectors/microsoft/lib/types"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class MicrosoftConfigurationModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class MicrosoftConfigurationModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -24,11 +16,6 @@ export class MicrosoftConfigurationModel extends Model< } MicrosoftConfigurationModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -39,10 +26,6 @@ MicrosoftConfigurationModel.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, pdfEnabled: { type: DataTypes.BOOLEAN, allowNull: false, @@ -66,17 +49,16 @@ MicrosoftConfigurationModel.init( } ); -ConnectorModel.hasMany(MicrosoftConfigurationModel); +ConnectorModel.hasMany(MicrosoftConfigurationModel, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); // MicrosoftRoot stores the drive/folders/channels selected by the user to sync. // In order to be able to uniquely identify each node, we store the GET path // to the item in the itemApiPath field (e.g. /drives/{drive-id}), except for the toplevel // sites-root and teams-root, which are stored as "sites-root" and "teams-root" respectively. -export class MicrosoftRootModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class MicrosoftRootModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -85,11 +67,6 @@ export class MicrosoftRootModel extends Model< } MicrosoftRootModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -100,10 +77,6 @@ MicrosoftRootModel.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, internalId: { type: DataTypes.STRING, allowNull: false, @@ -122,14 +95,13 @@ MicrosoftRootModel.init( ], } ); -ConnectorModel.hasMany(MicrosoftRootModel); +ConnectorModel.hasMany(MicrosoftRootModel, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); // MicrosftNode stores nodes (e.g. files, folder, channels, ...) synced from Microsoft. -export class MicrosoftNodeModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class MicrosoftNodeModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastSeenTs: Date | null; @@ -147,11 +119,6 @@ export class MicrosoftNodeModel extends Model< MicrosoftNodeModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -174,10 +141,6 @@ MicrosoftNodeModel.init( type: DataTypes.STRING, allowNull: true, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, internalId: { type: DataTypes.STRING(512), allowNull: false, @@ -217,4 +180,7 @@ MicrosoftNodeModel.init( ], } ); -ConnectorModel.hasMany(MicrosoftNodeModel); +ConnectorModel.hasMany(MicrosoftNodeModel, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); diff --git a/connectors/src/lib/models/notion.ts b/connectors/src/lib/models/notion.ts index f2a53d0696a1..a585a694ae31 100644 --- a/connectors/src/lib/models/notion.ts +++ b/connectors/src/lib/models/notion.ts @@ -1,20 +1,12 @@ import type { NotionBlockType, PageObjectProperties } from "@dust-tt/types"; -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class NotionConnectorState extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class NotionConnectorState extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -27,11 +19,6 @@ export class NotionConnectorState extends Model< } NotionConnectorState.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -63,11 +50,7 @@ NotionConnectorState.init( ); ConnectorModel.hasOne(NotionConnectorState); -export class NotionPage extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class NotionPage extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -88,11 +71,6 @@ export class NotionPage extends Model< } NotionPage.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -162,11 +140,7 @@ NotionPage.init( ); ConnectorModel.hasMany(NotionPage); -export class NotionDatabase extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class NotionDatabase extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -191,11 +165,6 @@ export class NotionDatabase extends Model< NotionDatabase.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -281,11 +250,7 @@ ConnectorModel.hasMany(NotionDatabase); // This is because it's a cache table that generates a lot of writes and we don't want to fill up the WAL. // It's also a cache table, so we don't care if we lose data. // This table is not replicated to the read replica, and all data is lost on a failover. -export class NotionConnectorPageCacheEntry extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class NotionConnectorPageCacheEntry extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -306,11 +271,6 @@ export class NotionConnectorPageCacheEntry extends Model< } NotionConnectorPageCacheEntry.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -388,11 +348,7 @@ ConnectorModel.hasMany(NotionConnectorPageCacheEntry); // This is because it's a cache table that generates a lot of writes and we don't want to fill up the WAL. // It's also a cache table, so we don't care if we lose data. // This table is not replicated to the read replica, and all data is lost on a failover. -export class NotionConnectorBlockCacheEntry extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class NotionConnectorBlockCacheEntry extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -412,11 +368,6 @@ export class NotionConnectorBlockCacheEntry extends Model< } NotionConnectorBlockCacheEntry.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -482,11 +433,7 @@ ConnectorModel.hasMany(NotionConnectorBlockCacheEntry); // This is because it's a cache table that generates a lot of writes and we don't want to fill up the WAL. // It's also a cache table, so we don't care if we lose data. // This table is not replicated to the read replica, and all data is lost on a failover. -export class NotionConnectorResourcesToCheckCacheEntry extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class NotionConnectorResourcesToCheckCacheEntry extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -499,11 +446,6 @@ export class NotionConnectorResourcesToCheckCacheEntry extends Model< } NotionConnectorResourcesToCheckCacheEntry.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/remote_databases.ts b/connectors/src/lib/models/remote_databases.ts index 473e89a0bc00..96627499e3ed 100644 --- a/connectors/src/lib/models/remote_databases.ts +++ b/connectors/src/lib/models/remote_databases.ts @@ -1,21 +1,13 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; type RemoteTablePermission = "selected" | "inherited"; // todo Daph move in next PR -export class RemoteDatabaseModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class RemoteDatabaseModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -26,11 +18,6 @@ export class RemoteDatabaseModel extends Model< } RemoteDatabaseModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, internalId: { type: DataTypes.STRING, allowNull: false, @@ -62,11 +49,7 @@ ConnectorModel.hasMany(RemoteDatabaseModel, { }); RemoteDatabaseModel.belongsTo(ConnectorModel); -export class RemoteSchemaModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class RemoteSchemaModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -79,11 +62,6 @@ export class RemoteSchemaModel extends Model< } RemoteSchemaModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, internalId: { type: DataTypes.STRING, allowNull: false, @@ -119,11 +97,7 @@ ConnectorModel.hasMany(RemoteSchemaModel, { }); RemoteSchemaModel.belongsTo(ConnectorModel); -export class RemoteTableModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class RemoteTableModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare lastUpsertedAt: CreationOptional | null; @@ -139,11 +113,6 @@ export class RemoteTableModel extends Model< } RemoteTableModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, internalId: { type: DataTypes.STRING, allowNull: false, diff --git a/connectors/src/lib/models/slack.ts b/connectors/src/lib/models/slack.ts index 76c0bfbd193f..6ba99e21bb8e 100644 --- a/connectors/src/lib/models/slack.ts +++ b/connectors/src/lib/models/slack.ts @@ -2,22 +2,14 @@ import type { ConnectorPermission, SlackbotWhitelistType, } from "@dust-tt/types"; -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class SlackConfigurationModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class SlackConfigurationModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare slackTeamId: string; @@ -29,11 +21,6 @@ export class SlackConfigurationModel extends Model< } SlackConfigurationModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -78,11 +65,7 @@ SlackConfigurationModel.init( ); ConnectorModel.hasOne(SlackConfigurationModel); -export class SlackMessages extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class SlackMessages extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -92,11 +75,6 @@ export class SlackMessages extends Model< } SlackMessages.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -107,10 +85,6 @@ SlackMessages.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, channelId: { type: DataTypes.STRING, allowNull: false, @@ -132,13 +106,12 @@ SlackMessages.init( ], } ); -ConnectorModel.hasOne(SlackMessages); +ConnectorModel.hasOne(SlackMessages, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); -export class SlackChannel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class SlackChannel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -153,11 +126,6 @@ export class SlackChannel extends Model< } SlackChannel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -168,10 +136,6 @@ SlackChannel.init( allowNull: false, defaultValue: DataTypes.NOW, }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, slackChannelId: { type: DataTypes.STRING, allowNull: false, @@ -203,13 +167,12 @@ SlackChannel.init( ], } ); -ConnectorModel.hasMany(SlackChannel); +ConnectorModel.hasMany(SlackChannel, { + foreignKey: "connectorId", + onDelete: "RESTRICT", +}); -export class SlackChatBotMessage extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class SlackChatBotMessage extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare connectorId: ForeignKey; @@ -230,11 +193,6 @@ export class SlackChatBotMessage extends Model< } SlackChatBotMessage.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, }, @@ -311,11 +269,7 @@ SlackChatBotMessage.init( ); ConnectorModel.hasOne(SlackChatBotMessage); -export class SlackBotWhitelistModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class SlackBotWhitelistModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare botName: string; @@ -327,11 +281,6 @@ export class SlackBotWhitelistModel extends Model< SlackBotWhitelistModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/snowflake.ts b/connectors/src/lib/models/snowflake.ts index e0b36f12a6ef..fad202242008 100644 --- a/connectors/src/lib/models/snowflake.ts +++ b/connectors/src/lib/models/snowflake.ts @@ -1,19 +1,11 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class SnowflakeConfigurationModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class SnowflakeConfigurationModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -21,11 +13,6 @@ export class SnowflakeConfigurationModel extends Model< } SnowflakeConfigurationModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/webcrawler.ts b/connectors/src/lib/models/webcrawler.ts index f950f526b83f..22df1c4403ff 100644 --- a/connectors/src/lib/models/webcrawler.ts +++ b/connectors/src/lib/models/webcrawler.ts @@ -1,20 +1,12 @@ import type { CrawlingFrequency, DepthOption } from "@dust-tt/types"; -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class WebCrawlerConfigurationModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class WebCrawlerConfigurationModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare url: string; @@ -28,11 +20,6 @@ export class WebCrawlerConfigurationModel extends Model< WebCrawlerConfigurationModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -78,11 +65,7 @@ WebCrawlerConfigurationModel.init( ); ConnectorModel.hasMany(WebCrawlerConfigurationModel); -export class WebCrawlerConfigurationHeader extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class WebCrawlerConfigurationHeader extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare key: string; @@ -94,11 +77,6 @@ export class WebCrawlerConfigurationHeader extends Model< WebCrawlerConfigurationHeader.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -133,11 +111,7 @@ WebCrawlerConfigurationHeader.init( WebCrawlerConfigurationModel.hasMany(WebCrawlerConfigurationHeader); -export class WebCrawlerFolder extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class WebCrawlerFolder extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare parentUrl: string | null; @@ -154,11 +128,6 @@ export class WebCrawlerFolder extends Model< WebCrawlerFolder.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -205,11 +174,7 @@ WebCrawlerFolder.init( ConnectorModel.hasMany(WebCrawlerFolder); WebCrawlerConfigurationModel.hasMany(WebCrawlerFolder); -export class WebCrawlerPage extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class WebCrawlerPage extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare title: string | null; @@ -226,11 +191,6 @@ export class WebCrawlerPage extends Model< WebCrawlerPage.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/lib/models/zendesk.ts b/connectors/src/lib/models/zendesk.ts index 8cac4dfc9f01..c915bb2f7c1b 100644 --- a/connectors/src/lib/models/zendesk.ts +++ b/connectors/src/lib/models/zendesk.ts @@ -1,13 +1,9 @@ -import type { - CreationOptional, - ForeignKey, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional, ForeignKey } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; function throwOnUnsafeInteger(value: number | null) { if (value !== null && !Number.isSafeInteger(value)) { @@ -15,11 +11,7 @@ function throwOnUnsafeInteger(value: number | null) { } } -export class ZendeskTimestampCursor extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ZendeskTimestampCursor extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -31,11 +23,6 @@ export class ZendeskTimestampCursor extends Model< ZendeskTimestampCursor.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -63,11 +50,7 @@ ConnectorModel.hasMany(ZendeskTimestampCursor, { }); ZendeskTimestampCursor.belongsTo(ConnectorModel); -export class ZendeskConfiguration extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ZendeskConfiguration extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -79,11 +62,6 @@ export class ZendeskConfiguration extends Model< ZendeskConfiguration.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -116,11 +94,7 @@ ConnectorModel.hasMany(ZendeskConfiguration, { }); ZendeskConfiguration.belongsTo(ConnectorModel); -export class ZendeskBrand extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ZendeskBrand extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -140,11 +114,6 @@ export class ZendeskBrand extends Model< ZendeskBrand.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -208,11 +177,7 @@ ConnectorModel.hasMany(ZendeskBrand, { onDelete: "RESTRICT", }); -export class ZendeskCategory extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ZendeskCategory extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -231,11 +196,6 @@ export class ZendeskCategory extends Model< ZendeskCategory.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -296,11 +256,7 @@ ConnectorModel.hasMany(ZendeskCategory, { onDelete: "RESTRICT", }); -export class ZendeskArticle extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ZendeskArticle extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -319,11 +275,6 @@ export class ZendeskArticle extends Model< ZendeskArticle.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, @@ -385,11 +336,7 @@ ConnectorModel.hasMany(ZendeskArticle, { onDelete: "RESTRICT", }); -export class ZendeskTicket extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ZendeskTicket extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -408,11 +355,6 @@ export class ZendeskTicket extends Model< ZendeskTicket.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/resources/storage/models/connector_model.ts b/connectors/src/resources/storage/models/connector_model.ts index 80ff56553aae..a797015eda72 100644 --- a/connectors/src/resources/storage/models/connector_model.ts +++ b/connectors/src/resources/storage/models/connector_model.ts @@ -3,20 +3,13 @@ import type { ConnectorProvider, ConnectorSyncStatus, } from "@dust-tt/types"; -import type { - CreationOptional, - InferAttributes, - InferCreationAttributes, -} from "sequelize"; -import { DataTypes, Model } from "sequelize"; +import type { CreationOptional } from "sequelize"; +import { DataTypes } from "sequelize"; import { sequelizeConnection } from "@connectors/resources/storage"; +import { BaseModel } from "@connectors/resources/storage/wrappers"; -export class ConnectorModel extends Model< - InferAttributes, - InferCreationAttributes -> { - declare id: CreationOptional; +export class ConnectorModel extends BaseModel { declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare type: ConnectorProvider; @@ -40,11 +33,6 @@ export class ConnectorModel extends Model< ConnectorModel.init( { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, createdAt: { type: DataTypes.DATE, allowNull: false, diff --git a/connectors/src/resources/storage/wrappers.ts b/connectors/src/resources/storage/wrappers.ts new file mode 100644 index 000000000000..3ca3231855ed --- /dev/null +++ b/connectors/src/resources/storage/wrappers.ts @@ -0,0 +1,51 @@ +import type { + CreationOptional, + InferAttributes, + InferCreationAttributes, + InitOptions, + ModelAttributes, + ModelStatic, +} from "sequelize"; +import { DataTypes, Model } from "sequelize"; + +interface BaseModelAttributes { + id?: { + type: typeof DataTypes.BIGINT; + primaryKey?: boolean; + autoIncrement?: boolean; + }; +} + +/** + * A wrapper class that enforces BIGINT for all model primary keys. + * All models should extend this class instead of Sequelize's Model. + */ +export class BaseModel extends Model< + InferAttributes, + InferCreationAttributes +> { + declare id: CreationOptional; + + static override init>( + this: MS, + attributes: ModelAttributes> & BaseModelAttributes, + options: InitOptions> + ): MS { + const attrs: ModelAttributes> & BaseModelAttributes = { + ...attributes, + id: { + type: DataTypes.BIGINT, + primaryKey: true, + autoIncrement: true, + ...("id" in attributes + ? { + autoIncrement: attributes.id?.autoIncrement ?? true, + primaryKey: attributes.id?.primaryKey ?? true, + } + : {}), + }, + }; + + return super.init(attrs, options) as MS; + } +} diff --git a/front/lib/resources/storage/wrappers.ts b/front/lib/resources/storage/wrappers.ts index 34e2d9cc1a5e..d7b73a55e4a9 100644 --- a/front/lib/resources/storage/wrappers.ts +++ b/front/lib/resources/storage/wrappers.ts @@ -15,12 +15,19 @@ import type { } from "sequelize"; import { DataTypes, Model, Op } from "sequelize"; +interface BaseModelAttributes { + id?: { + type: typeof DataTypes.BIGINT; + primaryKey?: boolean; + autoIncrement?: boolean; + }; +} + /** * A wrapper class that enforces BIGINT for all model primary keys. * All models should extend this class instead of Sequelize's Model. */ - -export class BaseModel extends Model< +export class BaseModel extends Model< InferAttributes, InferCreationAttributes > { @@ -28,10 +35,10 @@ export class BaseModel extends Model< static override init>( this: MS, - attributes: ModelAttributes, any>, + attributes: ModelAttributes> & BaseModelAttributes, options: InitOptions> ): MS { - const attrs = { + const attrs: ModelAttributes> & BaseModelAttributes = { ...attributes, id: { type: DataTypes.BIGINT, @@ -39,12 +46,12 @@ export class BaseModel extends Model< autoIncrement: true, ...("id" in attributes ? { - autoIncrement: (attributes as any).id?.autoIncrement ?? true, - primaryKey: (attributes as any).id?.primaryKey ?? true, + autoIncrement: attributes.id?.autoIncrement ?? true, + primaryKey: attributes.id?.primaryKey ?? true, } : {}), }, - } as ModelAttributes, any>; + }; return super.init(attrs, options) as MS; } diff --git a/front/migrations/db/migration_125.sql b/front/migrations/db/migration_125.sql index cfb6bcee4b49..d091cc74c588 100644 --- a/front/migrations/db/migration_125.sql +++ b/front/migrations/db/migration_125.sql @@ -1,16 +1,12 @@ \set ON_ERROR_STOP on -- Check if the safety flag variable exists -DO $$ -BEGIN - IF NOT EXISTS ( - SELECT 1 - FROM pg_settings - WHERE name = 'i_know_what_i_do' - ) THEN - RAISE EXCEPTION 'Safety flag not set. Run with: psql -v i_know_what_i_do=true -f convert_to_bigint.sql'; - END IF; -END $$; +\if :i_know_what_i_do +-- your migration code here +\else +\echo 'Safety flag not set. Run with: psql -v i_know_what_i_do=true -f migration_40.sql' +\q +\endif DO $$ DECLARE