diff --git a/src/database/migrate.ts b/src/database/migrate.ts index d72ff28b..1ff118e0 100644 --- a/src/database/migrate.ts +++ b/src/database/migrate.ts @@ -1,7 +1,7 @@ import { Client, ClientConfig } from 'pg' import { loadMigrationFiles, MigrationError } from 'postgres-migrations' import { getConfig } from '../config' -import { logger } from '../monitoring' +import { logger, logSchema } from '../monitoring' import { BasicPgClient, Migration } from 'postgres-migrations/dist/types' import { validateMigrationHashes } from 'postgres-migrations/dist/validation' import { runMigration } from 'postgres-migrations/dist/run-migration' @@ -44,15 +44,16 @@ export async function runMultitenantMigrations(): Promise { /** * Runs migrations on a specific tenant by providing its database DSN * @param databaseUrl + * @param tenantId */ -export async function runMigrationsOnTenant(databaseUrl: string): Promise { +export async function runMigrationsOnTenant(databaseUrl: string, tenantId?: string): Promise { let ssl: ClientConfig['ssl'] | undefined = undefined if (databaseSSLRootCert) { ssl = { ca: databaseSSLRootCert } } - await connectAndMigrate(databaseUrl, './migrations/tenant', ssl) + await connectAndMigrate(databaseUrl, './migrations/tenant', ssl, undefined, tenantId) } /** @@ -61,12 +62,14 @@ export async function runMigrationsOnTenant(databaseUrl: string): Promise * @param migrationsDirectory * @param ssl * @param shouldCreateStorageSchema + * @param tenantId */ async function connectAndMigrate( databaseUrl: string | undefined, migrationsDirectory: string, ssl?: ClientConfig['ssl'], - shouldCreateStorageSchema?: boolean + shouldCreateStorageSchema?: boolean, + tenantId?: string ) { const dbConfig: ClientConfig = { connectionString: databaseUrl, @@ -76,6 +79,13 @@ async function connectAndMigrate( } const client = new Client(dbConfig) + client.on('error', (err) => { + logSchema.error(logger, 'Error on database connection', { + type: 'error', + error: err, + project: tenantId, + }) + }) try { await client.connect() await migrate({ client }, migrationsDirectory, shouldCreateStorageSchema) diff --git a/src/database/tenant.ts b/src/database/tenant.ts index e63b3f56..6c67b444 100644 --- a/src/database/tenant.ts +++ b/src/database/tenant.ts @@ -56,7 +56,7 @@ export async function runMigrations( logOnError = false ): Promise { try { - await runMigrationsOnTenant(databaseUrl) + await runMigrationsOnTenant(databaseUrl, tenantId) console.log(`${tenantId} migrations ran successfully`) } catch (error: any) { if (logOnError) { diff --git a/src/http/routes/tus/lifecycle.ts b/src/http/routes/tus/lifecycle.ts index 5698c200..c2fe1e05 100644 --- a/src/http/routes/tus/lifecycle.ts +++ b/src/http/routes/tus/lifecycle.ts @@ -30,10 +30,6 @@ export async function onIncomingRequest( res: http.ServerResponse, id: string ) { - if (rawReq.method === 'OPTIONS') { - return - } - const req = rawReq as MultiPartRequest const uploadID = UploadId.fromString(id) @@ -43,14 +39,19 @@ export async function onIncomingRequest( }) }) + if (rawReq.method === 'OPTIONS') { + return + } + const isUpsert = req.headers['x-upsert'] === 'true' const uploader = new Uploader(req.upload.storage.backend, req.upload.storage.db) + await uploader.canUpload({ owner: req.upload.owner, bucketId: uploadID.bucket, objectName: uploadID.objectName, - isUpsert, + isUpsert: isUpsert, }) } diff --git a/src/monitoring/logger.ts b/src/monitoring/logger.ts index b4fefe05..05c14ede 100644 --- a/src/monitoring/logger.ts +++ b/src/monitoring/logger.ts @@ -57,6 +57,7 @@ export interface EventLog { interface ErrorLog { type: string error?: Error | unknown + project?: string } export const logSchema = {