From 44e6fd31d2fa7761c90ff1d6932cf69d163b22e8 Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 21 Feb 2019 23:49:29 +0900 Subject: Database migration and seeding --- .../migrations/20190221225812_initialMigration.js | 69 ++++++++++++++++++++++ src/api/database/seeds/initial.js | 32 ++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/api/database/migrations/20190221225812_initialMigration.js create mode 100644 src/api/database/seeds/initial.js (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js new file mode 100644 index 0000000..455d5c5 --- /dev/null +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -0,0 +1,69 @@ +exports.up = async knex => { + await knex.schema.createTable('users', table => { + table.increments(); + table.string('username'); + table.string('password'); + table.boolean('enabled').defaultTo(true); + table.boolean('isAdmin').defaultTo(false); + table.string('apiKey'); + table.timestamp('passwordEditedAt'); + table.timestamp('apiKeyEditedAt'); + table.timestamp('createdAt'); + table.timestamp('editedAt'); + }); + + await knex.schema.createTable('albums', table => { + table.increments(); + table.integer('userId'); + table.string('name'); + table.timestamp('zippedAt'); + table.timestamp('createdAt'); + table.timestamp('editedAt'); + }); + + await knex.schema.createTable('files', table => { + table.increments(); + table.integer('userId'); + table.string('name'); + table.string('original'); + table.string('type'); + table.integer('size'); + table.string('hash'); + table.string('ip'); + table.timestamp('createdAt'); + table.timestamp('editedAt'); + }); + + await knex.schema.createTable('links', table => { + table.increments(); + table.integer('userId'); + table.integer('albumId'); + table.string('identifier'); + table.integer('views').defaultTo(0); + table.boolean('enabled').defaultTo(true); + table.boolean('enableDownload').defaultTo(true); + table.timestamp('expiresAt'); + table.timestamp('createdAt'); + table.timestamp('editedAt'); + }); + + await knex.schema.createTable('albumsFiles', table => { + table.increments(); + table.integer('albumId'); + table.integer('fileId'); + }); + + await knex.schema.createTable('albumsLinks', table => { + table.increments(); + table.integer('albumId'); + table.integer('linkId'); + }); +}; +exports.down = async knex => { + await knex.schema.dropTableIfExists('users'); + await knex.schema.dropTableIfExists('albums'); + await knex.schema.dropTableIfExists('files'); + await knex.schema.dropTableIfExists('links'); + await knex.schema.dropTableIfExists('albumsFiles'); + await knex.schema.dropTableIfExists('albumsLinks'); +}; diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js new file mode 100644 index 0000000..d4a343c --- /dev/null +++ b/src/api/database/seeds/initial.js @@ -0,0 +1,32 @@ +const bcrypt = require('bcrypt'); +const moment = require('moment'); +const randomstring = require('randomstring'); + +exports.seed = async db => { + const now = moment.utc().toDate(); + const user = await db.table('users').where({ username: 'root' }).first(); + if (user) return; + try { + const hash = await bcrypt.hash(process.env.ADMIN_PASSWORD, 10); + await db.table('users').insert({ + username: process.env.ADMIN_ACCOUNT, + password: hash, + apiKey: randomstring.generate(64), + passwordEditedAt: now, + apiKeyEditedAt: now, + createdAt: now, + editedAt: now, + isAdmin: true + }); + console.log(); + console.log('===================================================='); + console.log('== Successfully created the admin account. =='); + console.log('===================================================='); + console.log('== Run `yarn api` and `yarn site` next =='); + console.log('== preferably with pm2 or tmux to keep them alive =='); + console.log('===================================================='); + console.log(); + } catch (error) { + console.error(error); + } +} -- cgit v1.2.3 From fc95cb7b0f047806937c25f0fc1104c72b0a32cb Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 23 Feb 2019 00:45:45 +0900 Subject: Better DB handling and stuff --- src/api/database/migrations/20190221225812_initialMigration.js | 10 +++++----- src/api/database/seeds/initial.js | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index 455d5c5..f2192f6 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -3,8 +3,8 @@ exports.up = async knex => { table.increments(); table.string('username'); table.string('password'); - table.boolean('enabled').defaultTo(true); - table.boolean('isAdmin').defaultTo(false); + table.boolean('enabled'); + table.boolean('isAdmin'); table.string('apiKey'); table.timestamp('passwordEditedAt'); table.timestamp('apiKeyEditedAt'); @@ -39,9 +39,9 @@ exports.up = async knex => { table.integer('userId'); table.integer('albumId'); table.string('identifier'); - table.integer('views').defaultTo(0); - table.boolean('enabled').defaultTo(true); - table.boolean('enableDownload').defaultTo(true); + table.integer('views'); + table.boolean('enabled'); + table.boolean('enableDownload'); table.timestamp('expiresAt'); table.timestamp('createdAt'); table.timestamp('editedAt'); diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index d4a343c..7445916 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -16,6 +16,7 @@ exports.seed = async db => { apiKeyEditedAt: now, createdAt: now, editedAt: now, + enabled: true, isAdmin: true }); console.log(); -- cgit v1.2.3 From 69dd014f49ceae4f1445071a5b4372f169fe9d57 Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 12 Mar 2019 06:18:19 +0000 Subject: Add uuids --- src/api/database/migrations/20190221225812_initialMigration.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index f2192f6..ee54e39 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -1,6 +1,7 @@ exports.up = async knex => { await knex.schema.createTable('users', table => { table.increments(); + table.string('uuid'); table.string('username'); table.string('password'); table.boolean('enabled'); @@ -14,6 +15,7 @@ exports.up = async knex => { await knex.schema.createTable('albums', table => { table.increments(); + table.string('uuid'); table.integer('userId'); table.string('name'); table.timestamp('zippedAt'); @@ -23,6 +25,7 @@ exports.up = async knex => { await knex.schema.createTable('files', table => { table.increments(); + table.string('uuid'); table.integer('userId'); table.string('name'); table.string('original'); @@ -36,6 +39,7 @@ exports.up = async knex => { await knex.schema.createTable('links', table => { table.increments(); + table.string('uuid'); table.integer('userId'); table.integer('albumId'); table.string('identifier'); -- cgit v1.2.3 From e7d27844d003f99896ae34c79d966e3c74e71cb6 Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 12 Mar 2019 07:57:15 +0000 Subject: Wonder if password generation is failing because of long passwords --- src/api/database/migrations/20190221225812_initialMigration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index ee54e39..a9ce2c7 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -3,7 +3,7 @@ exports.up = async knex => { table.increments(); table.string('uuid'); table.string('username'); - table.string('password'); + table.text('password'); table.boolean('enabled'); table.boolean('isAdmin'); table.string('apiKey'); -- cgit v1.2.3 From b1e751159339bca98b5d878802147cb318e113d0 Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 12 Mar 2019 08:17:37 +0000 Subject: What are strings even --- src/api/database/seeds/initial.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 7445916..0ea7bb4 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -20,12 +20,11 @@ exports.seed = async db => { isAdmin: true }); console.log(); - console.log('===================================================='); - console.log('== Successfully created the admin account. =='); - console.log('===================================================='); - console.log('== Run `yarn api` and `yarn site` next =='); - console.log('== preferably with pm2 or tmux to keep them alive =='); - console.log('===================================================='); + console.log('========================================================='); + console.log('== Successfully created the admin account. =='); + console.log('========================================================='); + console.log('== Run `pm2 start pm2.json` to start the service =='); + console.log('========================================================='); console.log(); } catch (error) { console.error(error); -- cgit v1.2.3 From 497a961a3844afccc763ebdfa2d77f107318394a Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 14 Mar 2019 23:14:37 +0900 Subject: Tags --- .../database/migrations/20190221225813_addTags.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/api/database/migrations/20190221225813_addTags.js (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225813_addTags.js b/src/api/database/migrations/20190221225813_addTags.js new file mode 100644 index 0000000..ef71877 --- /dev/null +++ b/src/api/database/migrations/20190221225813_addTags.js @@ -0,0 +1,21 @@ +exports.up = async knex => { + await knex.schema.createTable('tags', table => { + table.increments(); + table.string('uuid'); + table.integer('userId'); + table.string('name'); + table.timestamp('createdAt'); + table.timestamp('editedAt'); + }); + + await knex.schema.createTable('fileTags', table => { + table.increments(); + table.integer('fileId'); + table.integer('tagId'); + }); +}; + +exports.down = async knex => { + await knex.schema.dropTableIfExists('tags'); + await knex.schema.dropTableIfExists('fileTags'); +}; -- cgit v1.2.3 From 107d1f4750e8f82a628b528c4ec200e918be271d Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 19 Mar 2019 07:58:36 +0000 Subject: API key WIP --- src/api/database/seeds/initial.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 0ea7bb4..bb8b915 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -11,9 +11,7 @@ exports.seed = async db => { await db.table('users').insert({ username: process.env.ADMIN_ACCOUNT, password: hash, - apiKey: randomstring.generate(64), passwordEditedAt: now, - apiKeyEditedAt: now, createdAt: now, editedAt: now, enabled: true, -- cgit v1.2.3 From a552aca8ab67535bf025c6f06f751a0b11ef2e9a Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 1 Oct 2019 14:08:43 -0300 Subject: chore: Remove unnecesary stuff --- src/api/database/seeds/initial.js | 1 - 1 file changed, 1 deletion(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index bb8b915..560b6b2 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -1,6 +1,5 @@ const bcrypt = require('bcrypt'); const moment = require('moment'); -const randomstring = require('randomstring'); exports.seed = async db => { const now = moment.utc().toDate(); -- cgit v1.2.3 From 391ee68e4a67aec640e25bc3506f9e31c77e58f5 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 12 Oct 2019 15:47:25 +0900 Subject: chore: Upgrade buefy to newest version --- src/api/database/seeds/initial.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 560b6b2..bb4ce8c 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -26,4 +26,4 @@ exports.seed = async db => { } catch (error) { console.error(error); } -} +}; -- cgit v1.2.3 From bca8fbcd839d2239e3f6f141f662fbbc74726835 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 12 Oct 2019 21:14:19 +0900 Subject: refactor: removed useless code, cleaned up, fixed permissions --- src/api/database/seeds/initial.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index bb4ce8c..280fd74 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -3,7 +3,7 @@ const moment = require('moment'); exports.seed = async db => { const now = moment.utc().toDate(); - const user = await db.table('users').where({ username: 'root' }).first(); + const user = await db.table('users').where({ username: process.env.ADMIN_ACCOUNT }).first(); if (user) return; try { const hash = await bcrypt.hash(process.env.ADMIN_PASSWORD, 10); -- cgit v1.2.3 From cba7bf8586f59a049f79aba586db201ac6f3530b Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 13 Oct 2019 02:53:45 +0900 Subject: This commit adds a bunch of features for admins: * banning IP * see files from other users if you are admin * be able to see details of an uploaded file and it's user * improved display of thumbnails for non-image files --- .../migrations/20190221225812_initialMigration.js | 24 ++++++++++++++++++++++ .../database/migrations/20190221225813_addTags.js | 21 ------------------- 2 files changed, 24 insertions(+), 21 deletions(-) delete mode 100644 src/api/database/migrations/20190221225813_addTags.js (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index a9ce2c7..84bda7e 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -62,6 +62,27 @@ exports.up = async knex => { table.integer('albumId'); table.integer('linkId'); }); + + await knex.schema.createTable('tags', table => { + table.increments(); + table.string('uuid'); + table.integer('userId'); + table.string('name'); + table.timestamp('createdAt'); + table.timestamp('editedAt'); + }); + + await knex.schema.createTable('fileTags', table => { + table.increments(); + table.integer('fileId'); + table.integer('tagId'); + }); + + await knex.schema.createTable('bans', table => { + table.increments(); + table.string('ip'); + table.timestamp('createdAt'); + }); }; exports.down = async knex => { await knex.schema.dropTableIfExists('users'); @@ -70,4 +91,7 @@ exports.down = async knex => { await knex.schema.dropTableIfExists('links'); await knex.schema.dropTableIfExists('albumsFiles'); await knex.schema.dropTableIfExists('albumsLinks'); + await knex.schema.dropTableIfExists('tags'); + await knex.schema.dropTableIfExists('fileTags'); + await knex.schema.dropTableIfExists('bans'); }; diff --git a/src/api/database/migrations/20190221225813_addTags.js b/src/api/database/migrations/20190221225813_addTags.js deleted file mode 100644 index ef71877..0000000 --- a/src/api/database/migrations/20190221225813_addTags.js +++ /dev/null @@ -1,21 +0,0 @@ -exports.up = async knex => { - await knex.schema.createTable('tags', table => { - table.increments(); - table.string('uuid'); - table.integer('userId'); - table.string('name'); - table.timestamp('createdAt'); - table.timestamp('editedAt'); - }); - - await knex.schema.createTable('fileTags', table => { - table.increments(); - table.integer('fileId'); - table.integer('tagId'); - }); -}; - -exports.down = async knex => { - await knex.schema.dropTableIfExists('tags'); - await knex.schema.dropTableIfExists('fileTags'); -}; -- cgit v1.2.3 From de54e19d3a102cad6364a6f9f50dab48c2367683 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 10 May 2020 00:03:45 +0900 Subject: chore: remove the use of uuid --- src/api/database/migrations/20190221225812_initialMigration.js | 4 ---- src/api/database/seeds/initial.js | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index 84bda7e..a27a08a 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -1,7 +1,6 @@ exports.up = async knex => { await knex.schema.createTable('users', table => { table.increments(); - table.string('uuid'); table.string('username'); table.text('password'); table.boolean('enabled'); @@ -15,7 +14,6 @@ exports.up = async knex => { await knex.schema.createTable('albums', table => { table.increments(); - table.string('uuid'); table.integer('userId'); table.string('name'); table.timestamp('zippedAt'); @@ -25,7 +23,6 @@ exports.up = async knex => { await knex.schema.createTable('files', table => { table.increments(); - table.string('uuid'); table.integer('userId'); table.string('name'); table.string('original'); @@ -39,7 +36,6 @@ exports.up = async knex => { await knex.schema.createTable('links', table => { table.increments(); - table.string('uuid'); table.integer('userId'); table.integer('albumId'); table.string('identifier'); diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 280fd74..5e906fb 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -1,5 +1,6 @@ const bcrypt = require('bcrypt'); const moment = require('moment'); +const uuidv4 = require('uuid/v4'); exports.seed = async db => { const now = moment.utc().toDate(); @@ -8,6 +9,7 @@ exports.seed = async db => { try { const hash = await bcrypt.hash(process.env.ADMIN_PASSWORD, 10); await db.table('users').insert({ + uuid: uuidv4(), username: process.env.ADMIN_ACCOUNT, password: hash, passwordEditedAt: now, -- cgit v1.2.3 From 432d86022c5bf31403bc55607c6b0f1a7191e4ca Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 10 May 2020 00:39:53 +0900 Subject: chore: forgot to remove this uuid --- src/api/database/seeds/initial.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 5e906fb..280fd74 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -1,6 +1,5 @@ const bcrypt = require('bcrypt'); const moment = require('moment'); -const uuidv4 = require('uuid/v4'); exports.seed = async db => { const now = moment.utc().toDate(); @@ -9,7 +8,6 @@ exports.seed = async db => { try { const hash = await bcrypt.hash(process.env.ADMIN_PASSWORD, 10); await db.table('users').insert({ - uuid: uuidv4(), username: process.env.ADMIN_ACCOUNT, password: hash, passwordEditedAt: now, -- cgit v1.2.3 From 836a01327de6b2af5604bb77a34bc3f73b972178 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Sat, 4 Jul 2020 03:25:21 +0300 Subject: chore: add nsfw flag to migration --- src/api/database/migrations/20190221225812_initialMigration.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index a27a08a..4bcea8d 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -16,6 +16,7 @@ exports.up = async knex => { table.increments(); table.integer('userId'); table.string('name'); + table.boolean('nsfw'); table.timestamp('zippedAt'); table.timestamp('createdAt'); table.timestamp('editedAt'); @@ -28,6 +29,7 @@ exports.up = async knex => { table.string('original'); table.string('type'); table.integer('size'); + table.boolean('nsfw'); table.string('hash'); table.string('ip'); table.timestamp('createdAt'); -- cgit v1.2.3 From ad852de51a0d2dd5d29c08838d5a430c58849e74 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Wed, 8 Jul 2020 04:00:12 +0300 Subject: chore: linter the entire project using the new rules --- .../migrations/20190221225812_initialMigration.js | 22 +++++++++++----------- src/api/database/seeds/initial.js | 5 +++-- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index 4bcea8d..dd18ee5 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -1,5 +1,5 @@ -exports.up = async knex => { - await knex.schema.createTable('users', table => { +exports.up = async (knex) => { + await knex.schema.createTable('users', (table) => { table.increments(); table.string('username'); table.text('password'); @@ -12,7 +12,7 @@ exports.up = async knex => { table.timestamp('editedAt'); }); - await knex.schema.createTable('albums', table => { + await knex.schema.createTable('albums', (table) => { table.increments(); table.integer('userId'); table.string('name'); @@ -22,7 +22,7 @@ exports.up = async knex => { table.timestamp('editedAt'); }); - await knex.schema.createTable('files', table => { + await knex.schema.createTable('files', (table) => { table.increments(); table.integer('userId'); table.string('name'); @@ -36,7 +36,7 @@ exports.up = async knex => { table.timestamp('editedAt'); }); - await knex.schema.createTable('links', table => { + await knex.schema.createTable('links', (table) => { table.increments(); table.integer('userId'); table.integer('albumId'); @@ -49,19 +49,19 @@ exports.up = async knex => { table.timestamp('editedAt'); }); - await knex.schema.createTable('albumsFiles', table => { + await knex.schema.createTable('albumsFiles', (table) => { table.increments(); table.integer('albumId'); table.integer('fileId'); }); - await knex.schema.createTable('albumsLinks', table => { + await knex.schema.createTable('albumsLinks', (table) => { table.increments(); table.integer('albumId'); table.integer('linkId'); }); - await knex.schema.createTable('tags', table => { + await knex.schema.createTable('tags', (table) => { table.increments(); table.string('uuid'); table.integer('userId'); @@ -70,19 +70,19 @@ exports.up = async knex => { table.timestamp('editedAt'); }); - await knex.schema.createTable('fileTags', table => { + await knex.schema.createTable('fileTags', (table) => { table.increments(); table.integer('fileId'); table.integer('tagId'); }); - await knex.schema.createTable('bans', table => { + await knex.schema.createTable('bans', (table) => { table.increments(); table.string('ip'); table.timestamp('createdAt'); }); }; -exports.down = async knex => { +exports.down = async (knex) => { await knex.schema.dropTableIfExists('users'); await knex.schema.dropTableIfExists('albums'); await knex.schema.dropTableIfExists('files'); diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 280fd74..cdbfa80 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -1,7 +1,8 @@ +/* eslint-disable no-console */ const bcrypt = require('bcrypt'); const moment = require('moment'); -exports.seed = async db => { +exports.seed = async (db) => { const now = moment.utc().toDate(); const user = await db.table('users').where({ username: process.env.ADMIN_ACCOUNT }).first(); if (user) return; @@ -14,7 +15,7 @@ exports.seed = async db => { createdAt: now, editedAt: now, enabled: true, - isAdmin: true + isAdmin: true, }); console.log(); console.log('========================================================='); -- cgit v1.2.3 From 6713eca9d4a4887dc8d7416dbdd8ec37de7bb2ed Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Wed, 8 Jul 2020 19:22:25 +0300 Subject: chore: add unique integrity checks to the database for many-to-many tables --- .../migrations/20190221225812_initialMigration.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index dd18ee5..b755a33 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -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) => { -- cgit v1.2.3 From 90001c2df56d58e69fd199a518ae7f3e4ed327fc Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Thu, 24 Dec 2020 10:40:50 +0200 Subject: chore: remove trailing commas --- src/api/database/seeds/initial.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/database') diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index cdbfa80..2383a7b 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -15,7 +15,7 @@ exports.seed = async (db) => { createdAt: now, editedAt: now, enabled: true, - isAdmin: true, + isAdmin: true }); console.log(); console.log('========================================================='); -- cgit v1.2.3 From fb2c27086f570fec60f4d52dcc9ca80e53186293 Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 24 Dec 2020 23:45:16 +0900 Subject: Fix ESLint rules once and for all --- .../migrations/20190221225812_initialMigration.js | 22 +++++++++++----------- src/api/database/seeds/initial.js | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index b755a33..92103c1 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -1,5 +1,5 @@ -exports.up = async (knex) => { - await knex.schema.createTable('users', (table) => { +exports.up = async knex => { + await knex.schema.createTable('users', table => { table.increments(); table.string('username').unique(); table.text('password'); @@ -12,7 +12,7 @@ exports.up = async (knex) => { table.timestamp('editedAt'); }); - await knex.schema.createTable('albums', (table) => { + await knex.schema.createTable('albums', table => { table.increments(); table.integer('userId'); table.string('name'); @@ -24,7 +24,7 @@ exports.up = async (knex) => { table.unique(['userId', 'name']); }); - await knex.schema.createTable('files', (table) => { + await knex.schema.createTable('files', table => { table.increments(); table.integer('userId'); table.string('name'); @@ -38,7 +38,7 @@ exports.up = async (knex) => { table.timestamp('editedAt'); }); - await knex.schema.createTable('links', (table) => { + await knex.schema.createTable('links', table => { table.increments(); table.integer('userId'); table.integer('albumId'); @@ -53,7 +53,7 @@ exports.up = async (knex) => { table.unique(['userId', 'albumId', 'identifier']); }); - await knex.schema.createTable('albumsFiles', (table) => { + await knex.schema.createTable('albumsFiles', table => { table.increments(); table.integer('albumId'); table.integer('fileId'); @@ -61,13 +61,13 @@ exports.up = async (knex) => { table.unique(['albumId', 'fileId']); }); - await knex.schema.createTable('albumsLinks', (table) => { + await knex.schema.createTable('albumsLinks', table => { table.increments(); table.integer('albumId'); table.integer('linkId').unique(); }); - await knex.schema.createTable('tags', (table) => { + await knex.schema.createTable('tags', table => { table.increments(); table.string('uuid'); table.integer('userId'); @@ -78,7 +78,7 @@ exports.up = async (knex) => { table.unique(['userId', 'name']); }); - await knex.schema.createTable('fileTags', (table) => { + await knex.schema.createTable('fileTags', table => { table.increments(); table.integer('fileId'); table.integer('tagId'); @@ -86,13 +86,13 @@ exports.up = async (knex) => { table.unique(['fileId', 'tagId']); }); - await knex.schema.createTable('bans', (table) => { + await knex.schema.createTable('bans', table => { table.increments(); table.string('ip'); table.timestamp('createdAt'); }); }; -exports.down = async (knex) => { +exports.down = async knex => { await knex.schema.dropTableIfExists('users'); await knex.schema.dropTableIfExists('albums'); await knex.schema.dropTableIfExists('files'); diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index 2383a7b..edc1949 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -2,7 +2,7 @@ const bcrypt = require('bcrypt'); const moment = require('moment'); -exports.seed = async (db) => { +exports.seed = async db => { const now = moment.utc().toDate(); const user = await db.table('users').where({ username: process.env.ADMIN_ACCOUNT }).first(); if (user) return; -- cgit v1.2.3 From 68634418e1c86d5ebd5dc2feead241919d3aa9ed Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 27 Dec 2020 03:02:14 +0900 Subject: Squashed commit of the following: commit df4b0378571708086a276e49ac8630095e08b0b7 Author: Pitu Date: Sun Dec 27 03:00:17 2020 +0900 feat: move database modification to a new migration file --- .../migrations/20190221225812_initialMigration.js | 18 ++---------- .../migrations/20201227023216_addUniques.js | 33 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 src/api/database/migrations/20201227023216_addUniques.js (limited to 'src/api/database') diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index 92103c1..a27a08a 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -1,11 +1,11 @@ exports.up = async knex => { await knex.schema.createTable('users', table => { table.increments(); - table.string('username').unique(); + table.string('username'); table.text('password'); table.boolean('enabled'); table.boolean('isAdmin'); - table.string('apiKey').unique(); + table.string('apiKey'); table.timestamp('passwordEditedAt'); table.timestamp('apiKeyEditedAt'); table.timestamp('createdAt'); @@ -16,12 +16,9 @@ exports.up = async knex => { table.increments(); table.integer('userId'); table.string('name'); - table.boolean('nsfw').defaultTo(false); table.timestamp('zippedAt'); table.timestamp('createdAt'); table.timestamp('editedAt'); - - table.unique(['userId', 'name']); }); await knex.schema.createTable('files', table => { @@ -31,7 +28,6 @@ exports.up = async knex => { table.string('original'); table.string('type'); table.integer('size'); - table.boolean('nsfw').defaultTo(false); table.string('hash'); table.string('ip'); table.timestamp('createdAt'); @@ -49,22 +45,18 @@ 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').unique(); + table.integer('linkId'); }); await knex.schema.createTable('tags', table => { @@ -74,16 +66,12 @@ 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 => { diff --git a/src/api/database/migrations/20201227023216_addUniques.js b/src/api/database/migrations/20201227023216_addUniques.js new file mode 100644 index 0000000..14f9e7f --- /dev/null +++ b/src/api/database/migrations/20201227023216_addUniques.js @@ -0,0 +1,33 @@ +exports.up = async knex => { + await knex.schema.alterTable('users', table => { + table.unique(['username', 'apiKey']); + }); + + await knex.schema.alterTable('albums', table => { + table.boolean('nsfw').defaultTo(false); + table.unique(['userId', 'name']); + }); + + await knex.schema.alterTable('links', table => { + table.unique(['userId', 'albumId', 'identifier']); + }); + + await knex.schema.alterTable('albumsFiles', table => { + table.unique(['albumId', 'fileId']); + }); + + await knex.schema.alterTable('albumsLinks', table => { + table.unique(['linkId']); + }); + + await knex.schema.alterTable('tags', table => { + table.unique(['userId', 'name']); + }); + + await knex.schema.alterTable('fileTags', table => { + table.unique(['fileId', 'tagId']); + }); +}; +exports.down = async knex => { + // Nothing +}; -- cgit v1.2.3