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