Skip to content

Commit 15fec13

Browse files
committed
refactor scripts by encapsulation
1 parent e9a95cf commit 15fec13

11 files changed

Lines changed: 163 additions & 160 deletions

scripts/deduce-category-implications.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { type Client } from '@libsql/client'
1+
import type { Client } from '@libsql/client'
22
import { are_equal_sets } from './shared'
3-
import dotenv from 'dotenv'
4-
5-
dotenv.config({ quiet: true })
63

74
/**
85
* Deduces implications from given ones.

scripts/deduce-category-properties.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import type { Transaction, Client } from '@libsql/client'
2-
import dotenv from 'dotenv'
32
import {
43
get_assumption_string,
54
get_conclusion_string,
65
is_subset,
76
NormalizedCategoryImplication,
87
} from './shared'
98

10-
dotenv.config({ quiet: true })
11-
129
type CategoryMeta = {
1310
id: string
1411
name: string

scripts/deduce-functor-implications.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { type Client } from '@libsql/client'
1+
import type { Client } from '@libsql/client'
22
import { are_equal_sets } from './shared'
3-
import dotenv from 'dotenv'
4-
5-
dotenv.config({ quiet: true })
63

74
// TODO: remove code duplication with category implication deduction script
85

scripts/deduce-functor-properties.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import type { Transaction, Client } from '@libsql/client'
2-
import dotenv from 'dotenv'
32
import {
43
get_assumption_string,
54
get_conclusion_string,
65
is_subset,
76
NormalizedFunctorImplication,
87
} from './shared'
98

10-
dotenv.config({ quiet: true })
11-
129
type FunctorMeta = {
1310
id: string
1411
name: string

scripts/deduce-special-morphisms.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Client } from '@libsql/client'
1+
import type { Client } from '@libsql/client'
2+
3+
// TODO: deduce further morphisms,
4+
// e.g. isomorphisms = bijective morphisms in algebraic categories,
5+
// e.g. regular monomorphisms = same as monomorphisms in mono-regular categories
26

37
export async function deduce_special_morphisms(db: Client) {
48
await deduce_special_morphisms_of_dual_categories(db)
5-
// TODO: deduce further morphisms,
6-
// e.g. isomorphisms = bijective morphisms in algebraic categories,
7-
// e.g. regular monomorphisms = same as monomorphisms in mono-regular categories,
89
}
910

1011
/**

scripts/deduce-special-objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client } from '@libsql/client'
1+
import type { Client } from '@libsql/client'
22

33
export async function deduce_special_objects(db: Client) {
44
await deduce_special_objects_of_dual_categories(db)

scripts/deduce.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
import { createClient } from '@libsql/client'
2-
import dotenv from 'dotenv'
31
import { deduce_category_implications } from './deduce-category-implications'
42
import { deduce_category_properties } from './deduce-category-properties'
53
import { deduce_functor_implications } from './deduce-functor-implications'
64
import { deduce_functor_properties } from './deduce-functor-properties'
75
import { deduce_special_objects } from './deduce-special-objects'
86
import { deduce_special_morphisms } from './deduce-special-morphisms'
7+
import { get_client } from './shared'
98

10-
dotenv.config({ quiet: true })
9+
/**
10+
* Makes deductions for categories and functors.
11+
*/
12+
async function deduce() {
13+
const db = get_client()
1114

12-
const DB_URL = process.env.DB_URL
13-
const DB_AUTH_TOKEN = process.env.DB_AUTH_TOKEN
15+
await db.execute('PRAGMA foreign_keys = ON')
1416

15-
if (!DB_URL) throw new Error('No DB_URL found')
17+
await deduce_category_implications(db)
18+
await deduce_category_properties(db)
1619

17-
const db = createClient({
18-
url: DB_URL,
19-
authToken: DB_AUTH_TOKEN,
20-
})
20+
await deduce_special_objects(db)
21+
await deduce_special_morphisms(db)
2122

22-
await db.execute('PRAGMA foreign_keys = ON')
23+
await deduce_functor_implications(db)
24+
await deduce_functor_properties(db)
25+
}
2326

24-
await deduce_category_implications(db)
25-
await deduce_category_properties(db)
26-
27-
await deduce_special_objects(db)
28-
await deduce_special_morphisms(db)
29-
30-
await deduce_functor_implications(db)
31-
await deduce_functor_properties(db)
27+
await deduce()

scripts/migrate.ts

Lines changed: 79 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
1-
import { createClient } from '@libsql/client'
1+
import { type Client, createClient } from '@libsql/client'
22
import fs from 'node:fs/promises'
33
import path from 'node:path'
44
import dotenv from 'dotenv'
5+
import { get_client } from './shared'
56

67
dotenv.config({ quiet: true })
78

8-
const DB_VISITS_URL = process.env.DB_VISITS_URL
9-
const DB_VISITS_AUTH_TOKEN = process.env.DB_VISITS_AUTH_TOKEN
9+
await migrate()
1010

