]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
Begin advanced search
[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'
fdbda9e3 27
3fd3ab2d 28require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
8c308c2b 29
65fcc311
C
30const dbname = CONFIG.DATABASE.DBNAME
31const username = CONFIG.DATABASE.USERNAME
32const password = CONFIG.DATABASE.PASSWORD
228077ef
C
33const host = CONFIG.DATABASE.HOSTNAME
34const port = CONFIG.DATABASE.PORT
8c308c2b 35
3fd3ab2d
C
36const sequelizeTypescript = new SequelizeTypescript({
37 database: dbname,
feb4bdfd 38 dialect: 'postgres',
228077ef
C
39 host,
40 port,
3fd3ab2d
C
41 username,
42 password,
65fcc311 43 benchmark: isTestInstance(),
3fd3ab2d 44 isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
c2962505 45 operatorsAliases: false,
075f16ca 46 logging: (message: string, benchmark: number) => {
165cdc75
C
47 if (process.env.NODE_DB_LOG === 'false') return
48
7920c273 49 let newMessage = message
769d3321 50 if (isTestInstance() === true && benchmark !== undefined) {
7920c273
C
51 newMessage += ' | ' + benchmark + 'ms'
52 }
53
54 logger.debug(newMessage)
55 }
feb4bdfd
C
56})
57
91fea9fc 58async function initDatabaseModels (silent: boolean) {
3fd3ab2d
C
59 sequelizeTypescript.addModels([
60 ApplicationModel,
50d6de9c
C
61 ActorModel,
62 ActorFollowModel,
3fd3ab2d
C
63 AvatarModel,
64 AccountModel,
3fd3ab2d
C
65 OAuthClientModel,
66 OAuthTokenModel,
67 ServerModel,
68 TagModel,
69 AccountVideoRateModel,
3fd3ab2d
C
70 UserModel,
71 VideoAbuseModel,
72 VideoChannelModel,
3fd3ab2d
C
73 VideoShareModel,
74 VideoFileModel,
40e87e9e 75 VideoCaptionModel,
3fd3ab2d
C
76 VideoBlacklistModel,
77 VideoTagModel,
6d852470 78 VideoModel,
2baea0c7
C
79 VideoCommentModel,
80 ScheduleVideoUpdateModel
3fd3ab2d 81 ])
b769007f 82
57c36b27
C
83 // Check extensions exist in the database
84 await checkPostgresExtensions()
85
86 // Create custom PostgreSQL functions
87 await createFunctions()
88
89 await sequelizeTypescript.query('CREATE EXTENSION IF NOT EXISTS pg_trgm', { raw: true })
90
f5028693
C
91 if (!silent) logger.info('Database %s is ready.', dbname)
92
d412e80e 93 return
b769007f 94}
65fcc311
C
95
96// ---------------------------------------------------------------------------
97
e02643f3 98export {
91fea9fc 99 initDatabaseModels,
3fd3ab2d 100 sequelizeTypescript
74889a71 101}
57c36b27
C
102
103// ---------------------------------------------------------------------------
104
105async function checkPostgresExtensions () {
106 const extensions = [
107 'pg_trgm',
108 'unaccent'
109 ]
110
111 for (const extension of extensions) {
112 const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
113 const [ res ] = await sequelizeTypescript.query(query, { raw: true })
114
115 if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
116 // Try to create the extension ourself
117 try {
118 await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
119
120 } catch {
121 const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
122 `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
123 throw new Error(errorMessage)
124 }
125 }
126 }
127}
128
129async function createFunctions () {
130 const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(varchar)
131 RETURNS text AS $$
132 SELECT unaccent($1)
133 $$ LANGUAGE sql IMMUTABLE;`
134
135 return sequelizeTypescript.query(query, { raw: true })
136}