@@ -76,9 +76,11 @@ struct LambdaFunction {
76
76
// See: https://github.com/vapor/postgres-nio/issues/489#issuecomment-2186509773
77
77
result = try await timeout ( deadline: . seconds( 3 ) ) {
78
78
// check if table exists
79
+ logger. trace ( " Checking database " )
79
80
try await prepareDatabase ( )
80
81
81
82
// query users
83
+ logger. trace ( " Querying database " )
82
84
return try await self . queryUsers ( )
83
85
}
84
86
} catch {
@@ -94,13 +96,19 @@ struct LambdaFunction {
94
96
private func prepareDatabase( ) async throws {
95
97
logger. trace ( " Preparing to the database " )
96
98
do {
99
+
97
100
// initial creation of the table. This will fails if it already exists
101
+ logger. trace ( " Testing if table exists " )
98
102
try await self . pgClient. query ( SQLStatements . createTable)
103
+
99
104
// it did not fail, it means the table is new and empty
105
+ logger. trace ( " Populate table " )
100
106
try await self . pgClient. query ( SQLStatements . populateTable)
107
+
101
108
} catch is PSQLError {
102
109
// when there is a database error, it means the table or values already existed
103
110
// ignore this error
111
+ logger. trace ( " Table exists already " )
104
112
} catch {
105
113
// propagate other errors
106
114
throw error
@@ -109,12 +117,11 @@ struct LambdaFunction {
109
117
110
118
/// Query the database
111
119
private func queryUsers( ) async throws -> [ User ] {
112
- logger. trace ( " Querying to the database " )
113
120
var users : [ User ] = [ ]
114
121
let query = SQLStatements . queryAllUsers
115
122
let rows = try await self . pgClient. query ( query)
116
123
for try await (id, username) in rows. decode ( ( Int, String) . self) {
117
- self . logger. trace ( " Adding \( id) : \( username) " )
124
+ self . logger. trace ( " \( id) : \( username) " )
118
125
users. append ( User ( id: id, username: username) )
119
126
}
120
127
return users
0 commit comments