This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VAX-482: Keys in
electric_meta
tables have to be unique
Made table column 'key' primary key. Added test. Satellite schema uses default table names instead of hard-coded.
- Loading branch information
Showing
2 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import test from 'ava' | ||
import Database from 'better-sqlite3' | ||
|
||
import { rm as removeFile } from 'node:fs/promises' | ||
import { AnyDatabase } from '../../src/drivers' | ||
|
||
import { DatabaseAdapter } from '../../src/drivers/better-sqlite3/adapter' | ||
import { BundleMigrator } from '../../src/migrators/bundle' | ||
import { satelliteDefaults } from '../../src/satellite/config' | ||
|
||
import { randomValue } from '../../src/util/random' | ||
|
||
import { data as testMigrationsData } from '../support/migrations' | ||
const { migrations } = testMigrationsData | ||
|
||
type Context = { | ||
dbName: string | ||
adapter: DatabaseAdapter | ||
db: AnyDatabase | ||
} | ||
|
||
test.beforeEach((t) => { | ||
const dbName = `schema-migrations-${randomValue()}.db` | ||
const db = new Database(dbName) | ||
const adapter = new DatabaseAdapter(db) | ||
|
||
t.context = { | ||
adapter, | ||
dbName, | ||
} | ||
}) | ||
|
||
test.afterEach.always(async (t) => { | ||
const { dbName } = t.context as Context | ||
|
||
await removeFile(dbName, { force: true }) | ||
await removeFile(`${dbName}-journal`, { force: true }) | ||
}) | ||
|
||
test('check schema keys are unique', async (t) => { | ||
const { adapter } = t.context as Context | ||
|
||
const migrator = new BundleMigrator(adapter, migrations) | ||
await migrator.up() | ||
|
||
await adapter.run({ | ||
sql: `INSERT INTO ${satelliteDefaults.metaTable}(key, value) values ('key', 'value')`, | ||
}) | ||
try { | ||
await adapter.run({ | ||
sql: `INSERT INTO ${satelliteDefaults.metaTable}(key, value) values ('key', 'value')`, | ||
}) | ||
t.fail() | ||
} catch (err) { | ||
const castError = err as { code: string } | ||
t.is(castError.code, 'SQLITE_CONSTRAINT_PRIMARYKEY') | ||
} | ||
}) |