Skip to content

Commit 7443d7c

Browse files
committed
refactor seed and setup scripts
1 parent 5eaced8 commit 7443d7c

2 files changed

Lines changed: 20 additions & 22 deletions

File tree

databases/catdat/scripts/seed.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { get_client } from './shared'
55
/**
66
* Seeds the data recorded in SQL files into the database.
77
*/
8-
async function seed() {
8+
function seed() {
99
console.info('\n--- Seed CatDat database ---')
1010
const db = get_client()
1111
const data_folder = path.join(process.cwd(), 'databases', 'catdat', 'data')
@@ -22,26 +22,25 @@ async function seed() {
2222
const process_files = db.transaction(() => {
2323
for (const folder of subfolders) {
2424
const folder_path = path.join(data_folder, folder)
25+
2526
const files = fs
26-
.readdirSync(folder_path, { withFileTypes: true })
27-
.filter((f) => f.isFile() && f.name.endsWith('.sql'))
28-
.map((f) => path.join(folder_path, f.name))
27+
.readdirSync(folder_path, 'utf8')
28+
.filter((file) => file.endsWith('.sql'))
2929
.sort()
3030

31-
for (const file of files) {
32-
const base = path.basename(file)
33-
const is_valid = base?.match(/^[A-Za-z0-9_.,\-()]+$/)
34-
if (!is_valid) {
35-
throw new Error(`Invalid file name: ${base}`)
36-
}
31+
const invalid_file = files.find(
32+
(file) => !file.match(/^[A-Za-z0-9_.,\-()]+$/),
33+
)
34+
if (invalid_file) throw new Error(`Invalid file name: ${invalid_file}`)
3735

38-
console.info(`Seed: ${base}`)
36+
for (const file of files) {
37+
console.info(`Seed: ${file}`)
3938

4039
try {
41-
const sql = fs.readFileSync(file, 'utf8')
40+
const sql = fs.readFileSync(path.join(folder_path, file), 'utf8')
4241
db.exec(sql)
4342
} catch (err) {
44-
throw new Error(`Seed failed in ${base}: ${String(err)}`)
43+
throw new Error(`Seed failed in ${file}: ${String(err)}`)
4544
}
4645
}
4746
}
@@ -55,4 +54,4 @@ async function seed() {
5554
}
5655
}
5756

58-
await seed()
57+
seed()

databases/catdat/scripts/setup.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@ function setup() {
1515

1616
const schema_folder = path.join(process.cwd(), 'databases', 'catdat', 'schema')
1717

18-
const unsorted_files = fs.readdirSync(schema_folder, 'utf8')
19-
const files = unsorted_files.filter((f) => f.endsWith('.sql')).sort()
18+
const files = fs
19+
.readdirSync(schema_folder, 'utf8')
20+
.filter((file) => file.endsWith('.sql'))
21+
.sort()
2022

2123
const invalid_file = files.find((file) => !file.match(/^\d{3}_/))
2224
if (invalid_file) throw new Error(`Invalid file name: ${invalid_file}`)
2325

2426
for (const file of files) {
2527
const sql = fs.readFileSync(path.join(schema_folder, file), 'utf8')
2628

27-
const process_file = db.transaction(() => {
28-
console.info(`Apply: ${file}`)
29-
db.exec(sql)
30-
})
29+
console.info(`Apply: ${file}`)
3130

3231
try {
33-
process_file()
32+
db.exec(sql)
3433
} catch (err) {
35-
console.error(`Failed to apply file: ${file}`, err)
34+
console.error(`Failed to apply file: ${file}`, String(err))
3635
process.exit(1)
3736
}
3837
}

0 commit comments

Comments
 (0)