Repository navigation
Expand file tree
/
Copy pathmigration.js
More file actions
125 lines (110 loc) · 3.45 KB
/
Copy pathmigration.js
File metadata and controls
125 lines (110 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as dotenv from 'dotenv';
dotenv.config();
import Database from 'better-sqlite3';
const db = new Database('./database/database.db');
const createDatabase = () => {
const modules =
`
CREATE TABLE IF NOT EXISTS Modules (
id INTEGER PRIMARY KEY,
moduleName TEXT NOT NULL,
isActive INTEGER NOT NULL
);
`
const auditLogs = `CREATE TABLE IF NOT EXISTS AuditLogs (
id INTEGER PRIMARY KEY,
content TEXT NOT NULL
);`
const audtLogsChannels = `CREATE TABLE IF NOT EXISTS AuditLogsChannel (
id INTEGER PRIMARY KEY,
channelName TEXT UNIQUE NOT NULL,
channelId TEXT NOT NULL
);`
const mediaChannels = `CREATE TABLE IF NOT EXISTS MediaChannel (
id INTEGER PRIMARY KEY,
channelName TEXT UNIQUE NOT NULL,
channelId TEXT NOT NULL
);`
const collectedMessages = `CREATE TABLE IF NOT EXISTS CollectedMessages (
id INTEGER PRIMARY KEY,
discordUserId TEXT NOT NULL,
discordUsername TEXT NOT NULL,
discordMessageId TEXT NOT NULL,
content TEXT NOT NULL,
category TEXT NOT NULL,
collectedAt DATETIME DEFAULT CURRENT_TIMESTAMP
);`
const alterCollectedMessagesWithEmbedsPragmaQuery = `
PRAGMA table_info('CollectedMessages');
`;
const alterCollectedMessagesWithEmbeds = `
ALTER TABLE CollectedMessages
ADD COLUMN embeds TEXT;
`;
const challengeApprovals = `CREATE TABLE IF NOT EXISTS ChallengeApprovals (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
title TEXT NOT NULL,
data TEXT NOT NULL,
isPublished INTEGER DEFAULT 0,
difficulty INTEGER,
createdAt INTEGER,
updatedAt INTEGER,
publishedAt INTEGER
);`;
const challenges = `CREATE TABLE IF NOT EXISTS Challenge (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
title TEXT NOT NULL,
data TEXT NOT NULL,
isPublished INTEGER DEFAULT 0,
difficulty INTEGER,
createdAt INTEGER,
updatedAt INTEGER,
publishedAt INTEGER
);`;
const quotesSettings = `CREATE TABLE IF NOT EXISTS QuotesSettings (
id TEXT PRIMARY KEY,
channelId TEXT NOT NULL,
category TEXT UNIQUE NOT NULL,
isRunning INTEGER NOT NULL,
cronId TEXT UNIQUE NOT NULL,
createdAt INTEGER,
updatedAt INTEGER
);`;
const alterQuotesSettings = `
ALTER TABLE QuotesSettings
ADD COLUMN cronHour INTEGER;
`;
try {
// Table creations
db.prepare(modules);
db.prepare(auditLogs);
db.prepare(audtLogsChannels);
db.prepare(mediaChannels);
db.prepare(collectedMessages);
db.prepare(challengeApprovals);
db.prepare(challenges);
db.prepare(quotesSettings);
db.exec(modules);
db.exec(auditLogs);
db.exec(audtLogsChannels);
db.exec(mediaChannels);
db.exec(collectedMessages);
db.exec(challengeApprovals);
db.exec(challenges);
db.exec(quotesSettings);
// Alters
const alterCollectedMessagesWithEmbedsPragmaQueryInfo = db.prepare(alterCollectedMessagesWithEmbedsPragmaQuery).all();
const collectedMessageEmbedColumnExists = alterCollectedMessagesWithEmbedsPragmaQueryInfo.some((column) => column.name === 'embeds');
if (!collectedMessageEmbedColumnExists) {
db.prepare(alterCollectedMessagesWithEmbeds);
db.exec(alterCollectedMessagesWithEmbeds);
}
db.prepare(alterQuotesSettings);
db.exec(alterQuotesSettings);
} catch (error) {
console.log(error)
}
}
createDatabase();