1
- import { BadRequestException , Injectable } from '@nestjs/common' ;
1
+ import {
2
+ BadRequestException ,
3
+ Injectable ,
4
+ RequestTimeoutException ,
5
+ } from '@nestjs/common' ;
2
6
import { InjectRepository } from '@nestjs/typeorm' ;
3
7
4
8
import { EntityManager , Repository } from 'typeorm' ;
@@ -12,6 +16,7 @@ import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/work
12
16
import { DistantTables } from 'src/engine/metadata-modules/remote-server/remote-table/distant-table/types/distant-table' ;
13
17
import { STRIPE_DISTANT_TABLES } from 'src/engine/metadata-modules/remote-server/remote-table/distant-table/utils/stripe-distant-tables.util' ;
14
18
import { PostgresTableSchemaColumn } from 'src/engine/metadata-modules/remote-server/types/postgres-table-schema-column' ;
19
+ import { isQueryTimeoutError } from 'src/engine/utils/query-timeout.util' ;
15
20
16
21
@Injectable ( )
17
22
export class DistantTableService {
@@ -70,44 +75,54 @@ export class DistantTableService {
70
75
workspaceId ,
71
76
) ;
72
77
73
- const distantTables = await workspaceDataSource . transaction (
74
- async ( entityManager : EntityManager ) => {
75
- await entityManager . query ( `CREATE SCHEMA "${ tmpSchemaName } "` ) ;
76
-
77
- const tableLimitationsOptions = tableName
78
- ? ` LIMIT TO ("${ tableName } ")`
79
- : '' ;
80
-
81
- await entityManager . query (
82
- `IMPORT FOREIGN SCHEMA "${ remoteServer . schema } "${ tableLimitationsOptions } FROM SERVER "${ remoteServer . foreignDataWrapperId } " INTO "${ tmpSchemaName } "` ,
83
- ) ;
84
-
85
- const createdForeignTableNames = await entityManager . query (
86
- `SELECT table_name, column_name, data_type, udt_name FROM information_schema.columns WHERE table_schema = '${ tmpSchemaName } '` ,
87
- ) ;
88
-
89
- await entityManager . query ( `DROP SCHEMA "${ tmpSchemaName } " CASCADE` ) ;
90
-
91
- return createdForeignTableNames . reduce (
92
- ( acc , { table_name, column_name, data_type, udt_name } ) => {
93
- if ( ! acc [ table_name ] ) {
94
- acc [ table_name ] = [ ] ;
95
- }
96
-
97
- acc [ table_name ] . push ( {
98
- columnName : column_name ,
99
- dataType : data_type ,
100
- udtName : udt_name ,
101
- } ) ;
78
+ try {
79
+ const distantTables = await workspaceDataSource . transaction (
80
+ async ( entityManager : EntityManager ) => {
81
+ await entityManager . query ( `CREATE SCHEMA "${ tmpSchemaName } "` ) ;
82
+
83
+ const tableLimitationsOptions = tableName
84
+ ? ` LIMIT TO ("${ tableName } ")`
85
+ : '' ;
86
+
87
+ await entityManager . query (
88
+ `IMPORT FOREIGN SCHEMA "${ remoteServer . schema } "${ tableLimitationsOptions } FROM SERVER "${ remoteServer . foreignDataWrapperId } " INTO "${ tmpSchemaName } "` ,
89
+ ) ;
90
+
91
+ const createdForeignTableNames = await entityManager . query (
92
+ `SELECT table_name, column_name, data_type, udt_name FROM information_schema.columns WHERE table_schema = '${ tmpSchemaName } '` ,
93
+ ) ;
94
+
95
+ await entityManager . query ( `DROP SCHEMA "${ tmpSchemaName } " CASCADE` ) ;
96
+
97
+ return createdForeignTableNames . reduce (
98
+ ( acc , { table_name, column_name, data_type, udt_name } ) => {
99
+ if ( ! acc [ table_name ] ) {
100
+ acc [ table_name ] = [ ] ;
101
+ }
102
+
103
+ acc [ table_name ] . push ( {
104
+ columnName : column_name ,
105
+ dataType : data_type ,
106
+ udtName : udt_name ,
107
+ } ) ;
108
+
109
+ return acc ;
110
+ } ,
111
+ { } ,
112
+ ) ;
113
+ } ,
114
+ ) ;
102
115
103
- return acc ;
104
- } ,
105
- { } ,
116
+ return distantTables ;
117
+ } catch ( error ) {
118
+ if ( isQueryTimeoutError ( error ) ) {
119
+ throw new RequestTimeoutException (
120
+ `Could not find distant tables: ${ error . message } ` ,
106
121
) ;
107
- } ,
108
- ) ;
122
+ }
109
123
110
- return distantTables ;
124
+ throw error ;
125
+ }
111
126
}
112
127
113
128
private getDistantTablesFromStaticSchema (
0 commit comments