1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-05 02:45:18 +02:00

Merge branch 'master' of https://github.com/pawelmalak/flame into merge_upstream_2020-12-06

This commit is contained in:
François Darveau 2021-12-06 22:29:22 -05:00
commit 021bd4e85a
266 changed files with 13470 additions and 7067 deletions

View file

@ -1,9 +1,9 @@
const { Sequelize } = require('sequelize');
const { join } = require('path');
const fs = require('fs');
const Umzug = require('umzug');
const backupDB = require('./utils/backupDb');
// Utils
const backupDB = require('./utils/backupDb');
const Logger = require('../utils/Logger');
const logger = new Logger();
@ -29,15 +29,16 @@ const connectDB = async () => {
backupDB();
await sequelize.authenticate();
logger.log('Connected to database');
// migrations
// execute all pending migrations
const pendingMigrations = await umzug.pending();
if (pendingMigrations.length > 0) {
logger.log('Executing pending migrations');
await umzug.up();
}
logger.log('Connected to database');
} catch (error) {
logger.log(`Unable to connect to the database: ${error.message}`, 'ERROR');
process.exit(1);

View file

@ -0,0 +1,37 @@
const { readFile, writeFile, copyFile } = require('fs/promises');
const Config = require('../../models/Config');
const up = async (query) => {
await copyFile('utils/init/initialConfig.json', 'data/config.json');
const initConfigFile = await readFile('data/config.json', 'utf-8');
const parsedNewConfig = JSON.parse(initConfigFile);
const existingConfig = await Config.findAll({ raw: true });
for (let pair of existingConfig) {
const { key, value, valueType } = pair;
let newValue = value;
if (valueType == 'number') {
newValue = parseFloat(value);
} else if (valueType == 'boolean') {
newValue = value == 1;
}
parsedNewConfig[key] = newValue;
}
const newConfig = JSON.stringify(parsedNewConfig);
await writeFile('data/config.json', newConfig);
await query.dropTable('config');
};
const down = async (query) => {};
module.exports = {
up,
down,
};

View file

@ -0,0 +1,27 @@
const { DataTypes } = require('sequelize');
const { INTEGER } = DataTypes;
const tables = ['categories', 'bookmarks', 'apps'];
const up = async (query) => {
const template = {
type: INTEGER,
allowNull: true,
defaultValue: 1,
};
for await (let table of tables) {
await query.addColumn(table, 'isPublic', template);
}
};
const down = async (query) => {
for await (let table of tables) {
await query.removeColumn(table, 'isPublic');
}
};
module.exports = {
up,
down,
};

View file

@ -0,0 +1,27 @@
const { DataTypes } = require('sequelize');
const { INTEGER, FLOAT } = DataTypes;
const up = async (query) => {
await query.addColumn('weather', 'humidity', {
type: INTEGER,
});
await query.addColumn('weather', 'windK', {
type: FLOAT,
});
await query.addColumn('weather', 'windM', {
type: FLOAT,
});
};
const down = async (query) => {
await query.removeColumn('weather', 'humidity');
await query.removeColumn('weather', 'windK');
await query.removeColumn('weather', 'windM');
};
module.exports = {
up,
down,
};

View file

@ -0,0 +1,19 @@
const { DataTypes } = require('sequelize');
const { INTEGER } = DataTypes;
const up = async (query) => {
await query.addColumn('bookmarks', 'orderId', {
type: INTEGER,
allowNull: true,
defaultValue: null,
});
};
const down = async (query) => {
await query.removeColumn('bookmarks', 'orderId');
};
module.exports = {
up,
down,
};

View file

@ -0,0 +1,25 @@
const { DataTypes } = require('sequelize');
const { INTEGER } = DataTypes;
const up = async (query) => {
await query.addColumn('categories', 'type', {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'bookmarks'
});
await query.addColumn('apps', 'categoryId', {
type: INTEGER,
allowNull: true,
defaultValue: -1,
});
};
const down = async (query) => {
await query.removeColumn('apps', 'categoryId');
await query.removeColumn('categories', 'type');
};
module.exports = {
up,
down,
};

View file

@ -1,12 +1,12 @@
const fs = require('fs');
const { slugify } = require('./slugify');
const backupDB = () => {
if (!fs.existsSync('data/db_backups')) {
fs.mkdirSync('data/db_backups');
}
const version = process.env.VERSION;
const slug = `db-${version.replace(/\./g, '')}-backup.sqlite`;
const slug = slugify();
const srcPath = 'data/db.sqlite';
const destPath = `data/db_backups/${slug}`;

19
db/utils/slugify.js Normal file
View file

@ -0,0 +1,19 @@
const slugify = () => {
const version = process.env.VERSION;
const slug = `db-${version.replace(/\./g, '')}-backup.sqlite`;
return slug;
};
const parseSlug = (slug) => {
const parts = slug.split('-');
const version = {
raw: parts[1],
parsed: parts[1].split('').join('.'),
};
return version;
};
module.exports = {
slugify,
parseSlug,
};