@@ -9,7 +9,10 @@ import {
9
9
RemoteServerType ,
10
10
RemoteServerEntity ,
11
11
} from 'src/engine/metadata-modules/remote-server/remote-server.entity' ;
12
- import { RemoteTableStatus } from 'src/engine/metadata-modules/remote-server/remote-table/dtos/remote-table.dto' ;
12
+ import {
13
+ DistantTableUpdate ,
14
+ RemoteTableStatus ,
15
+ } from 'src/engine/metadata-modules/remote-server/remote-table/dtos/remote-table.dto' ;
13
16
import {
14
17
mapUdtNameToFieldType ,
15
18
mapUdtNameToFieldSettings ,
@@ -31,6 +34,7 @@ import { PostgresTableSchemaColumn } from 'src/engine/metadata-modules/remote-se
31
34
import { fetchTableColumns } from 'src/engine/metadata-modules/remote-server/remote-table/utils/fetch-table-columns.util' ;
32
35
import { ForeignTableService } from 'src/engine/metadata-modules/remote-server/remote-table/foreign-table/foreign-table.service' ;
33
36
import { RemoteTableSchemaUpdateService } from 'src/engine/metadata-modules/remote-server/remote-table/remote-table-schema-update/remote-table-schema-update.service' ;
37
+ import { sortDistantTables } from 'src/engine/metadata-modules/remote-server/remote-table/distant-table/utils/sort-distant-tables.util' ;
34
38
35
39
export class RemoteTableService {
36
40
private readonly logger = new Logger ( RemoteTableService . name ) ;
@@ -52,7 +56,7 @@ export class RemoteTableService {
52
56
private readonly remoteTableSchemaUpdateService : RemoteTableSchemaUpdateService ,
53
57
) { }
54
58
55
- public async findDistantTablesWithStatusByServerId (
59
+ public async findDistantTablesWithStatus (
56
60
id : string ,
57
61
workspaceId : string ,
58
62
shouldFetchPendingSchemaUpdates ?: boolean ,
@@ -82,26 +86,37 @@ export class RemoteTableService {
82
86
workspaceId ,
83
87
) ;
84
88
85
- if ( currentRemoteTables . length === 0 || ! shouldFetchPendingSchemaUpdates ) {
86
- const distantTablesWithStatus = Object . keys ( distantTables ) . map (
87
- ( tableName ) => ( {
88
- name : tableName ,
89
- schema : remoteServer . schema ,
90
- status : currentRemoteTableDistantNames . includes ( tableName )
91
- ? RemoteTableStatus . SYNCED
92
- : RemoteTableStatus . NOT_SYNCED ,
93
- } ) ,
94
- ) ;
89
+ const distantTablesWithStatus = Object . keys ( distantTables ) . map (
90
+ ( tableName ) => ( {
91
+ name : tableName ,
92
+ schema : remoteServer . schema ,
93
+ status : currentRemoteTableDistantNames . includes ( tableName )
94
+ ? RemoteTableStatus . SYNCED
95
+ : RemoteTableStatus . NOT_SYNCED ,
96
+ } ) ,
97
+ ) ;
95
98
96
- return distantTablesWithStatus ;
99
+ if ( ! shouldFetchPendingSchemaUpdates ) {
100
+ return distantTablesWithStatus . sort ( sortDistantTables ) ;
97
101
}
98
102
99
- return this . remoteTableSchemaUpdateService . getDistantTablesWithUpdates ( {
100
- remoteServerSchema : remoteServer . schema ,
101
- workspaceId,
102
- remoteTables : currentRemoteTables ,
103
- distantTables,
104
- } ) ;
103
+ const schemaPendingUpdates =
104
+ await this . remoteTableSchemaUpdateService . getSchemaUpdatesBetweenForeignAndDistantTables (
105
+ {
106
+ workspaceId,
107
+ remoteTables : currentRemoteTables ,
108
+ distantTables,
109
+ } ,
110
+ ) ;
111
+
112
+ const distantTablesWithPendingUpdates =
113
+ this . getDistantTablesWithPendingUpdates (
114
+ schemaPendingUpdates ,
115
+ distantTablesWithStatus ,
116
+ remoteServer . schema ,
117
+ ) ;
118
+
119
+ return distantTablesWithPendingUpdates . sort ( sortDistantTables ) ;
105
120
}
106
121
107
122
public async findRemoteTablesByServerId ( {
@@ -442,4 +457,32 @@ export class RemoteTableService {
442
457
}
443
458
}
444
459
}
460
+
461
+ private getDistantTablesWithPendingUpdates (
462
+ schemaPendingUpdates : { [ tablename : string ] : DistantTableUpdate [ ] } ,
463
+ distantTablesWithStatus : {
464
+ name : string ;
465
+ schema : string ;
466
+ status : RemoteTableStatus ;
467
+ } [ ] ,
468
+ remoteServerSchema : string ,
469
+ ) {
470
+ const distantTablesWithUpdates = distantTablesWithStatus . map ( ( table ) => ( {
471
+ ...table ,
472
+ schemaPendingUpdates : schemaPendingUpdates [ table . name ] || [ ] ,
473
+ } ) ) ;
474
+
475
+ const deletedTables = Object . entries ( schemaPendingUpdates )
476
+ . filter ( ( [ _tableName , updates ] ) =>
477
+ updates . includes ( DistantTableUpdate . TABLE_DELETED ) ,
478
+ )
479
+ . map ( ( [ tableName , updates ] ) => ( {
480
+ name : tableName ,
481
+ schema : remoteServerSchema ,
482
+ status : RemoteTableStatus . SYNCED ,
483
+ schemaPendingUpdates : updates ,
484
+ } ) ) ;
485
+
486
+ return [ ...distantTablesWithUpdates , ...deletedTables ] ;
487
+ }
445
488
}
0 commit comments