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