11-
if (!DB_VISITS_URL) throw new Error('No DB_VISITS_URL found')
11+
/**
12+
* Creates the tables, indexes, triggers, and views.
13+
*/
14+
async function migrate() {
15+
await create_visits_table()
1216

13-
const db_visits = createClient({
14-
url: DB_VISITS_URL,
15-
authToken: DB_VISITS_AUTH_TOKEN,
16-
})
17+
const db = get_client()
18+
await db.execute('PRAGMA foreign_keys = ON')
19+
await create_migrations_table(db)
20+
await apply_migrations(db)
21+
}
22+
23+
/**
24+
* Creates the visits table. It is stored in a separate database.
25+
*/
26+
async function create_visits_table() {
27+
const DB_VISITS_URL = process.env.DB_VISITS_URL
28+
const DB_VISITS_AUTH_TOKEN = process.env.DB_VISITS_AUTH_TOKEN
29+
30+
if (!DB_VISITS_URL) throw new Error('No DB_VISITS_URL found')
31+
32+
const db_visits = createClient({
33+
url: DB_VISITS_URL,
34+
authToken: DB_VISITS_AUTH_TOKEN,
35+
})
1736

18-
await db_visits.execute(`
37+
await db_visits.execute(`
1938
CREATE TABLE IF NOT EXISTS visits (
2039
id INTEGER PRIMARY KEY,
2140
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -25,63 +44,63 @@ await db_visits.execute(`
2544
)
2645
`)
2746

28-
console.info('Created visits table')
29-
30-
const DB_URL = process.env.DB_URL
31-
const DB_AUTH_TOKEN = process.env.DB_AUTH_TOKEN
32-
33-
if (!DB_URL) throw new Error('No DB_URL found')
34-
35-
const db = createClient({
36-
url: DB_URL,
37-
authToken: DB_AUTH_TOKEN,
38-
})
39-
40-
await db.execute('PRAGMA foreign_keys = ON')
47+
console.info('Created visits table')
48+
}
4149

42-
await db.execute(`
43-
CREATE TABLE IF NOT EXISTS migrations (
44-
file TEXT PRIMARY KEY,
45-
applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
46-
)
47-
`)
50+
/**
51+
* Creates the migration table that records
52+
* which migrations have already been applied.
53+
*/
54+
async function create_migrations_table(db: Client) {
55+
await db.execute(`
56+
CREATE TABLE IF NOT EXISTS migrations (
57+
file TEXT PRIMARY KEY,
58+
applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
59+
)
60+
`)
61+
}
4862

49-
const { rows } = await db.execute('SELECT file FROM migrations')
50-
const applied_migrations = new Set<string>(rows.map((row) => row.file) as string[])
63+
/**
64+
* Applies all migrations that have not been applied yet.
65+
*/
66+
async function apply_migrations(db: Client) {
67+
const { rows } = await db.execute('SELECT file FROM migrations')
68+
const applied_migrations = new Set<string>(rows.map((row) => row.file) as string[])
5169

52-
const migrations_folder = path.join(process.cwd(), 'database', 'migrations')
53-
const unsorted_files = await fs.readdir(migrations_folder, 'utf8')
54-
const files = unsorted_files.filter((f) => f.endsWith('.sql')).sort()
70+
const migrations_folder = path.join(process.cwd(), 'database', 'migrations')
71+
const unsorted_files = await fs.readdir(migrations_folder, 'utf8')
72+
const files = unsorted_files.filter((f) => f.endsWith('.sql')).sort()
5573

56-
const invalid_file = files.find((file) => !file.match(/^\d{3}_/))
57-
if (invalid_file) throw new Error(`Invalid file name: ${invalid_file}`)
74+
const invalid_file = files.find((file) => !file.match(/^\d{3}_/))
75+
if (invalid_file) throw new Error(`Invalid file name: ${invalid_file}`)
5876

59-
const all_done = files.every((file) => applied_migrations.has(file))
77+
const all_done = files.every((file) => applied_migrations.has(file))
6078

61-
if (all_done) {
62-
console.info('No migrations need to be applied')
63-
process.exit(0)
64-
}
79+
if (all_done) {
80+
console.info('No migrations need to be applied')
81+
process.exit(0)
82+
}
6583

66-
for (const file of files) {
67-
if (applied_migrations.has(file)) continue
68-
69-
const sql = await fs.readFile(path.join(migrations_folder, file), 'utf8')
70-
71-
const tx = await db.transaction()
72-
try {
73-
await tx.executeMultiple(sql)
74-
await tx.execute({
75-
sql: 'INSERT INTO migrations (file) VALUES (?)',
76-
args: [file],
77-
})
78-
await tx.commit()
79-
console.info(`Applied migration: ${file}`)
80-
} catch (err) {
81-
console.error(`Failed migration: ${file}`, err)
82-
await tx.rollback()
83-
process.exit(1)
84+
for (const file of files) {
85+
if (applied_migrations.has(file)) continue
86+
87+
const sql = await fs.readFile(path.join(migrations_folder, file), 'utf8')
88+
89+
const tx = await db.transaction()
90+
try {
91+
await tx.executeMultiple(sql)
92+
await tx.execute({
93+
sql: 'INSERT INTO migrations (file) VALUES (?)',
94+
args: [file],
95+
})
96+
await tx.commit()
97+
console.info(`Applied migration: ${file}`)
98+
} catch (err) {
99+
console.error(`Failed migration: ${file}`, err)
100+
await tx.rollback()
101+
process.exit(1)
102+
}
84103
}
85-
}
86104

87-
console.info('Applied all migrations')
105+
console.info('Applied all migrations')
106+
}

scripts/seed.ts

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,51 @@
1-
import { createClient } from '@libsql/client'
21
import fs from 'node:fs/promises'
32
import path from 'node:path'
4-
import dotenv from 'dotenv'
5-
6-
dotenv.config({ quiet: true })
7-
8-
const DB_URL = process.env.DB_URL
9-
const DB_AUTH_TOKEN = process.env.DB_AUTH_TOKEN
10-
11-
if (!DB_URL) throw new Error('No DB_URL found')
12-
13-
const db = createClient({
14-
url: DB_URL,
15-
authToken: DB_AUTH_TOKEN,
16-
})
17-
18-
const data_folder = path.join(process.cwd(), 'database', 'data')
19-
20-
const subfolders = (await fs.readdir(data_folder, { withFileTypes: true }))
21-
.filter((f) => f.isDirectory())
22-
.map((f) => f.name)
23-
.sort()
24-
25-
const invalid_folder = subfolders.find((f) => !f.match(/^\d{3}_/))
26-
if (invalid_folder) throw new Error(`Invalid folder name: ${invalid_folder}`)
27-
28-
for (const folder of subfolders) {
29-
const folder_path = path.join(data_folder, folder)
30-
const files = (await fs.readdir(folder_path, { withFileTypes: true }))
31-
.filter((f) => f.isFile() && f.name.endsWith('.sql'))
32-
.map((f) => path.join(folder_path, f.name))
3+
import { get_client } from './shared'
4+
5+
/**
6+
* Seeds the data recorded in SQL files into the database.
7+
*/
8+
async function seed() {
9+
const db = get_client()
10+
const data_folder = path.join(process.cwd(), 'database', 'data')
11+
12+
const subfolders = (await fs.readdir(data_folder, { withFileTypes: true }))
13+
.filter((f) => f.isDirectory())
14+
.map((f) => f.name)
3315
.sort()
3416

35-
for (const file of files) {
36-
const base = path.basename(file)
37-
const is_valid = base?.match(/^[A-Za-z0-9_.,\-()]+$/)
38-
if (!is_valid) {
39-
throw new Error(`Invalid file name: ${base}`)
40-
}
41-
const sql = await fs.readFile(file, 'utf8')
42-
const tx = await db.transaction()
43-
try {
44-
await tx.executeMultiple(sql)
45-
await tx.commit()
46-
const operation = file.includes('clear') ? 'Clear data' : 'Insert data'
47-
console.info(`${operation}: ${base}`)
48-
} catch (err) {
49-
console.error(`Failed to process ${file}`, err)
50-
await tx.rollback()
51-
process.exit(1)
17+
const invalid_folder = subfolders.find((f) => !f.match(/^\d{3}_/))
18+
if (invalid_folder) throw new Error(`Invalid folder name: ${invalid_folder}`)
19+
20+
for (const folder of subfolders) {
21+
const folder_path = path.join(data_folder, folder)
22+
const files = (await fs.readdir(folder_path, { withFileTypes: true }))
23+
.filter((f) => f.isFile() && f.name.endsWith('.sql'))
24+
.map((f) => path.join(folder_path, f.name))
25+
.sort()
26+
27+
for (const file of files) {
28+
const base = path.basename(file)
29+
const is_valid = base?.match(/^[A-Za-z0-9_.,\-()]+$/)
30+
if (!is_valid) {
31+
throw new Error(`Invalid file name: ${base}`)
32+
}
33+
const sql = await fs.readFile(file, 'utf8')
34+
const tx = await db.transaction()
35+
try {
36+
await tx.executeMultiple(sql)
37+
await tx.commit()
38+
const operation = file.includes('clear') ? 'Clear data' : 'Insert data'
39+
console.info(`${operation}: ${base}`)
40+
} catch (err) {
41+
console.error(`Failed to process ${file}`, err)
42+
await tx.rollback()
43+
process.exit(1)
44+
}
5245
}
5346
}
47+
48+
console.info('Inserted all data')
5449
}
5550

56-
console.info('Inserted all data')
51+
await seed()

0 commit comments

Comments
 (0)