Delete invalid or deleted remote videos
[oweals/peertube.git] / server / initializers / database.ts
1 import { Sequelize as SequelizeTypescript } from 'sequelize-typescript'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { logger } from '../helpers/logger'
4
5 import { AccountModel } from '../models/account/account'
6 import { AccountVideoRateModel } from '../models/account/account-video-rate'
7 import { UserModel } from '../models/account/user'
8 import { ActorModel } from '../models/activitypub/actor'
9 import { ActorFollowModel } from '../models/activitypub/actor-follow'
10 import { ApplicationModel } from '../models/application/application'
11 import { AvatarModel } from '../models/avatar/avatar'
12 import { OAuthClientModel } from '../models/oauth/oauth-client'
13 import { OAuthTokenModel } from '../models/oauth/oauth-token'
14 import { ServerModel } from '../models/server/server'
15 import { TagModel } from '../models/video/tag'
16 import { VideoModel } from '../models/video/video'
17 import { VideoAbuseModel } from '../models/video/video-abuse'
18 import { VideoBlacklistModel } from '../models/video/video-blacklist'
19 import { VideoChannelModel } from '../models/video/video-channel'
20 import { VideoCommentModel } from '../models/video/video-comment'
21 import { VideoFileModel } from '../models/video/video-file'
22 import { VideoShareModel } from '../models/video/video-share'
23 import { VideoTagModel } from '../models/video/video-tag'
24 import { CONFIG } from './constants'
25 import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update'
26 import { VideoCaptionModel } from '../models/video/video-caption'
27 import { VideoImportModel } from '../models/video/video-import'
28 import { VideoViewModel } from '../models/video/video-views'
29 import { VideoChangeOwnershipModel } from '../models/video/video-change-ownership'
30 import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
31 import { UserVideoHistoryModel } from '../models/account/user-video-history'
32 import { AccountBlocklistModel } from '../models/account/account-blocklist'
33 import { ServerBlocklistModel } from '../models/server/server-blocklist'
34
35 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
36
37 const dbname = CONFIG.DATABASE.DBNAME
38 const username = CONFIG.DATABASE.USERNAME
39 const password = CONFIG.DATABASE.PASSWORD
40 const host = CONFIG.DATABASE.HOSTNAME
41 const port = CONFIG.DATABASE.PORT
42 const poolMax = CONFIG.DATABASE.POOL.MAX
43
44 const sequelizeTypescript = new SequelizeTypescript({
45   database: dbname,
46   dialect: 'postgres',
47   host,
48   port,
49   username,
50   password,
51   pool: {
52     max: poolMax
53   },
54   benchmark: isTestInstance(),
55   isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
56   operatorsAliases: false,
57   logging: (message: string, benchmark: number) => {
58     if (process.env.NODE_DB_LOG === 'false') return
59
60     let newMessage = message
61     if (isTestInstance() === true && benchmark !== undefined) {
62       newMessage += ' | ' + benchmark + 'ms'
63     }
64
65     logger.debug(newMessage)
66   }
67 })
68
69 async function initDatabaseModels (silent: boolean) {
70   sequelizeTypescript.addModels([
71     ApplicationModel,
72     ActorModel,
73     ActorFollowModel,
74     AvatarModel,
75     AccountModel,
76     OAuthClientModel,
77     OAuthTokenModel,
78     ServerModel,
79     TagModel,
80     AccountVideoRateModel,
81     UserModel,
82     VideoAbuseModel,
83     VideoChangeOwnershipModel,
84     VideoChannelModel,
85     VideoShareModel,
86     VideoFileModel,
87     VideoCaptionModel,
88     VideoBlacklistModel,
89     VideoTagModel,
90     VideoModel,
91     VideoCommentModel,
92     ScheduleVideoUpdateModel,
93     VideoImportModel,
94     VideoViewModel,
95     VideoRedundancyModel,
96     UserVideoHistoryModel,
97     AccountBlocklistModel,
98     ServerBlocklistModel
99   ])
100
101   // Check extensions exist in the database
102   await checkPostgresExtensions()
103
104   // Create custom PostgreSQL functions
105   await createFunctions()
106
107   if (!silent) logger.info('Database %s is ready.', dbname)
108
109   return
110 }
111
112 // ---------------------------------------------------------------------------
113
114 export {
115   initDatabaseModels,
116   sequelizeTypescript
117 }
118
119 // ---------------------------------------------------------------------------
120
121 async function checkPostgresExtensions () {
122   const promises = [
123     checkPostgresExtension('pg_trgm'),
124     checkPostgresExtension('unaccent')
125   ]
126
127   return Promise.all(promises)
128 }
129
130 async function checkPostgresExtension (extension: string) {
131   const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
132   const [ res ] = await sequelizeTypescript.query(query, { raw: true })
133
134   if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
135     // Try to create the extension ourself
136     try {
137       await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
138
139     } catch {
140       const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
141         `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
142       throw new Error(errorMessage)
143     }
144   }
145 }
146
147 async function createFunctions () {
148   const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text)
149   RETURNS text AS
150 $func$
151 SELECT public.unaccent('public.unaccent', $1::text)
152 $func$  LANGUAGE sql IMMUTABLE;`
153
154   return sequelizeTypescript.query(query, { raw: true })
155 }