Skip to content

Commit

Permalink
Merge pull request #201 from Cryptkeeper/daily-db
Browse files Browse the repository at this point in the history
5.5.8 release preview
  • Loading branch information
Cryptkeeper authored Aug 1, 2020
2 parents 68e01a6 + 783b49e commit bfa3ae5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**5.5.8** *(August 1 2020)*
- Adds daily database copies. This is mostly for use by Minetrack Data for automated exports. By setting `createDailyDatabaseCopy: true` in `config.json`, Minetrack will lazily create a copy of `database.sql` for each day, automatically rolling over to a new file each day. The database file is named in the format of `database_copy_(day)-(month)-(year).sql`. Daily database copies do not contain indexes or previous records. Pings are inserted into the daily database copy as they occur, Minetrack will not retroactively insert previous pings from `database.sql`.
- Bump lodash from 4.17.15 to 4.17.19

**5.5.7** *(July 7 2020)*
- Fixes an issue in which the light theme CSS may not be applied by default.

Expand Down
40 changes: 39 additions & 1 deletion lib/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const sqlite = require('sqlite3')

const config = require('../config')
const { TimeTracker } = require('./time')

class Database {
Expand All @@ -8,6 +9,32 @@ class Database {
this._sql = new sqlite.Database('database.sql')
}

getDailyDatabase () {
if (!config.createDailyDatabaseCopy) {
return
}

const date = new Date()
const fileName = `database_copy_${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}.sql`

if (fileName !== this._currentDatabaseCopyFileName) {
if (this._currentDatabaseCopyInstance) {
this._currentDatabaseCopyInstance.close()
}

this._currentDatabaseCopyInstance = new sqlite.Database(fileName)
this._currentDatabaseCopyFileName = fileName

// Ensure the initial tables are created
// This does not created indexes since it is only inserted to
this._currentDatabaseCopyInstance.serialize(() => {
this._currentDatabaseCopyInstance.run('CREATE TABLE IF NOT EXISTS pings (timestamp BIGINT NOT NULL, ip TINYTEXT, playerCount MEDIUMINT)')
})
}

return this._currentDatabaseCopyInstance
}

ensureIndexes () {
this._sql.serialize(() => {
this._sql.run('CREATE TABLE IF NOT EXISTS pings (timestamp BIGINT NOT NULL, ip TINYTEXT, playerCount MEDIUMINT)')
Expand Down Expand Up @@ -122,7 +149,18 @@ class Database {
}

insertPing (ip, timestamp, unsafePlayerCount) {
const statement = this._sql.prepare('INSERT INTO pings (timestamp, ip, playerCount) VALUES (?, ?, ?)')
this._insertPingTo(ip, timestamp, unsafePlayerCount, this._sql)

// Push a copy of the data into the database copy, if any
// This creates an "insert only" copy of the database for archiving
const dailyDatabase = this.getDailyDatabase()
if (dailyDatabase) {
this._insertPingTo(ip, timestamp, unsafePlayerCount, dailyDatabase)
}
}

_insertPingTo (ip, timestamp, unsafePlayerCount, db) {
const statement = db.prepare('INSERT INTO pings (timestamp, ip, playerCount) VALUES (?, ?, ?)')
statement.run(timestamp, ip, unsafePlayerCount)
statement.finalize()
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minetrack",
"version": "5.5.7",
"version": "5.5.8",
"description": "A Minecraft server tracker that lets you focus on the basics.",
"main": "main.js",
"dependencies": {
Expand Down

0 comments on commit bfa3ae5

Please sign in to comment.