@@ -63,6 +63,7 @@ export class RenameIndexNameCommand extends ActiveOrSuspendedWorkspacesMigration
6363 ] ) ;
6464
6565 let hasIndexNameChanges = false ;
66+ let hasRemovedIndexMetadata = false ;
6667
6768 for ( const index of Object . values ( flatIndexMaps . byId ) . filter ( isDefined ) ) {
6869 const flatObjectMetadata = findFlatEntityByIdInFlatEntityMapsOrThrow ( {
@@ -84,43 +85,62 @@ export class RenameIndexNameCommand extends ActiveOrSuspendedWorkspacesMigration
8485 if ( indexNameV2 === index . name ) {
8586 this . logger . log ( `Index ${ index . name } is V2` ) ;
8687 continue ;
87- } else {
88- this . logger . log ( `Renaming index ${ index . name } to ${ indexNameV2 } ` ) ;
88+ }
89+
90+ this . logger . log ( `Renaming index ${ index . name } to ${ indexNameV2 } ` ) ;
91+
92+ if ( isDryRun ) {
93+ continue ;
94+ }
95+
96+ const queryRunner = this . coreDataSource . createQueryRunner ( ) ;
97+
98+ await queryRunner . connect ( ) ;
99+ await queryRunner . startTransaction ( ) ;
100+
101+ try {
102+ await queryRunner . query (
103+ `ALTER INDEX "${ schemaName } "."${ index . name } " RENAME TO "${ indexNameV2 } "` ,
104+ ) ;
105+
106+ await queryRunner . manager . update ( IndexMetadataEntity , index . id , {
107+ name : indexNameV2 ,
108+ } ) ;
109+
110+ await queryRunner . commitTransaction ( ) ;
89111 hasIndexNameChanges = true ;
90- if ( ! isDryRun ) {
91- const queryRunner = this . coreDataSource . createQueryRunner ( ) ;
92-
93- await queryRunner . connect ( ) ;
94- await queryRunner . startTransaction ( ) ;
95-
96- try {
97- await queryRunner . query (
98- `ALTER INDEX "${ schemaName } "."${ index . name } " RENAME TO "${ indexNameV2 } "` ,
99- ) ;
100-
101- await queryRunner . manager . update ( IndexMetadataEntity , index . id , {
102- name : indexNameV2 ,
103- } ) ;
104-
105- await queryRunner . commitTransaction ( ) ;
106- } catch ( error ) {
107- await queryRunner . rollbackTransaction ( ) ;
108- throw error ;
109- } finally {
110- await queryRunner . release ( ) ;
111- }
112+ } catch ( error ) {
113+ await queryRunner . rollbackTransaction ( ) ;
114+
115+ // PostgreSQL error code 42704: undefined_object (index does not exist)
116+ if ( error . code === '42704' ) {
117+ this . logger . log (
118+ `Index ${ index . name } does not exist in schema ${ schemaName } , removing metadata` ,
119+ ) ;
120+
121+ await this . coreDataSource . manager . delete (
122+ IndexMetadataEntity ,
123+ index . id ,
124+ ) ;
125+ hasRemovedIndexMetadata = true ;
126+ } else {
127+ throw error ;
112128 }
129+ } finally {
130+ await queryRunner . release ( ) ;
113131 }
114132 }
115- if ( hasIndexNameChanges ) {
133+
134+ const shouldInvalidateCache =
135+ hasIndexNameChanges || hasRemovedIndexMetadata ;
136+
137+ if ( shouldInvalidateCache ) {
116138 this . logger . log ( 'Invalidating workspace cache' ) ;
117139
118- if ( ! isDryRun ) {
119- await this . workspaceCacheService . invalidateAndRecompute ( workspaceId , [
120- 'flatFieldMetadataMaps' ,
121- 'flatIndexMaps' ,
122- ] ) ;
123- }
140+ await this . workspaceCacheService . invalidateAndRecompute ( workspaceId , [
141+ 'flatFieldMetadataMaps' ,
142+ 'flatIndexMaps' ,
143+ ] ) ;
124144 }
125145 }
126146}
0 commit comments