Skip to content

Commit 360548c

Browse files
committed
fix: cleanup imports
1 parent 1612b85 commit 360548c

File tree

14 files changed

+127
-119
lines changed

14 files changed

+127
-119
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"dev": "tsx watch src/start/server.ts | pino-pretty",
8-
"build": "node ./build.js && resolve-tspaths",
8+
"build": "tsc -noEmit && node ./build.js && resolve-tspaths",
99
"start": "NODE_ENV=production node dist/start/server.js",
1010
"format": "prettier -c --write src/**",
1111
"lint": "prettier -v && prettier -c src/**",

src/http/plugins/db.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import fastifyPlugin from 'fastify-plugin'
22
import { getConfig, MultitenantMigrationStrategy } from '../../config'
33
import {
4-
areMigrationsUpToDate,
54
getServiceKeyUser,
65
getTenantConfig,
76
TenantMigrationStatus,
8-
updateTenantMigrationsState,
97
TenantConnection,
108
getPostgresConnection,
11-
progressiveMigrations,
12-
runMigrationsOnTenant,
13-
hasMissingSyncMigration,
14-
DBMigration,
15-
lastMigrationName,
169
} from '@internal/database'
1710
import { verifyJWT } from '@internal/auth'
1811
import { logSchema } from '@internal/monitoring'
1912
import { createMutexByKey } from '@internal/concurrency'
13+
import {
14+
areMigrationsUpToDate,
15+
DBMigration,
16+
hasMissingSyncMigration,
17+
lastMigrationName,
18+
progressiveMigrations,
19+
runMigrationsOnTenant,
20+
updateTenantMigrationsState,
21+
} from '@internal/database/migrations'
2022

2123
declare module 'fastify' {
2224
interface FastifyRequest {

src/http/routes/admin/migrations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { FastifyInstance } from 'fastify'
22
import { Queue } from '@internal/queue'
3-
import { multitenantKnex, runMigrationsOnAllTenants } from '@internal/database'
3+
import { multitenantKnex } from '@internal/database'
44
import { RunMigrationsOnTenants } from '@storage/events'
55
import apiKey from '../../plugins/apikey'
66
import { getConfig } from '../../../config'
7+
import { runMigrationsOnAllTenants } from '@internal/database/migrations'
78

89
const { pgQueueEnable } = getConfig()
910

src/http/routes/admin/tenants.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import { FastifyInstance, RequestGenericInterface } from 'fastify'
22
import { FromSchema } from 'json-schema-to-ts'
33
import apiKey from '../../plugins/apikey'
44
import { decrypt, encrypt } from '@internal/auth'
5+
import { deleteTenantConfig, TenantMigrationStatus, multitenantKnex } from '@internal/database'
6+
import { dbSuperUser, storage } from '../../plugins'
57
import {
6-
deleteTenantConfig,
7-
TenantMigrationStatus,
8-
multitenantKnex,
98
lastMigrationName,
10-
runMigrationsOnTenant,
119
progressiveMigrations,
12-
} from '@internal/database'
13-
import { dbSuperUser, storage } from '../../plugins'
10+
runMigrationsOnTenant,
11+
} from '@internal/database/migrations'
1412

1513
const patchSchema = {
1614
body: {
@@ -292,6 +290,9 @@ export default async function routes(fastify: FastifyInstance) {
292290
migrations_status: TenantMigrationStatus.COMPLETED,
293291
})
294292
} catch (e) {
293+
if (e instanceof Error) {
294+
request.executionError = e
295+
}
295296
progressiveMigrations.addTenant(tenantId)
296297
}
297298
}

src/internal/database/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './multitenant-db'
22
export * from './tenant'
33
export * from './connection'
4-
export * from './migrations'
54
export * from './client'
65
export * from './pubsub'

src/internal/database/migrations/migrate.ts

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { BasicPgClient, Migration } from 'postgres-migrations/dist/types'
77
import { validateMigrationHashes } from 'postgres-migrations/dist/validation'
88
import { runMigration } from 'postgres-migrations/dist/run-migration'
99
import { searchPath } from '../connection'
10-
import { getTenantConfig, listTenantsToMigrate } from '../tenant'
10+
import { getTenantConfig, TenantMigrationStatus } from '../tenant'
1111
import { multitenantKnex } from '../multitenant-db'
1212
import { ProgressiveMigrations } from './progressive'
1313
import { RunMigrationsOnTenants } from '@storage/events'
1414
import { ERRORS } from '@internal/errors'
15-
import { DBMigration } from '@internal/database'
15+
import { DBMigration } from './types'
1616

