aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers')
-rw-r--r--server/initializers/checker.ts20
-rw-r--r--server/initializers/constants.ts14
-rw-r--r--server/initializers/database.ts186
-rw-r--r--server/initializers/installer.ts25
-rw-r--r--server/initializers/migrations/0065-video-file-size.ts5
-rw-r--r--server/initializers/migrations/0100-activitypub.ts6
-rw-r--r--server/initializers/migrations/0105-server-mail.ts3
-rw-r--r--server/initializers/migrations/0110-server-key.ts3
-rw-r--r--server/initializers/migrations/0115-account-avatar.ts3
-rw-r--r--server/initializers/migrations/0120-video-null.ts3
-rw-r--r--server/initializers/migrator.ts21
11 files changed, 99 insertions, 190 deletions
diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts
index 317d59423..7e76990b5 100644
--- a/server/initializers/checker.ts
+++ b/server/initializers/checker.ts
@@ -1,8 +1,8 @@
1import * as config from 'config' 1import * as config from 'config'
2import { promisify0 } from '../helpers/core-utils' 2import { promisify0 } from '../helpers'
3import { UserModel } from '../models/account/user-interface' 3import { UserModel } from '../models/account/user'
4import { ApplicationModel } from '../models/application/application-interface' 4import { ApplicationModel } from '../models/application/application'
5import { OAuthClientModel } from '../models/oauth/oauth-client-interface' 5import { OAuthClientModel } from '../models/oauth/oauth-client'
6 6
7// Some checks on configuration files 7// Some checks on configuration files
8function checkConfig () { 8function checkConfig () {
@@ -57,22 +57,22 @@ async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
57} 57}
58 58
59// We get db by param to not import it in this file (import orders) 59// We get db by param to not import it in this file (import orders)
60async function clientsExist (OAuthClient: OAuthClientModel) { 60async function clientsExist () {
61 const totalClients = await OAuthClient.countTotal() 61 const totalClients = await OAuthClientModel.countTotal()
62 62
63 return totalClients !== 0 63 return totalClients !== 0
64} 64}
65 65
66// We get db by param to not import it in this file (import orders) 66// We get db by param to not import it in this file (import orders)
67async function usersExist (User: UserModel) { 67async function usersExist () {
68 const totalUsers = await User.countTotal() 68 const totalUsers = await UserModel.countTotal()
69 69
70 return totalUsers !== 0 70 return totalUsers !== 0
71} 71}
72 72
73// We get db by param to not import it in this file (import orders) 73// We get db by param to not import it in this file (import orders)
74async function applicationExist (Application: ApplicationModel) { 74async function applicationExist () {
75 const totalApplication = await Application.countTotal() 75 const totalApplication = await ApplicationModel.countTotal()
76 76
77 return totalApplication !== 0 77 return totalApplication !== 0
78} 78}
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 7be7a5f95..f539eb2ee 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -1,16 +1,10 @@
1import * as config from 'config' 1import * as config from 'config'
2import { join } from 'path' 2import { join } from 'path'
3 3import { JobCategory, JobState, VideoRateType } from '../../shared/models'
4import { FollowState } from '../../shared/models/accounts'
5import { VideoPrivacy } from '../../shared/models/videos'
4// Do not use barrels, remain constants as independent as possible 6// Do not use barrels, remain constants as independent as possible
5import { root, isTestInstance } from '../helpers/core-utils' 7import { isTestInstance, root } from '../helpers/core-utils'
6
7import {
8 VideoRateType,
9 JobState,
10 JobCategory
11} from '../../shared/models'
12import { VideoPrivacy } from '../../shared/models/videos/video-privacy.enum'
13import { FollowState } from '../../shared/models/accounts/follow.model'
14 8
15// --------------------------------------------------------------------------- 9// ---------------------------------------------------------------------------
16 10
diff --git a/server/initializers/database.ts b/server/initializers/database.ts
index bb95992e1..f9e24c6b8 100644
--- a/server/initializers/database.ts
+++ b/server/initializers/database.ts
@@ -1,72 +1,43 @@
1import { join } from 'path' 1import { Sequelize as SequelizeTypescript } from 'sequelize-typescript'
2import { flattenDepth } from 'lodash' 2import { isTestInstance } from '../helpers/core-utils'
3require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string 3import { logger } from '../helpers/logger'
4import * as Sequelize from 'sequelize'
5import { AvatarModel } from '../models/avatar'
6 4
5import { AccountModel } from '../models/account/account'
6import { AccountFollowModel } from '../models/account/account-follow'
7import { AccountVideoRateModel } from '../models/account/account-video-rate'
8import { UserModel } from '../models/account/user'
9import { ApplicationModel } from '../models/application/application'
10import { AvatarModel } from '../models/avatar/avatar'
11import { JobModel } from '../models/job/job'
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'
20import { VideoChannelShareModel } from '../models/video/video-channel-share'
21import { VideoFileModel } from '../models/video/video-file'
22import { VideoShareModel } from '../models/video/video-share'
23import { VideoTagModel } from '../models/video/video-tag'
7import { CONFIG } from './constants' 24import { CONFIG } from './constants'
8// Do not use barrel, we need to load database first
9import { logger } from '../helpers/logger'
10import { isTestInstance, readdirPromise } from '../helpers/core-utils'
11 25
12import { VideoModel } from './../models/video/video-interface' 26require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
13import { VideoTagModel } from './../models/video/video-tag-interface'
14import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
15import { VideoFileModel } from './../models/video/video-file-interface'
16import { VideoAbuseModel } from './../models/video/video-abuse-interface'
17import { VideoChannelModel } from './../models/video/video-channel-interface'
18import { UserModel } from '../models/account/user-interface'
19import { AccountVideoRateModel } from '../models/account/account-video-rate-interface'
20import { AccountFollowModel } from '../models/account/account-follow-interface'
21import { TagModel } from './../models/video/tag-interface'
22import { ServerModel } from '../models/server/server-interface'
23import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
24import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
25import { JobModel } from './../models/job/job-interface'
26import { AccountModel } from './../models/account/account-interface'
27import { ApplicationModel } from './../models/application/application-interface'
28import { VideoChannelShareModel } from '../models/video/video-channel-share-interface'
29import { VideoShareModel } from '../models/video/video-share-interface'
30 27
31const dbname = CONFIG.DATABASE.DBNAME 28const dbname = CONFIG.DATABASE.DBNAME
32const username = CONFIG.DATABASE.USERNAME 29const username = CONFIG.DATABASE.USERNAME
33const password = CONFIG.DATABASE.PASSWORD 30const password = CONFIG.DATABASE.PASSWORD
34 31
35export type PeerTubeDatabase = { 32const sequelizeTypescript = new SequelizeTypescript({
36 sequelize?: Sequelize.Sequelize, 33 database: dbname,
37 init?: (silent: boolean) => Promise<void>,
38
39 Application?: ApplicationModel,
40 Avatar?: AvatarModel,
41 Account?: AccountModel,
42 Job?: JobModel,
43 OAuthClient?: OAuthClientModel,
44 OAuthToken?: OAuthTokenModel,
45 Server?: ServerModel,
46 Tag?: TagModel,
47 AccountVideoRate?: AccountVideoRateModel,
48 AccountFollow?: AccountFollowModel,
49 User?: UserModel,
50 VideoAbuse?: VideoAbuseModel,
51 VideoChannel?: VideoChannelModel,
52 VideoChannelShare?: VideoChannelShareModel,
53 VideoShare?: VideoShareModel,
54 VideoFile?: VideoFileModel,
55 BlacklistedVideo?: BlacklistedVideoModel,
56 VideoTag?: VideoTagModel,
57 Video?: VideoModel
58}
59
60const database: PeerTubeDatabase = {}
61
62const sequelize = new Sequelize(dbname, username, password, {
63 dialect: 'postgres', 34 dialect: 'postgres',
64 host: CONFIG.DATABASE.HOSTNAME, 35 username,
65 port: CONFIG.DATABASE.PORT, 36 password,
37 modelPaths: [__dirname + '/models'],
66 benchmark: isTestInstance(), 38 benchmark: isTestInstance(),
67 isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE, 39 isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
68 operatorsAliases: false, 40 operatorsAliases: false,
69
70 logging: (message: string, benchmark: number) => { 41 logging: (message: string, benchmark: number) => {
71 if (process.env.NODE_DB_LOG === 'false') return 42 if (process.env.NODE_DB_LOG === 'false') return
72 43
@@ -79,34 +50,28 @@ const sequelize = new Sequelize(dbname, username, password, {
79 } 50 }
80}) 51})
81 52
82database.sequelize = sequelize 53async function initDatabase (silent: boolean) {
83 54 sequelizeTypescript.addModels([
84database.init = async (silent: boolean) => { 55 ApplicationModel,
85 const modelDirectory = join(__dirname, '..', 'models') 56 AvatarModel,
86 57 AccountModel,
87 const filePaths = await getModelFiles(modelDirectory) 58 JobModel,
88 59 OAuthClientModel,
89 for (const filePath of filePaths) { 60 OAuthTokenModel,
90 try { 61 ServerModel,
91 const model = sequelize.import(filePath) 62 TagModel,
92 63 AccountVideoRateModel,
93 database[model['name']] = model 64 AccountFollowModel,
94 } catch (err) { 65 UserModel,
95 logger.error('Cannot import database model %s.', filePath, err) 66 VideoAbuseModel,
96 process.exit(0) 67 VideoChannelModel,
97 } 68 VideoChannelShareModel,
98 } 69 VideoShareModel,
99 70 VideoFileModel,
100 for (const modelName of Object.keys(database)) { 71 VideoBlacklistModel,
101 if ('associate' in database[modelName]) { 72 VideoTagModel,
102 try { 73 VideoModel
103 database[modelName].associate(database) 74 ])
104 } catch (err) {
105 logger.error('Cannot associate model %s.', modelName, err)
106 process.exit(0)
107 }
108 }
109 }
110 75
111 if (!silent) logger.info('Database %s is ready.', dbname) 76 if (!silent) logger.info('Database %s is ready.', dbname)
112 77
@@ -116,51 +81,6 @@ database.init = async (silent: boolean) => {
116// --------------------------------------------------------------------------- 81// ---------------------------------------------------------------------------
117 82
118export { 83export {
119 database 84 initDatabase,
120} 85 sequelizeTypescript
121
122// ---------------------------------------------------------------------------
123
124async function getModelFiles (modelDirectory: string) {
125 const files = await readdirPromise(modelDirectory)
126 const directories = files.filter(directory => {
127 // Find directories
128 if (
129 directory.endsWith('.js.map') ||
130 directory === 'index.js' || directory === 'index.ts' ||
131 directory === 'utils.js' || directory === 'utils.ts'
132 ) return false
133
134 return true
135 })
136
137 const tasks: Promise<any>[] = []
138
139 // For each directory we read it and append model in the modelFilePaths array
140 for (const directory of directories) {
141 const modelDirectoryPath = join(modelDirectory, directory)
142
143 const promise = readdirPromise(modelDirectoryPath)
144 .then(files => {
145 const filteredFiles = files
146 .filter(file => {
147 if (
148 file === 'index.js' || file === 'index.ts' ||
149 file === 'utils.js' || file === 'utils.ts' ||
150 file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
151 file.endsWith('.js.map')
152 ) return false
153
154 return true
155 })
156 .map(file => join(modelDirectoryPath, file))
157
158 return filteredFiles
159 })
160
161 tasks.push(promise)
162 }
163
164 const filteredFilesArray: string[][] = await Promise.all(tasks)
165 return flattenDepth<string>(filteredFilesArray, 1)
166} 86}
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts
index 954516057..5452743b6 100644
--- a/server/initializers/installer.ts
+++ b/server/initializers/installer.ts
@@ -1,16 +1,17 @@
1import * as passwordGenerator from 'password-generator' 1import * as passwordGenerator from 'password-generator'
2import { UserRole } from '../../shared' 2import { UserRole } from '../../shared'
3import { logger, mkdirpPromise, rimrafPromise } from '../helpers' 3import { createPrivateAndPublicKeys, logger, mkdirpPromise, rimrafPromise } from '../helpers'
4import { createUserAccountAndChannel } from '../lib' 4import { createLocalAccountWithoutKeys, createUserAccountAndChannel } from '../lib'
5import { createLocalAccountWithoutKeys } from '../lib/user' 5import { UserModel } from '../models/account/user'
6import { ApplicationModel } from '../models/application/application'
7import { OAuthClientModel } from '../models/oauth/oauth-client'
6import { applicationExist, clientsExist, usersExist } from './checker' 8import { applicationExist, clientsExist, usersExist } from './checker'
7import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants' 9import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
8import { database as db } from './database' 10import { sequelizeTypescript } from './database'
9import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
10 11
11async function installApplication () { 12async function installApplication () {
12 try { 13 try {
13 await db.sequelize.sync() 14 await sequelizeTypescript.sync()
14 await removeCacheDirectories() 15 await removeCacheDirectories()
15 await createDirectoriesIfNotExist() 16 await createDirectoriesIfNotExist()
16 await createApplicationIfNotExist() 17 await createApplicationIfNotExist()
@@ -64,7 +65,7 @@ function createDirectoriesIfNotExist () {
64} 65}
65 66
66async function createOAuthClientIfNotExist () { 67async function createOAuthClientIfNotExist () {
67 const exist = await clientsExist(db.OAuthClient) 68 const exist = await clientsExist()
68 // Nothing to do, clients already exist 69 // Nothing to do, clients already exist
69 if (exist === true) return undefined 70 if (exist === true) return undefined
70 71
@@ -72,7 +73,7 @@ async function createOAuthClientIfNotExist () {
72 73
73 const id = passwordGenerator(32, false, /[a-z0-9]/) 74 const id = passwordGenerator(32, false, /[a-z0-9]/)
74 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) 75 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
75 const client = db.OAuthClient.build({ 76 const client = new OAuthClientModel({
76 clientId: id, 77 clientId: id,
77 clientSecret: secret, 78 clientSecret: secret,
78 grants: [ 'password', 'refresh_token' ], 79 grants: [ 'password', 'refresh_token' ],
@@ -87,7 +88,7 @@ async function createOAuthClientIfNotExist () {
87} 88}
88 89
89async function createOAuthAdminIfNotExist () { 90async function createOAuthAdminIfNotExist () {
90 const exist = await usersExist(db.User) 91 const exist = await usersExist()
91 // Nothing to do, users already exist 92 // Nothing to do, users already exist
92 if (exist === true) return undefined 93 if (exist === true) return undefined
93 94
@@ -120,7 +121,7 @@ async function createOAuthAdminIfNotExist () {
120 role, 121 role,
121 videoQuota: -1 122 videoQuota: -1
122 } 123 }
123 const user = db.User.build(userData) 124 const user = new UserModel(userData)
124 125
125 await createUserAccountAndChannel(user, validatePassword) 126 await createUserAccountAndChannel(user, validatePassword)
126 logger.info('Username: ' + username) 127 logger.info('Username: ' + username)
@@ -128,12 +129,12 @@ async function createOAuthAdminIfNotExist () {
128} 129}
129 130
130async function createApplicationIfNotExist () { 131async function createApplicationIfNotExist () {
131 const exist = await applicationExist(db.Application) 132 const exist = await applicationExist()
132 // Nothing to do, application already exist 133 // Nothing to do, application already exist
133 if (exist === true) return undefined 134 if (exist === true) return undefined
134 135
135 logger.info('Creating Application table.') 136 logger.info('Creating Application table.')
136 const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) 137 const applicationInstance = await ApplicationModel.create({ migrationVersion: LAST_MIGRATION_VERSION })
137 138
138 logger.info('Creating application account.') 139 logger.info('Creating application account.')
139 140
diff --git a/server/initializers/migrations/0065-video-file-size.ts b/server/initializers/migrations/0065-video-file-size.ts
index 58f8f3bcc..4e2075f8b 100644
--- a/server/initializers/migrations/0065-video-file-size.ts
+++ b/server/initializers/migrations/0065-video-file-size.ts
@@ -1,8 +1,7 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import * as Promise from 'bluebird' 2import * as Promise from 'bluebird'
3import { stat } from 'fs' 3import { stat } from 'fs'
4 4import { VideoModel } from '../../models/video/video'
5import { VideoInstance } from '../../models'
6 5
7function up (utils: { 6function up (utils: {
8 transaction: Sequelize.Transaction, 7 transaction: Sequelize.Transaction,
@@ -11,7 +10,7 @@ function up (utils: {
11 db: any 10 db: any
12}): Promise<void> { 11}): Promise<void> {
13 return utils.db.Video.listOwnedAndPopulateAuthorAndTags() 12 return utils.db.Video.listOwnedAndPopulateAuthorAndTags()
14 .then((videos: VideoInstance[]) => { 13 .then((videos: VideoModel[]) => {
15 const tasks: Promise<any>[] = [] 14 const tasks: Promise<any>[] = []
16 15
17 videos.forEach(video => { 16 videos.forEach(video => {
diff --git a/server/initializers/migrations/0100-activitypub.ts b/server/initializers/migrations/0100-activitypub.ts
index 50a0adc14..fb42e1d57 100644
--- a/server/initializers/migrations/0100-activitypub.ts
+++ b/server/initializers/migrations/0100-activitypub.ts
@@ -4,14 +4,14 @@ import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
4import { shareVideoByServer } from '../../lib/activitypub/share' 4import { shareVideoByServer } from '../../lib/activitypub/share'
5import { getVideoActivityPubUrl, getVideoChannelActivityPubUrl } from '../../lib/activitypub/url' 5import { getVideoActivityPubUrl, getVideoChannelActivityPubUrl } from '../../lib/activitypub/url'
6import { createLocalAccountWithoutKeys } from '../../lib/user' 6import { createLocalAccountWithoutKeys } from '../../lib/user'
7import { ApplicationModel } from '../../models/application/application'
7import { JOB_CATEGORIES, SERVER_ACCOUNT_NAME } from '../constants' 8import { JOB_CATEGORIES, SERVER_ACCOUNT_NAME } from '../constants'
8import { PeerTubeDatabase } from '../database'
9 9
10async function up (utils: { 10async function up (utils: {
11 transaction: Sequelize.Transaction, 11 transaction: Sequelize.Transaction,
12 queryInterface: Sequelize.QueryInterface, 12 queryInterface: Sequelize.QueryInterface,
13 sequelize: Sequelize.Sequelize, 13 sequelize: Sequelize.Sequelize,
14 db: PeerTubeDatabase 14 db: any
15}): Promise<void> { 15}): Promise<void> {
16 const q = utils.queryInterface 16 const q = utils.queryInterface
17 const db = utils.db 17 const db = utils.db
@@ -65,7 +65,7 @@ async function up (utils: {
65 65
66 // Create application account 66 // Create application account
67 { 67 {
68 const applicationInstance = await db.Application.findOne() 68 const applicationInstance = await ApplicationModel.findOne()
69 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined) 69 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)
70 70
71 const { publicKey, privateKey } = await createPrivateAndPublicKeys() 71 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
diff --git a/server/initializers/migrations/0105-server-mail.ts b/server/initializers/migrations/0105-server-mail.ts
index 5836992d5..4b9600e91 100644
--- a/server/initializers/migrations/0105-server-mail.ts
+++ b/server/initializers/migrations/0105-server-mail.ts
@@ -1,11 +1,10 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { PeerTubeDatabase } from '../database'
3 2
4async function up (utils: { 3async function up (utils: {
5 transaction: Sequelize.Transaction, 4 transaction: Sequelize.Transaction,
6 queryInterface: Sequelize.QueryInterface, 5 queryInterface: Sequelize.QueryInterface,
7 sequelize: Sequelize.Sequelize, 6 sequelize: Sequelize.Sequelize,
8 db: PeerTubeDatabase 7 db: any
9}): Promise<void> { 8}): Promise<void> {
10 await utils.queryInterface.removeColumn('Servers', 'email') 9 await utils.queryInterface.removeColumn('Servers', 'email')
11} 10}
diff --git a/server/initializers/migrations/0110-server-key.ts b/server/initializers/migrations/0110-server-key.ts
index 560353945..5ff6daf69 100644
--- a/server/initializers/migrations/0110-server-key.ts
+++ b/server/initializers/migrations/0110-server-key.ts
@@ -1,11 +1,10 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { PeerTubeDatabase } from '../database'
3 2
4async function up (utils: { 3async function up (utils: {
5 transaction: Sequelize.Transaction, 4 transaction: Sequelize.Transaction,
6 queryInterface: Sequelize.QueryInterface, 5 queryInterface: Sequelize.QueryInterface,
7 sequelize: Sequelize.Sequelize, 6 sequelize: Sequelize.Sequelize,
8 db: PeerTubeDatabase 7 db: any
9}): Promise<void> { 8}): Promise<void> {
10 await utils.queryInterface.removeColumn('Servers', 'publicKey') 9 await utils.queryInterface.removeColumn('Servers', 'publicKey')
11} 10}
diff --git a/server/initializers/migrations/0115-account-avatar.ts b/server/initializers/migrations/0115-account-avatar.ts
index 2b947ceda..b318e8163 100644
--- a/server/initializers/migrations/0115-account-avatar.ts
+++ b/server/initializers/migrations/0115-account-avatar.ts
@@ -1,11 +1,10 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { PeerTubeDatabase } from '../database'
3 2
4async function up (utils: { 3async function up (utils: {
5 transaction: Sequelize.Transaction, 4 transaction: Sequelize.Transaction,
6 queryInterface: Sequelize.QueryInterface, 5 queryInterface: Sequelize.QueryInterface,
7 sequelize: Sequelize.Sequelize, 6 sequelize: Sequelize.Sequelize,
8 db: PeerTubeDatabase 7 db: any
9}): Promise<void> { 8}): Promise<void> {
10 await utils.db.Avatar.sync() 9 await utils.db.Avatar.sync()
11 10
diff --git a/server/initializers/migrations/0120-video-null.ts b/server/initializers/migrations/0120-video-null.ts
index 9130d10ee..63f3984dd 100644
--- a/server/initializers/migrations/0120-video-null.ts
+++ b/server/initializers/migrations/0120-video-null.ts
@@ -1,12 +1,11 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { CONSTRAINTS_FIELDS } from '../constants' 2import { CONSTRAINTS_FIELDS } from '../constants'
3import { PeerTubeDatabase } from '../database'
4 3
5async function up (utils: { 4async function up (utils: {
6 transaction: Sequelize.Transaction, 5 transaction: Sequelize.Transaction,
7 queryInterface: Sequelize.QueryInterface, 6 queryInterface: Sequelize.QueryInterface,
8 sequelize: Sequelize.Sequelize, 7 sequelize: Sequelize.Sequelize,
9 db: PeerTubeDatabase 8 db: any
10}): Promise<void> { 9}): Promise<void> {
11 10
12 { 11 {
diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts
index 187c9be6e..f3a05cc8c 100644
--- a/server/initializers/migrator.ts
+++ b/server/initializers/migrator.ts
@@ -1,19 +1,19 @@
1import * as path from 'path' 1import * as path from 'path'
2
3import { database as db } from './database'
4import { LAST_MIGRATION_VERSION } from './constants'
5import { logger, readdirPromise } from '../helpers' 2import { logger, readdirPromise } from '../helpers'
3import { ApplicationModel } from '../models/application/application'
4import { LAST_MIGRATION_VERSION } from './constants'
5import { sequelizeTypescript } from './database'
6 6
7async function migrate () { 7async function migrate () {
8 const tables = await db.sequelize.getQueryInterface().showAllTables() 8 const tables = await sequelizeTypescript.getQueryInterface().showAllTables()
9 9
10 // No tables, we don't need to migrate anything 10 // No tables, we don't need to migrate anything
11 // The installer will do that 11 // The installer will do that
12 if (tables.length === 0) return 12 if (tables.length === 0) return
13 13
14 let actualVersion = await db.Application.loadMigrationVersion() 14 let actualVersion = await ApplicationModel.loadMigrationVersion()
15 if (actualVersion === null) { 15 if (actualVersion === null) {
16 await db.Application.create({ migrationVersion: 0 }) 16 await ApplicationModel.create({ migrationVersion: 0 })
17 actualVersion = 0 17 actualVersion = 0
18 } 18 }
19 19
@@ -78,17 +78,16 @@ async function executeMigration (actualVersion: number, entity: { version: strin
78 78
79 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) 79 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
80 80
81 await db.sequelize.transaction(async t => { 81 await sequelizeTypescript.transaction(async t => {
82 const options = { 82 const options = {
83 transaction: t, 83 transaction: t,
84 queryInterface: db.sequelize.getQueryInterface(), 84 queryInterface: sequelizeTypescript.getQueryInterface(),
85 sequelize: db.sequelize, 85 sequelize: sequelizeTypescript
86 db
87 } 86 }
88 87
89 await migrationScript.up(options) 88 await migrationScript.up(options)
90 89
91 // Update the new migration version 90 // Update the new migration version
92 await db.Application.updateMigrationVersion(versionScript, t) 91 await ApplicationModel.updateMigrationVersion(versionScript, t)
93 }) 92 })
94} 93}