aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-04-26 14:27:14 +0200
committerChocobozzz <me@florianbigard.com>2021-04-26 14:27:42 +0200
commitf816da921f6cf735a988f5ff0bb0e9d0e8e4f8ca (patch)
tree1e9c0edcd9a7e2968650a486f211acbdb78e4be7
parent185eabe1ef777ab341cc90ace3590f07197c1a8a (diff)
downloadPeerTube-f816da921f6cf735a988f5ff0bb0e9d0e8e4f8ca.tar.gz
PeerTube-f816da921f6cf735a988f5ff0bb0e9d0e8e4f8ca.tar.zst
PeerTube-f816da921f6cf735a988f5ff0bb0e9d0e8e4f8ca.zip
Add migration to remove duplicated keys
Due to old/corrupted data, it can be problematic to restore a backup
-rw-r--r--server/initializers/constants.ts2
-rw-r--r--server/initializers/migrations/0640-unique-keys.ts39
2 files changed, 40 insertions, 1 deletions
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 1802257df..37a963760 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config'
24 24
25// --------------------------------------------------------------------------- 25// ---------------------------------------------------------------------------
26 26
27const LAST_MIGRATION_VERSION = 635 27const LAST_MIGRATION_VERSION = 640
28 28
29// --------------------------------------------------------------------------- 29// ---------------------------------------------------------------------------
30 30
diff --git a/server/initializers/migrations/0640-unique-keys.ts b/server/initializers/migrations/0640-unique-keys.ts
new file mode 100644
index 000000000..b082accc2
--- /dev/null
+++ b/server/initializers/migrations/0640-unique-keys.ts
@@ -0,0 +1,39 @@
1import * as Sequelize from 'sequelize'
2
3async function up (utils: {
4 transaction: Sequelize.Transaction
5 queryInterface: Sequelize.QueryInterface
6 sequelize: Sequelize.Sequelize
7 db: any
8}): Promise<void> {
9 {
10 await utils.sequelize.query(
11 'DELETE FROM "actor" v1 USING (SELECT MIN(id) as id, "preferredUsername", "serverId" FROM "actor" ' +
12 'GROUP BY "preferredUsername", "serverId" HAVING COUNT(*) > 1 AND "serverId" IS NOT NULL) v2 ' +
13 'WHERE v1."preferredUsername" = v2."preferredUsername" AND v1."serverId" = v2."serverId" AND v1.id <> v2.id'
14 )
15 }
16
17 {
18 await utils.sequelize.query(
19 'DELETE FROM "actor" v1 USING (SELECT MIN(id) as id, "url" FROM "actor" GROUP BY "url" HAVING COUNT(*) > 1) v2 ' +
20 'WHERE v1."url" = v2."url" AND v1.id <> v2.id'
21 )
22 }
23
24 {
25 await utils.sequelize.query(
26 'DELETE FROM "tag" v1 USING (SELECT MIN(id) as id, "name" FROM "tag" GROUP BY "name" HAVING COUNT(*) > 1) v2 ' +
27 'WHERE v1."name" = v2."name" AND v1.id <> v2.id'
28 )
29 }
30}
31
32function down (options) {
33 throw new Error('Not implemented.')
34}
35
36export {
37 up,
38 down
39}