1717
const {
1818
multitenantDatabaseUrl,
@@ -81,6 +81,86 @@ export async function lastMigrationName() {
8181
return migrations[migrations.length - 1].name as keyof typeof DBMigration
8282
}
8383

84+
/**
85+
* List all tenants that needs to have the migrations run
86+
*/
87+
export async function* listTenantsToMigrate(signal: AbortSignal) {
88+
let lastCursor = 0
89+
90+
while (true) {
91+
if (signal.aborted) {
92+
break
93+
}
94+
95+
const migrationVersion = await lastMigrationName()
96+
97+
const data = await multitenantKnex
98+
.table<{ id: string; cursor_id: number }>('tenants')
99+
.select('id', 'cursor_id')
100+
.where('cursor_id', '>', lastCursor)
101+
.where((builder) => {
102+
builder
103+
.where((whereBuilder) => {
104+
whereBuilder
105+
.where('migrations_version', '!=', migrationVersion)
106+
.whereNotIn('migrations_status', [
107+
TenantMigrationStatus.FAILED,
108+
TenantMigrationStatus.FAILED_STALE,
109+
])
110+
})
111+
.orWhere('migrations_status', null)
112+
})
113+
.orderBy('cursor_id', 'asc')
114+
.limit(200)
115+
116+
if (data.length === 0) {
117+
break
118+
}
119+
120+
lastCursor = data[data.length - 1].cursor_id
121+
yield data.map((tenant) => tenant.id)
122+
}
123+
}
124+
125+
/**
126+
* Update tenant migration version and status
127+
* @param tenantId
128+
* @param options
129+
*/
130+
export async function updateTenantMigrationsState(
131+
tenantId: string,
132+
options?: { state: TenantMigrationStatus }
133+
) {
134+
const migrationVersion = await lastMigrationName()
135+
const state = options?.state || TenantMigrationStatus.COMPLETED
136+
return multitenantKnex
137+
.table('tenants')
138+
.where('id', tenantId)
139+
.update({
140+
migrations_version: [
141+
TenantMigrationStatus.FAILED,
142+
TenantMigrationStatus.FAILED_STALE,
143+
].includes(state)
144+
? undefined
145+
: migrationVersion,
146+
migrations_status: state,
147+
})
148+
}
149+
150+
/**
151+
* Determine if a tenant has the migrations up to date
152+
* @param tenantId
153+
*/
154+
export async function areMigrationsUpToDate(tenantId: string) {
155+
const latestMigrationVersion = await lastMigrationName()
156+
const tenant = await getTenantConfig(tenantId)
157+
158+
return (
159+
latestMigrationVersion === tenant.migrationVersion &&
160+
tenant.migrationStatus === TenantMigrationStatus.COMPLETED
161+
)
162+
}
163+
84164
export async function hasMissingSyncMigration(tenantId: string) {
85165
const { migrationVersion, migrationStatus } = await getTenantConfig(tenantId)
86166
const migrations = await loadMigrationFilesCached('./migrations/tenant')

src/internal/database/migrations/progressive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { logger, logSchema } from '../../monitoring'
2-
import { areMigrationsUpToDate, getTenantConfig, TenantMigrationStatus } from '../tenant'
2+
import { getTenantConfig, TenantMigrationStatus } from '../tenant'
33
import { RunMigrationsOnTenants } from '@storage/events'
4+
import { areMigrationsUpToDate } from '@internal/database/migrations/migrate'
45

56
export class ProgressiveMigrations {
67
protected tenants: string[] = []

src/internal/database/tenant.ts

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { PubSubAdapter } from '../pubsub'
77
import { createMutexByKey } from '../concurrency'
88
import { LRUCache } from 'lru-cache'
99
import objectSizeOf from 'object-sizeof'
10-
import { lastMigrationName } from './migrations/migrate'
1110
import { ERRORS } from '@internal/errors'
1211
import { DBMigration } from '@internal/database/migrations'
1312

@@ -88,86 +87,6 @@ const singleTenantServiceKey:
8887
}
8988
: undefined
9089

91-
/**
92-
* List all tenants that needs to have the migrations run
93-
*/
94-
export async function* listTenantsToMigrate(signal: AbortSignal) {
95-
let lastCursor = 0
96-
97-
while (true) {
98-
if (signal.aborted) {
99-
break
100-
}
101-
102-
const migrationVersion = await lastMigrationName()
103-
104-
const data = await multitenantKnex
105-
.table<{ id: string; cursor_id: number }>('tenants')
106-
.select('id', 'cursor_id')
107-
.where('cursor_id', '>', lastCursor)
108-
.where((builder) => {
109-
builder
110-
.where((whereBuilder) => {
111-
whereBuilder
112-
.where('migrations_version', '!=', migrationVersion)
113-
.whereNotIn('migrations_status', [
114-
TenantMigrationStatus.FAILED,
115-
TenantMigrationStatus.FAILED_STALE,
116-
])
117-
})
118-
.orWhere('migrations_status', null)
119-
})
120-
.orderBy('cursor_id', 'asc')
121-
.limit(200)
122-
123-
if (data.length === 0) {
124-
break
125-
}
126-
127-
lastCursor = data[data.length - 1].cursor_id
128-
yield data.map((tenant) => tenant.id)
129-
}
130-
}
131-
132-
/**
133-
* Update tenant migration version and status
134-
* @param tenantId
135-
* @param options
136-
*/
137-
export async function updateTenantMigrationsState(
138-
tenantId: string,
139-
options?: { state: TenantMigrationStatus }
140-
) {
141-
const migrationVersion = await lastMigrationName()
142-
const state = options?.state || TenantMigrationStatus.COMPLETED
143-
return multitenantKnex
144-
.table('tenants')
145-
.where('id', tenantId)
146-
.update({
147-
migrations_version: [
148-
TenantMigrationStatus.FAILED,
149-
TenantMigrationStatus.FAILED_STALE,
150-
].includes(state)
151-
? undefined
152-
: migrationVersion,
153-
migrations_status: state,
154-
})
155-
}
156-
157-
/**
158-
* Determine if a tenant has the migrations up to date
159-
* @param tenantId
160-
*/
161-
export async function areMigrationsUpToDate(tenantId: string) {
162-
const latestMigrationVersion = await lastMigrationName()
163-
const tenant = await getTenantConfig(tenantId)
164-
165-
return (
166-
latestMigrationVersion === tenant.migrationVersion &&
167-
tenant.migrationStatus === TenantMigrationStatus.COMPLETED
168-
)
169-
}
170-
17190
/**
17291
* Deletes tenants config from the in-memory cache
17392
* @param tenantId

src/scripts/migrate-call.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dotenv from 'dotenv'
22
dotenv.config()
33

4-
import { runMigrationsOnTenant } from '@internal/database'
4+
import { runMigrationsOnTenant } from '@internal/database/migrations'
55
;(async () => {
66
await runMigrationsOnTenant(process.env.DATABASE_URL as string)
77
})()

src/start/server.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import { IncomingMessage, Server, ServerResponse } from 'node:http'
55
import build from '../app'
66
import buildAdmin from '../admin-app'
77
import { getConfig } from '../config'
8-
import {
9-
runMultitenantMigrations,
10-
runMigrationsOnTenant,
11-
startAsyncMigrations,
12-
listenForTenantUpdate,
13-
PubSub,
14-
} from '@internal/database'
8+
import { listenForTenantUpdate, PubSub } from '@internal/database'
159
import { logger, logSchema } from '@internal/monitoring'
1610
import { Queue } from '@internal/queue'
1711
import { registerWorkers } from '@storage/events'
1812
import { AsyncAbortController } from '@internal/concurrency'
1913

2014
import { bindShutdownSignals, createServerClosedPromise, shutdown } from './shutdown'
15+
import {
16+
runMigrationsOnTenant,
17+
runMultitenantMigrations,
18+
startAsyncMigrations,
19+
} from '@internal/database/migrations'
2120

2221
const shutdownSignal = new AsyncAbortController()
2322

0 commit comments

Comments
 (0)