Skip to content

Commit

Permalink
Update filesToMongo to refer to storage, tiny log change
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Jun 25, 2018
1 parent 267b7b0 commit 38128fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
29 changes: 16 additions & 13 deletions filesToMongo.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
const WIPE_DATABASE = false
const WIPE_DATABASE = true

const fs = require('fs')
const config = require('./config.json')
const storage = require('./util/storage.js')
const mongoose = require('mongoose')
const BUFFER_CONFIGS = ['sslCA', 'sslCRL', 'sslCert', 'sslKey']
const CON_SETTINGS = typeof config.database.connection === 'object' ? config.database.connection : {}

const buffers = {}
if (Object.keys(CON_SETTINGS).length > 0) {
for (var x = 0; x < BUFFER_CONFIGS.length; ++x) {
const name = BUFFER_CONFIGS[x]
if (CON_SETTINGS[name]) buffers[name] = fs.readFileSync(CON_SETTINGS[name])
}
}

const files = fs.readdirSync('./sources')
mongoose.connect(config.database.uri)
mongoose.connect(config.database.uri, { keepAlive: 120, ...CON_SETTINGS, ...buffers })
const db = mongoose.connection
let c = 0

const Guild = mongoose.model('guilds', mongoose.Schema({
id: String,
name: String,
sources: Object,
dateFormat: String,
dateLanguage: String,
timezone: String
}))

function addFileToDb (name, i, arr) {
const id = name.replace(/\.json/i, '')
const guild = JSON.parse(fs.readFileSync(`./sources/${id}.json`))
Guild.update({ id: id }, guild, { overwrite: true, upsert: true, strict: true }, err => {
storage.models.GuildRss().update({ id: id }, guild, { overwrite: true, upsert: true, strict: true }, err => {
if (err) throw err
console.log(`Completed ${name} (${++c}/${arr.length})`)
if (c === arr.length) db.close()
Expand All @@ -33,7 +36,7 @@ db.once('open', () => {
if (WIPE_DATABASE === false) return files.filter(f => /^\d+$/.test(f.replace(/\.json/i, ''))).forEach(addFileToDb)

// Otherwise drop the "guilds" collection from database for do-over if WIPE_DATABASE is true
Guild.collection.drop((err, res) => {
storage.models.GuildRss().collection.drop((err, res) => {
if (err) throw err
console.log(`Database drop successful`)
db.close()
Expand Down
2 changes: 1 addition & 1 deletion structs/ArticleMessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function toggleRoleMentionable (mentionable, channelId, roleIds, callback) {
role.setMentionable(mentionable).then(r => {
if (++done >= roleIds.length && callback) callback()
}).catch(err => {
log.general.error(`Unable to toggle role ${role.id} (${role.name}) mentionable to ${mentionable} for article delivery`, err)
log.general.error(`Unable to toggle role ${role.id} (${role.name}) mentionable to ${mentionable} for article delivery`, guild, err)
if (++done >= roleIds.length && callback) callback()
})
}
Expand Down
4 changes: 4 additions & 0 deletions util/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ exports.schemas = {
type: Date,
index: { expires: 0 }
},
gracedUntil: {
type: Date,
index: { expires: 0 }
},
override: Boolean
}),
blacklist: mongoose.Schema({
Expand Down

0 comments on commit 38128fb

Please sign in to comment.