chore: add unique integrity checks to the database for many-to-many tables

This commit is contained in:
Zephyrrus 2020-07-08 19:22:25 +03:00
parent ad852de51a
commit 6713eca9d4
2 changed files with 17 additions and 13 deletions

View File

@ -1,11 +1,11 @@
exports.up = async (knex) => {
await knex.schema.createTable('users', (table) => {
table.increments();
table.string('username');
table.string('username').unique();
table.text('password');
table.boolean('enabled');
table.boolean('isAdmin');
table.string('apiKey');
table.string('apiKey').unique();
table.timestamp('passwordEditedAt');
table.timestamp('apiKeyEditedAt');
table.timestamp('createdAt');
@ -16,10 +16,12 @@ exports.up = async (knex) => {
table.increments();
table.integer('userId');
table.string('name');
table.boolean('nsfw');
table.boolean('nsfw').defaultTo(false);
table.timestamp('zippedAt');
table.timestamp('createdAt');
table.timestamp('editedAt');
table.unique(['userId', 'name']);
});
await knex.schema.createTable('files', (table) => {
@ -29,7 +31,7 @@ exports.up = async (knex) => {
table.string('original');
table.string('type');
table.integer('size');
table.boolean('nsfw');
table.boolean('nsfw').defaultTo(false);
table.string('hash');
table.string('ip');
table.timestamp('createdAt');
@ -47,18 +49,22 @@ exports.up = async (knex) => {
table.timestamp('expiresAt');
table.timestamp('createdAt');
table.timestamp('editedAt');
table.unique(['userId', 'albumId', 'identifier']);
});
await knex.schema.createTable('albumsFiles', (table) => {
table.increments();
table.integer('albumId');
table.integer('fileId');
table.unique(['albumId', 'fileId']);
});
await knex.schema.createTable('albumsLinks', (table) => {
table.increments();
table.integer('albumId');
table.integer('linkId');
table.integer('linkId').unique();
});
await knex.schema.createTable('tags', (table) => {
@ -68,12 +74,16 @@ exports.up = async (knex) => {
table.string('name');
table.timestamp('createdAt');
table.timestamp('editedAt');
table.unique(['userId', 'name']);
});
await knex.schema.createTable('fileTags', (table) => {
table.increments();
table.integer('fileId');
table.integer('tagId');
table.unique(['fileId', 'tagId']);
});
await knex.schema.createTable('bans', (table) => {

View File

@ -84,10 +84,7 @@ class uploadPOST extends Route {
if (remappedKeys && remappedKeys.uuid) {
const chunkOutput = path.join(__dirname,
'..',
'..',
'..',
'..',
'../../../../',
process.env.UPLOAD_FOLDER,
'chunks',
remappedKeys.uuid,
@ -95,10 +92,7 @@ class uploadPOST extends Route {
await jetpack.writeAsync(chunkOutput, file.buffer);
} else {
const output = path.join(__dirname,
'..',
'..',
'..',
'..',
'../../../../',
process.env.UPLOAD_FOLDER,
filename);
await jetpack.writeAsync(output, file.buffer);