diff --git a/packages/cli/src/databases/migrations/common/1720101653148-AddConstraintToExecutionMetadata.ts b/packages/cli/src/databases/migrations/common/1720101653148-AddConstraintToExecutionMetadata.ts index fb51da5015..21cd672ce2 100644 --- a/packages/cli/src/databases/migrations/common/1720101653148-AddConstraintToExecutionMetadata.ts +++ b/packages/cli/src/databases/migrations/common/1720101653148-AddConstraintToExecutionMetadata.ts @@ -108,5 +108,15 @@ export class AddConstraintToExecutionMetadata1720101653148 implements Reversible await context.runQuery( `ALTER TABLE ${executionMetadataTableTemp} RENAME TO ${executionMetadataTable};`, ); + + if (context.dbType === 'postgresdb') { + // Update sequence so that inserts continue with the next highest id. + const tableName = escape.tableName('execution_metadata'); + const sequenceName = escape.tableName('execution_metadata_temp_id_seq1'); + + await context.runQuery( + `SELECT setval('${sequenceName}', (SELECT MAX(id) FROM ${tableName}));`, + ); + } } } diff --git a/packages/cli/src/databases/migrations/postgresdb/1721377157740-FixExecutionMetadataSequence.ts b/packages/cli/src/databases/migrations/postgresdb/1721377157740-FixExecutionMetadataSequence.ts new file mode 100644 index 0000000000..2b9786ecad --- /dev/null +++ b/packages/cli/src/databases/migrations/postgresdb/1721377157740-FixExecutionMetadataSequence.ts @@ -0,0 +1,12 @@ +import type { IrreversibleMigration, MigrationContext } from '@db/types'; + +export class FixExecutionMetadataSequence1721377157740 implements IrreversibleMigration { + async up({ queryRunner, escape }: MigrationContext) { + const tableName = escape.tableName('execution_metadata'); + const sequenceName = escape.tableName('execution_metadata_temp_id_seq'); + + await queryRunner.query( + `SELECT setval('${sequenceName}', (SELECT MAX(id) FROM ${tableName}));`, + ); + } +} diff --git a/packages/cli/src/databases/migrations/postgresdb/index.ts b/packages/cli/src/databases/migrations/postgresdb/index.ts index 7bfed988c0..720c79a8e3 100644 --- a/packages/cli/src/databases/migrations/postgresdb/index.ts +++ b/packages/cli/src/databases/migrations/postgresdb/index.ts @@ -58,6 +58,7 @@ import { RemoveNodesAccess1712044305787 } from '../common/1712044305787-RemoveNo import { MakeExecutionStatusNonNullable1714133768521 } from '../common/1714133768521-MakeExecutionStatusNonNullable'; import { AddActivatedAtUserSetting1717498465931 } from './1717498465931-AddActivatedAtUserSetting'; import { AddConstraintToExecutionMetadata1720101653148 } from '../common/1720101653148-AddConstraintToExecutionMetadata'; +import { FixExecutionMetadataSequence1721377157740 } from './1721377157740-FixExecutionMetadataSequence'; export const postgresMigrations: Migration[] = [ InitialMigration1587669153312, @@ -119,4 +120,5 @@ export const postgresMigrations: Migration[] = [ MakeExecutionStatusNonNullable1714133768521, AddActivatedAtUserSetting1717498465931, AddConstraintToExecutionMetadata1720101653148, + FixExecutionMetadataSequence1721377157740, ];