Skip to content

Commit

Permalink
fix(system-api): un-hardcode schema name in LatestTransactionIdByStag…
Browse files Browse the repository at this point in the history
…eQuery
  • Loading branch information
matej21 committed Oct 25, 2022
1 parent 98c5d3c commit 64968fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ContentApiMiddlewareFactory {
request,
timer,
systemDatabase,
stageSlug: stage.slug,
stageId: stage.id,
})
if (notModifiedRes?.isModified === false) {
response.status = 304
Expand Down
6 changes: 3 additions & 3 deletions packages/engine-http/src/content/NotModifiedChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export interface NotModifiedCheckResult {
}

export class NotModifiedChecker {
public async checkNotModified({ request, timer, systemDatabase, stageSlug }: {
public async checkNotModified({ request, timer, systemDatabase, stageId }: {
request: Request
timer: Timer
systemDatabase: DatabaseContext
stageSlug: string
stageId: string
}): Promise<NotModifiedCheckResult | null> {
if (request.headers[NotModifiedHeaderName] === undefined) {
return null
Expand All @@ -28,7 +28,7 @@ export class NotModifiedChecker {
}
const latestRef = await timer('NotModifiedCheck', () => {
const queryHandler = systemDatabase.queryHandler
return queryHandler.fetch(new LatestTransactionIdByStageQuery(stageSlug))
return queryHandler.fetch(new LatestTransactionIdByStageQuery(stageId))
})

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import { DatabaseQuery, DatabaseQueryable } from '@contember/database'
import { DatabaseQuery, DatabaseQueryable, SelectBuilder } from '@contember/database'
import { ImplementationException } from '../../../utils'

export class LatestTransactionIdByStageQuery extends DatabaseQuery<string> {
constructor(private readonly stageSlug: string) {
constructor(private readonly stageId: string) {
super()
}

async fetch(queryable: DatabaseQueryable): Promise<string> {
const stageId = (
await queryable.db.query<{ id: string }>(
`
SELECT id
FROM system.stage
WHERE slug = ?
`,
[this.stageSlug],
)
).rows[0].id
const rows = (
await queryable.db.query<{ transaction_id: string }>(
`
SELECT transaction_id
FROM system.stage_transaction
WHERE stage_id = ?
ORDER BY applied_at DESC
LIMIT 1
`,
[stageId],
)
).rows
const rows = await SelectBuilder.create<{ transaction_id: string }>()
.from('stage_transaction')
.select('transaction_id')
.where({ stage_id: this.stageId })
.orderBy('applied_at', 'desc')
.limit(1)
.getResult(queryable.db)

if (rows.length !== 1) {
throw new ImplementationException()
}
Expand Down

0 comments on commit 64968fd

Please sign in to comment.