Skip to content

Commit

Permalink
fix TS backwards compat following ControlledTransaction. (#1193)
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Nov 24, 2024
1 parent c872dc7 commit 59a89d4
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 72 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ jobs:
- name: Install older TypeScript
run: npm i -D typescript@${{ matrix.typescript-version }} tsd@${{ fromJson('{ "^4.6":"0.20.0", "^4.7":"0.22.0", "^4.8":"0.24.1", "^4.9":"0.27.0", "^5.0":"0.28.1", "^5.2":"0.29.0", "^5.3":"0.30.7", "^5.4":"0.31.2" }')[matrix.typescript-version] }}

- name: Exclude non-backward compatible tests
run: npx tsx ./scripts/exclude-test-files-for-backwards-compat.mts

- name: Run tests with older TypeScript
run: npm run test:typings && npm run test:node:build

Expand Down
111 changes: 59 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@types/node": "^22.5.0",
"@types/pg": "^8.11.6",
"@types/pg-cursor": "^2.7.2",
"@types/semver": "^7.5.8",
"@types/sinon": "^17.0.2",
"@types/tedious": "^4.0.9",
"better-sqlite3": "^11.2.1",
Expand All @@ -92,15 +93,18 @@
"lodash": "^4.17.21",
"mocha": "^10.7.3",
"mysql2": "^3.11.0",
"pathe": "^1.1.2",
"pg": "^8.12.0",
"pg-cursor": "^2.11.0",
"pkg-pr-new": "^0.0.30",
"playwright": "^1.46.1",
"prettier": "^3.3.3",
"semver": "^7.6.3",
"sinon": "^19.0.2",
"tarn": "^3.0.2",
"tedious": "^19.0.0",
"tsd": "^0.31.1",
"tsx": "^4.19.1",
"typescript": "^5.6.3"
}
}
22 changes: 22 additions & 0 deletions scripts/exclude-test-files-for-backwards-compat.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { writeFile } from 'node:fs/promises'
import { dirname, resolve } from 'pathe'
import { lt } from 'semver'
import { devDependencies } from '../package.json'

const typescriptVersion = devDependencies.typescript.replace('^', '')
const testTsConfigRelativePath = '../test/node/tsconfig.json'

if (lt(typescriptVersion, '5.0.0')) {
const tsconfig = await import('../test/node/tsconfig.json')

await writeFile(
resolve(
dirname(new URL(import.meta.url).pathname),
testTsConfigRelativePath,
),
JSON.stringify({
...tsconfig,
exclude: [...(tsconfig.exclude || []), 'src/async-dispose.test.ts'],
}),
)
}
12 changes: 8 additions & 4 deletions src/kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,9 @@ export class ControlledTransaction<
*/
rollbackToSavepoint<SN extends S[number]>(
savepointName: SN,
): Command<ControlledTransaction<DB, RollbackToSavepoint<S, SN>>> {
): RollbackToSavepoint<S, SN> extends string[]
? Command<ControlledTransaction<DB, RollbackToSavepoint<S, SN>>>
: never {
this.#assertNotCommittedOrRolledBack()

return new Command(async () => {
Expand All @@ -922,7 +924,7 @@ export class ControlledTransaction<
)

return new ControlledTransaction({ ...this.#props })
})
}) as any
}

/**
Expand Down Expand Up @@ -953,7 +955,9 @@ export class ControlledTransaction<
*/
releaseSavepoint<SN extends S[number]>(
savepointName: SN,
): Command<ControlledTransaction<DB, ReleaseSavepoint<S, SN>>> {
): ReleaseSavepoint<S, SN> extends string[]
? Command<ControlledTransaction<DB, ReleaseSavepoint<S, SN>>>
: never {
this.#assertNotCommittedOrRolledBack()

return new Command(async () => {
Expand All @@ -964,7 +968,7 @@ export class ControlledTransaction<
)

return new ControlledTransaction({ ...this.#props })
})
}) as any
}

override withPlugin(plugin: KyselyPlugin): ControlledTransaction<DB, S> {
Expand Down
8 changes: 4 additions & 4 deletions src/parser/savepoint-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { RawNode } from '../operation-node/raw-node.js'
export type RollbackToSavepoint<
S extends string[],
SN extends S[number],
> = S extends [...infer L extends string[], infer R]
> = S extends [...infer L, infer R]
? R extends SN
? S
: RollbackToSavepoint<L, SN>
: RollbackToSavepoint<L extends string[] ? L : never, SN>
: never

export type ReleaseSavepoint<
S extends string[],
SN extends S[number],
> = S extends [...infer L extends string[], infer R]
> = S extends [...infer L, infer R]
? R extends SN
? L
: ReleaseSavepoint<L, SN>
: ReleaseSavepoint<L extends string[] ? L : never, SN>
: never

export function parseSavepointCommand(
Expand Down
1 change: 1 addition & 0 deletions test/node/src/async-dispose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('async dispose', function () {
const steps: string[] = []

{
// @ts-ignore - `using` was only introduced in TS 5.2
await using db = new Kysely({
dialect: {
createAdapter: () => new PostgresAdapter(),
Expand Down
2 changes: 1 addition & 1 deletion test/node/src/controlled-transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ for (const dialect of DIALECTS) {
'repeatable read',
'serializable',
...(dialect === 'mssql' ? (['snapshot'] as const) : []),
] satisfies IsolationLevel[]) {
] as const) {
it(`should set the transaction isolation level as "${isolationLevel}"`, async () => {
const trx = await ctx.db
.startTransaction()
Expand Down
12 changes: 1 addition & 11 deletions test/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
{
"extends": "../../tsconfig-base.json",
"include": ["src/**/*"],
"compilerOptions": {
"target": "ES2022",
"lib": ["ESNext"],
"module": "CommonJS",
"outDir": "dist",
"skipLibCheck": true
}
}
{"compilerOptions":{"target":"ES2022","lib":["ESNext"],"module":"CommonJS","outDir":"dist","skipLibCheck":true},"default":{"compilerOptions":{"target":"ES2022","lib":["ESNext"],"module":"CommonJS","outDir":"dist","skipLibCheck":true},"default":{"compilerOptions":{"target":"ES2022","lib":["ESNext"],"module":"CommonJS","outDir":"dist","skipLibCheck":true},"default":{"extends":"../../tsconfig-base.json","include":["src/**/*"],"compilerOptions":{"target":"ES2022","lib":["ESNext"],"module":"CommonJS","outDir":"dist","skipLibCheck":true}},"extends":"../../tsconfig-base.json","include":["src/**/*"],"exclude":["src/async-dispose.test.ts"]},"exclude":["src/async-dispose.test.ts"],"extends":"../../tsconfig-base.json","include":["src/**/*"]},"exclude":["src/async-dispose.test.ts","src/async-dispose.test.ts"],"extends":"../../tsconfig-base.json","include":["src/**/*"]}

0 comments on commit 59a89d4

Please sign in to comment.