Skip to content

Commit abf1a56

Browse files
committed
record schema and check mismatch
1 parent d59b885 commit abf1a56

5 files changed

Lines changed: 58 additions & 5 deletions

File tree

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
package-lock.json
33
pnpm-lock.yaml
44
yarn.lock
5+
6+
schema.json

databases/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
**/*.db
22
**/*.db-wal
33
**/*.db-shm
4-
**/*.db-journal
4+
**/*.db-journal
5+
**/*.schema.json
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createHash } from 'node:crypto'
2+
import fs from 'node:fs'
3+
import path from 'node:path'
4+
5+
const schema_folder = path.join(process.cwd(), 'databases', 'catdat', 'schema')
6+
7+
export function create_schema_hash(): string {
8+
const schema = fs
9+
.readdirSync(schema_folder, 'utf8')
10+
.filter((file) => file.endsWith('.sql'))
11+
.sort()
12+
.map((file) => fs.readFileSync(path.join(schema_folder, file), 'utf8'))
13+
.join('\n')
14+
return sha256_hex(schema)
15+
}
16+
17+
export function write_schema_hash(hash: string) {
18+
fs.writeFileSync(
19+
path.join(schema_folder, 'schema.json'),
20+
JSON.stringify({ hash }),
21+
'utf8',
22+
)
23+
}
24+
25+
export function get_saved_schema_hash() {
26+
try {
27+
const txt = fs.readFileSync(path.join(schema_folder, 'schema.json'), 'utf8')
28+
const json = JSON.parse(txt) as { hash: string }
29+
return json.hash
30+
} catch (_) {
31+
return ''
32+
}
33+
}
34+
35+
export function sha256_hex(input: string): string {
36+
return createHash('sha256').update(input).digest('hex')
37+
}

databases/catdat/scripts/seed.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33
import { get_client } from './shared'
4+
import { create_schema_hash, get_saved_schema_hash } from './schema.utils'
5+
6+
seed()
47

58
/**
69
* Seeds the data recorded in SQL files into the database.
710
*/
811
function seed() {
912
console.info('\n--- Seed CatDat database ---')
13+
14+
const schema_hash = get_saved_schema_hash()
15+
const actual_hash = create_schema_hash()
16+
if (schema_hash !== actual_hash) {
17+
console.error(`❌ Your schema is outdated. Run first pnpm db:setup.`)
18+
process.exit(1)
19+
}
20+
1021
const db = get_client()
1122
const data_folder = path.join(process.cwd(), 'databases', 'catdat', 'data')
1223

@@ -53,5 +64,3 @@ function seed() {
5364
process.exit(1)
5465
}
5566
}
56-
57-
seed()

databases/catdat/scripts/setup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33
import { get_client } from './shared'
4+
import { create_schema_hash, write_schema_hash } from './schema.utils'
5+
6+
const schema_folder = path.join(process.cwd(), 'databases', 'catdat', 'schema')
47

58
setup()
69

@@ -13,8 +16,6 @@ function setup() {
1316
const db = get_client()
1417
db.pragma('foreign_keys = ON')
1518

16-
const schema_folder = path.join(process.cwd(), 'databases', 'catdat', 'schema')
17-
1819
const files = fs
1920
.readdirSync(schema_folder, 'utf8')
2021
.filter((file) => file.endsWith('.sql'))
@@ -36,5 +37,8 @@ function setup() {
3637
}
3738
}
3839

40+
const hash = create_schema_hash()
41+
write_schema_hash(hash)
42+
3943
console.info('Setup complete')
4044
}

0 commit comments

Comments
 (0)