Skip to content

Commit d34d3a5

Browse files
committed
feat(bots/discord): auto migrate database schema
1 parent dfa0839 commit d34d3a5

File tree

6 files changed

+170
-28
lines changed

6 files changed

+170
-28
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CREATE TABLE `applied_presets` (
2+
`member` text NOT NULL,
3+
`guild` text NOT NULL,
4+
`roles` text DEFAULT '[]' NOT NULL,
5+
`preset` text NOT NULL,
6+
`until` integer
7+
);
8+
--> statement-breakpoint
9+
CREATE UNIQUE INDEX `unique_composite` ON `applied_presets` (`member`,`preset`,`guild`);--> statement-breakpoint
10+
CREATE TABLE `responses` (
11+
`reply` text PRIMARY KEY NOT NULL,
12+
`channel` text NOT NULL,
13+
`guild` text NOT NULL,
14+
`ref` text NOT NULL,
15+
`label` text NOT NULL,
16+
`text` text NOT NULL,
17+
`by` text
18+
);
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"version": "6",
3+
"dialect": "sqlite",
4+
"id": "81c6e9da-4d03-4d2f-9934-1a6cf376dd6e",
5+
"prevId": "00000000-0000-0000-0000-000000000000",
6+
"tables": {
7+
"applied_presets": {
8+
"name": "applied_presets",
9+
"columns": {
10+
"member": {
11+
"name": "member",
12+
"type": "text",
13+
"primaryKey": false,
14+
"notNull": true,
15+
"autoincrement": false
16+
},
17+
"guild": {
18+
"name": "guild",
19+
"type": "text",
20+
"primaryKey": false,
21+
"notNull": true,
22+
"autoincrement": false
23+
},
24+
"roles": {
25+
"name": "roles",
26+
"type": "text",
27+
"primaryKey": false,
28+
"notNull": true,
29+
"autoincrement": false,
30+
"default": "'[]'"
31+
},
32+
"preset": {
33+
"name": "preset",
34+
"type": "text",
35+
"primaryKey": false,
36+
"notNull": true,
37+
"autoincrement": false
38+
},
39+
"until": {
40+
"name": "until",
41+
"type": "integer",
42+
"primaryKey": false,
43+
"notNull": false,
44+
"autoincrement": false
45+
}
46+
},
47+
"indexes": {
48+
"unique_composite": {
49+
"name": "unique_composite",
50+
"columns": [
51+
"member",
52+
"preset",
53+
"guild"
54+
],
55+
"isUnique": true
56+
}
57+
},
58+
"foreignKeys": {},
59+
"compositePrimaryKeys": {},
60+
"uniqueConstraints": {},
61+
"checkConstraints": {}
62+
},
63+
"responses": {
64+
"name": "responses",
65+
"columns": {
66+
"reply": {
67+
"name": "reply",
68+
"type": "text",
69+
"primaryKey": true,
70+
"notNull": true,
71+
"autoincrement": false
72+
},
73+
"channel": {
74+
"name": "channel",
75+
"type": "text",
76+
"primaryKey": false,
77+
"notNull": true,
78+
"autoincrement": false
79+
},
80+
"guild": {
81+
"name": "guild",
82+
"type": "text",
83+
"primaryKey": false,
84+
"notNull": true,
85+
"autoincrement": false
86+
},
87+
"ref": {
88+
"name": "ref",
89+
"type": "text",
90+
"primaryKey": false,
91+
"notNull": true,
92+
"autoincrement": false
93+
},
94+
"label": {
95+
"name": "label",
96+
"type": "text",
97+
"primaryKey": false,
98+
"notNull": true,
99+
"autoincrement": false
100+
},
101+
"text": {
102+
"name": "text",
103+
"type": "text",
104+
"primaryKey": false,
105+
"notNull": true,
106+
"autoincrement": false
107+
},
108+
"by": {
109+
"name": "by",
110+
"type": "text",
111+
"primaryKey": false,
112+
"notNull": false,
113+
"autoincrement": false
114+
}
115+
},
116+
"indexes": {},
117+
"foreignKeys": {},
118+
"compositePrimaryKeys": {},
119+
"uniqueConstraints": {},
120+
"checkConstraints": {}
121+
}
122+
},
123+
"views": {},
124+
"enums": {},
125+
"_meta": {
126+
"schemas": {},
127+
"tables": {},
128+
"columns": {}
129+
},
130+
"internal": {
131+
"indexes": {}
132+
}
133+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "7",
3+
"dialect": "sqlite",
4+
"entries": [
5+
{
6+
"idx": 0,
7+
"version": "6",
8+
"when": 1771768794695,
9+
"tag": "0000_schema",
10+
"breakpoints": true
11+
}
12+
]
13+
}

bots/discord/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ dist
178178
*.db
179179
*.sqlite
180180
*.sqlite3
181-
.drizzle
182181

183182
# Auto-generated files
184183
src/commands/index.ts

bots/discord/scripts/build.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ await cp('./config.js', './dist/config.js')
2020

2121
logger.info('Copying database schema...')
2222
await cp('./.drizzle', './dist/.drizzle', { recursive: true })
23-
await rm('./.drizzle', { recursive: true })

bots/discord/src/context.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Client as APIClient } from '@revanced/bot-api'
33
import { createLogger } from '@revanced/bot-shared'
44
import { Client as DiscordClient, type Message, Options, Partials } from 'discord.js'
55
import { drizzle } from 'drizzle-orm/bun-sqlite'
6-
import { existsSync, readdirSync, readFileSync } from 'fs'
6+
import { migrate } from 'drizzle-orm/bun-sqlite/migrator'
77
import { join } from 'path'
88
import { __getConfig, config } from './config'
99
import * as schemas from './database/schemas'
@@ -30,37 +30,17 @@ export const api = {
3030
}
3131

3232
const DatabasePath = process.env['DATABASE_PATH']
33-
const DatabaseSchemaDir = join(import.meta.dir, '..', '.drizzle')
34-
35-
let dbSchemaFileName: string | undefined
36-
37-
if (DatabasePath && !existsSync(DatabasePath)) {
38-
logger.warn('Database file not found, trying to create from schema...')
39-
40-
try {
41-
const file = readdirSync(DatabaseSchemaDir, { withFileTypes: true })
42-
.filter(file => file.isFile() && file.name.endsWith('.sql'))
43-
.sort()
44-
.at(-1)
45-
46-
if (!file) throw new Error('No schema file found')
47-
48-
dbSchemaFileName = file.name
49-
logger.debug(`Using schema file: ${dbSchemaFileName}`)
50-
} catch (e) {
51-
logger.fatal('Could not create database from schema, check if the schema file exists and is accessible')
52-
logger.fatal(e)
53-
process.exit(1)
54-
}
55-
}
5633

5734
const db = new Database(DatabasePath, { readwrite: true, create: true })
58-
if (dbSchemaFileName) db.run(readFileSync(join(DatabaseSchemaDir, dbSchemaFileName)).toString())
5935

60-
export const database = drizzle(db, {
36+
const database = drizzle(db, {
6137
schema: schemas,
6238
})
6339

40+
migrate(database, { migrationsFolder: join(import.meta.dir, '..', '.drizzle') })
41+
42+
export { database }
43+
6444
export const discord = {
6545
client: new DiscordClient({
6646
intents: [

0 commit comments

Comments
 (0)