-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to SQLite, switch to prettier, add whitelist/blacklist system …
…to conf
- Loading branch information
1 parent
97cfd1e
commit 41884cb
Showing
42 changed files
with
4,251 additions
and
3,284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,6 @@ DISCORD_CLIENT_ID= | |
DISCORD_CLIENT_SECRET= | ||
DISCORD_CHANNEL_LOGGING= | ||
|
||
DB_URL= | ||
|
||
BDPW_KEY= | ||
|
||
LOG_LEVEL=info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.DS_Store | ||
node_modules | ||
.env | ||
.vscode | ||
db/*.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
const mongoose = require('mongoose'); | ||
const { default: PQueue } = require('p-queue'); | ||
const ProgressBar = require('progress'); | ||
const Guild = require('../lib/Models/Guild'); | ||
const Channel = require('../lib/Models/Channel'); | ||
|
||
require('dotenv').config(); | ||
|
||
const uniqueElementsBy = (arr, fn) => | ||
arr.reduce((acc, v) => { | ||
if (!acc.some(x => fn(v, x))) acc.push(v); | ||
return acc; | ||
}, []); | ||
const uniqueBy = prop => (a, b) => a[prop] == b[prop]; | ||
const progressBarFormat = '[:bar] :rate/s :percent :elapseds (estimated :etas)'; | ||
|
||
const channelConfig = mongoose.model('ChannelConfig', { | ||
guildName: String, | ||
guildID: String, | ||
channelName: String, | ||
channelID: String, | ||
repos: Array, | ||
repo: String, | ||
embed: Boolean, | ||
disabledEvents: { | ||
type: Array, | ||
default: ['merge_request/update'], | ||
}, | ||
ignoredUsers: Array, | ||
ignoredBranches: Array, | ||
}); | ||
|
||
const serverConfig = mongoose.model('ServerConfig', { | ||
guildName: String, | ||
guildID: String, | ||
prefix: String, | ||
}); | ||
|
||
process.on('unhandledRejection', console.error); | ||
|
||
(async () => { | ||
console.log('DB |> Connecting'); | ||
|
||
await mongoose.connect(process.env.DB_URL, { | ||
useNewUrlParser: true, | ||
}); | ||
|
||
console.log('DB |> Connected'); | ||
|
||
// === GUILDS === | ||
|
||
console.log('DB |> Guilds |> Retrieving'); | ||
|
||
const guilds = await Guild.fetchAll(); | ||
const allGuilds = await serverConfig.find({}); | ||
|
||
console.log('DB |> Guilds |> Filtering'); | ||
|
||
const guildsToMigrate = uniqueElementsBy( | ||
allGuilds.filter(g => g && g.guildID && !guilds.get(g.guildID)), | ||
uniqueBy('guildID') | ||
); | ||
|
||
console.log(`DB |> Guilds |> Migrating (${guildsToMigrate.length})`); | ||
|
||
let progress; | ||
|
||
if (guildsToMigrate.length) { | ||
progress = new ProgressBar(`DB |> Guilds |> Migrating ${progressBarFormat}`, { total: guildsToMigrate.length, width: 20 }); | ||
} | ||
|
||
for (const guild of guildsToMigrate) { | ||
await Guild.forge({ | ||
id: guild.guildID, | ||
name: guild.guildName, | ||
prefix: guild.prefix, | ||
}).save(null, { | ||
method: 'insert', | ||
}); | ||
|
||
progress.tick(); | ||
} | ||
|
||
// === CHANNELS === | ||
|
||
console.log('DB |> Channels |> Retrieving'); | ||
|
||
const channels = await Channel.fetchAll(); | ||
const allChannels = await channelConfig.find({}); | ||
const channelsAdded = []; | ||
|
||
console.log('DB |> Channels |> Filtering'); | ||
|
||
const channelsToMigrate = allChannels.filter(ch => ch && ch.channelID && !channels.get(ch.channelID) && ch.guildID); | ||
|
||
console.log(`DB |> Channels |> Migrating (${channelsToMigrate.length})`); | ||
|
||
if (channelsToMigrate.length) { | ||
progress = new ProgressBar(`DB |> Channels |> Migrating ${progressBarFormat}`, { total: channelsToMigrate.length, width: 20 }); | ||
} | ||
|
||
for (const ch of channelsToMigrate) { | ||
if (channelsAdded.includes(ch.channelID)) continue; | ||
|
||
const channel = await Channel.forge({ | ||
id: ch.channelID, | ||
name: ch.channelName, | ||
guild_id: ch.guildID, | ||
repo: ch.repo, | ||
use_embed: !!ch.embed, | ||
events_list: JSON.stringify(ch.disabledEvents || []) || [], | ||
users_list: JSON.stringify(ch.ignoredUsers || []) || [], | ||
branches_list: JSON.stringify(ch.ignoredBranches || []) || [], | ||
}).save(null, { | ||
method: 'insert', | ||
}); | ||
|
||
if (Array.isArray(ch.repos)) | ||
await Promise.all( | ||
ch.repos.map(repo => | ||
channel.related('repos').create({ | ||
name: repo, | ||
}) | ||
) | ||
); | ||
|
||
channelsAdded.push(ch.channelID); | ||
progress.tick(); | ||
} | ||
|
||
console.log(); | ||
console.log(`DB |> Channels |> Migrated (${channelsAdded.length})`); | ||
|
||
if (channelsToMigrate.length) process.stdout.write('\n'); | ||
|
||
process.exit(0); | ||
})().then(() => process.exit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
exports.up = knex => { | ||
return knex.schema | ||
.createTable('guilds', t => { | ||
t.string('id').primary(); | ||
|
||
t.string('name').nullable(); | ||
|
||
t.string('prefix').nullable(); | ||
}) | ||
|
||
.createTable('channels', t => { | ||
t.string('id').primary(); | ||
|
||
t.string('name').nullable(); | ||
t.string('guild_id').nullable(); | ||
|
||
t.string('repo').nullable(); | ||
|
||
t.boolean('use_embed').defaultTo(true); | ||
|
||
t.enum('events_type', ['whitelist', 'blacklist']).defaultTo('blacklist'); | ||
t.json('events_list').defaultTo(['merge_request/update']); | ||
|
||
t.enum('users_type', ['whitelist', 'blacklist']).defaultTo('blacklist'); | ||
t.json('users_list').defaultTo([]); | ||
|
||
t.enum('branches_type', ['whitelist', 'blacklist']).defaultTo('blacklist'); | ||
t.json('branches_list').defaultTo([]); | ||
|
||
t.enum('repos_type', ['whitelist', 'blacklist']).defaultTo('blacklist'); | ||
t.json('repos_list').defaultTo([]); | ||
|
||
t.foreign('guild_id') | ||
.references('guilds.id') | ||
.onDelete('cascade'); | ||
}); | ||
}; | ||
|
||
exports.down = knex => { | ||
return knex.schema.dropTable('channels').dropTable('guilds'); | ||
}; |
13 changes: 13 additions & 0 deletions
13
db/migrations/2019_11_25_000001_create_channel_repos_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
exports.up = knex => | ||
knex.schema.createTable('channel_repos', t => { | ||
t.increments('id'); | ||
|
||
t.string('channel_id'); | ||
t.string('name').index(); | ||
|
||
t.foreign('channel_id') | ||
.references('channels.id') | ||
.onDelete('cascade'); | ||
}); | ||
|
||
exports.down = knex => knex.schema.dropTable('channel_repos'); |
13 changes: 13 additions & 0 deletions
13
db/migrations/2019_11_25_000002_create_channel_orgs_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
exports.up = knex => | ||
knex.schema.createTable('channel_orgs', t => { | ||
t.increments('id'); | ||
|
||
t.string('channel_id'); | ||
t.string('name').index(); | ||
|
||
t.foreign('channel_id') | ||
.references('channels.id') | ||
.onDelete('cascade'); | ||
}); | ||
|
||
exports.down = knex => knex.schema.dropTable('channel_orgs'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require('dotenv').config(); | ||
|
||
module.exports = { | ||
client: 'sqlite3', | ||
connection: { | ||
filename: "./db/db.sqlite" | ||
}, | ||
pool: { | ||
min: 2, | ||
max: 10 | ||
}, | ||
migrations: { | ||
tableName: 'migrations', | ||
directory: './db/migrations' | ||
}, | ||
useNullAsDefault: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.