3.0.0.mjs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. import { v4 as uuid } from 'uuid'
  2. import bcrypt from 'bcryptjs'
  3. import crypto from 'node:crypto'
  4. import { DateTime } from 'luxon'
  5. import { pem2jwk } from 'pem-jwk'
  6. export async function up (knex) {
  7. WIKI.logger.info('Running 3.0.0 database migration...')
  8. // =====================================
  9. // PG EXTENSIONS
  10. // =====================================
  11. await knex.raw('CREATE EXTENSION IF NOT EXISTS ltree;')
  12. await knex.raw('CREATE EXTENSION IF NOT EXISTS pgcrypto;')
  13. await knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm;')
  14. await knex.schema
  15. // =====================================
  16. // MODEL TABLES
  17. // =====================================
  18. // ACTIVITY LOGS -----------------------
  19. .createTable('activityLogs', table => {
  20. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  21. table.timestamp('ts').notNullable().defaultTo(knex.fn.now())
  22. table.string('action').notNullable()
  23. table.jsonb('meta').notNullable()
  24. })
  25. // ANALYTICS ---------------------------
  26. .createTable('analytics', table => {
  27. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  28. table.string('module').notNullable()
  29. table.boolean('isEnabled').notNullable().defaultTo(false)
  30. table.jsonb('config').notNullable()
  31. })
  32. // API KEYS ----------------------------
  33. .createTable('apiKeys', table => {
  34. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  35. table.string('name').notNullable()
  36. table.text('key').notNullable()
  37. table.timestamp('expiration').notNullable().defaultTo(knex.fn.now())
  38. table.boolean('isRevoked').notNullable().defaultTo(false)
  39. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  40. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  41. })
  42. // ASSETS ------------------------------
  43. .createTable('assets', table => {
  44. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  45. table.string('fileName').notNullable()
  46. table.string('fileExt').notNullable()
  47. table.boolean('isSystem').notNullable().defaultTo(false)
  48. table.enum('kind', ['document', 'image', 'other']).notNullable().defaultTo('other')
  49. table.string('mimeType').notNullable().defaultTo('application/octet-stream')
  50. table.integer('fileSize').unsigned().comment('In bytes')
  51. table.jsonb('meta').notNullable().defaultTo('{}')
  52. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  53. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  54. table.binary('data')
  55. table.binary('preview')
  56. table.enum('previewState', ['none', 'pending', 'ready', 'failed']).notNullable().defaultTo('none')
  57. table.jsonb('storageInfo')
  58. })
  59. // AUTHENTICATION ----------------------
  60. .createTable('authentication', table => {
  61. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  62. table.string('module').notNullable()
  63. table.boolean('isEnabled').notNullable().defaultTo(false)
  64. table.string('displayName').notNullable().defaultTo('')
  65. table.jsonb('config').notNullable().defaultTo('{}')
  66. table.boolean('selfRegistration').notNullable().defaultTo(false)
  67. table.jsonb('domainWhitelist').notNullable().defaultTo('[]')
  68. table.jsonb('autoEnrollGroups').notNullable().defaultTo('[]')
  69. })
  70. .createTable('commentProviders', table => {
  71. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  72. table.string('module').notNullable()
  73. table.boolean('isEnabled').notNullable().defaultTo(false)
  74. table.json('config').notNullable()
  75. })
  76. // COMMENTS ----------------------------
  77. .createTable('comments', table => {
  78. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  79. table.uuid('replyTo')
  80. table.text('content').notNullable()
  81. table.text('render').notNullable().defaultTo('')
  82. table.string('name').notNullable().defaultTo('')
  83. table.string('email').notNullable().defaultTo('')
  84. table.string('ip').notNullable().defaultTo('')
  85. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  86. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  87. })
  88. // GROUPS ------------------------------
  89. .createTable('groups', table => {
  90. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  91. table.string('name').notNullable()
  92. table.jsonb('permissions').notNullable()
  93. table.jsonb('rules').notNullable()
  94. table.string('redirectOnLogin').notNullable().defaultTo('')
  95. table.string('redirectOnFirstLogin').notNullable().defaultTo('')
  96. table.string('redirectOnLogout').notNullable().defaultTo('')
  97. table.boolean('isSystem').notNullable().defaultTo(false)
  98. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  99. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  100. })
  101. // HOOKS -------------------------------
  102. .createTable('hooks', table => {
  103. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  104. table.string('name').notNullable()
  105. table.jsonb('events').notNullable().defaultTo('[]')
  106. table.string('url').notNullable()
  107. table.boolean('includeMetadata').notNullable().defaultTo(false)
  108. table.boolean('includeContent').notNullable().defaultTo(false)
  109. table.boolean('acceptUntrusted').notNullable().defaultTo(false)
  110. table.string('authHeader')
  111. table.enum('state', ['pending', 'error', 'success']).notNullable().defaultTo('pending')
  112. table.string('lastErrorMessage')
  113. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  114. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  115. })
  116. // JOB HISTORY -------------------------
  117. .createTable('jobHistory', table => {
  118. table.uuid('id').notNullable().primary()
  119. table.string('task').notNullable()
  120. table.enum('state', ['active', 'completed', 'failed', 'interrupted']).notNullable()
  121. table.boolean('useWorker').notNullable().defaultTo(false)
  122. table.boolean('wasScheduled').notNullable().defaultTo(false)
  123. table.jsonb('payload')
  124. table.integer('attempt').notNullable().defaultTo(1)
  125. table.integer('maxRetries').notNullable().defaultTo(0)
  126. table.text('lastErrorMessage')
  127. table.string('executedBy')
  128. table.timestamp('createdAt').notNullable()
  129. table.timestamp('startedAt').notNullable().defaultTo(knex.fn.now())
  130. table.timestamp('completedAt')
  131. })
  132. // JOB SCHEDULE ------------------------
  133. .createTable('jobSchedule', table => {
  134. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  135. table.string('task').notNullable()
  136. table.string('cron').notNullable()
  137. table.string('type').notNullable().defaultTo('system')
  138. table.jsonb('payload')
  139. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  140. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  141. })
  142. // JOB SCHEDULE ------------------------
  143. .createTable('jobLock', table => {
  144. table.string('key').notNullable().primary()
  145. table.string('lastCheckedBy')
  146. table.timestamp('lastCheckedAt').notNullable().defaultTo(knex.fn.now())
  147. })
  148. // JOBS --------------------------------
  149. .createTable('jobs', table => {
  150. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  151. table.string('task').notNullable()
  152. table.boolean('useWorker').notNullable().defaultTo(false)
  153. table.jsonb('payload')
  154. table.integer('retries').notNullable().defaultTo(0)
  155. table.integer('maxRetries').notNullable().defaultTo(0)
  156. table.timestamp('waitUntil')
  157. table.boolean('isScheduled').notNullable().defaultTo(false)
  158. table.string('createdBy')
  159. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  160. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  161. })
  162. // LOCALES -----------------------------
  163. .createTable('locales', table => {
  164. table.string('code', 10).notNullable().primary()
  165. table.string('name').notNullable()
  166. table.string('nativeName').notNullable()
  167. table.string('language', 2).notNullable().index()
  168. table.string('region', 2)
  169. table.string('script', 4)
  170. table.boolean('isRTL').notNullable().defaultTo(false)
  171. table.jsonb('strings')
  172. table.integer('completeness').notNullable().defaultTo(0)
  173. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  174. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  175. })
  176. // NAVIGATION ----------------------------
  177. .createTable('navigation', table => {
  178. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  179. table.jsonb('items').notNullable().defaultTo('[]')
  180. })
  181. // PAGE HISTORY ------------------------
  182. .createTable('pageHistory', table => {
  183. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  184. table.uuid('pageId').notNullable().index()
  185. table.string('action').defaultTo('updated')
  186. table.jsonb('affectedFields').notNullable().defaultTo('[]')
  187. table.string('locale', 10).notNullable().defaultTo('en')
  188. table.string('path').notNullable()
  189. table.string('hash').notNullable()
  190. table.string('alias')
  191. table.string('title').notNullable()
  192. table.string('description')
  193. table.string('icon')
  194. table.enu('publishState', ['draft', 'published', 'scheduled']).notNullable().defaultTo('draft')
  195. table.timestamp('publishStartDate')
  196. table.timestamp('publishEndDate')
  197. table.jsonb('config').notNullable().defaultTo('{}')
  198. table.jsonb('relations').notNullable().defaultTo('[]')
  199. table.text('content')
  200. table.text('render')
  201. table.jsonb('toc')
  202. table.string('editor').notNullable()
  203. table.string('contentType').notNullable()
  204. table.jsonb('scripts').notNullable().defaultTo('{}')
  205. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  206. table.timestamp('versionDate').notNullable().defaultTo(knex.fn.now())
  207. })
  208. // PAGE LINKS --------------------------
  209. .createTable('pageLinks', table => {
  210. table.increments('id').primary()
  211. table.string('path').notNullable()
  212. table.string('locale', 10).notNullable()
  213. })
  214. // PAGES -------------------------------
  215. .createTable('pages', table => {
  216. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  217. table.string('locale', 10).notNullable()
  218. table.string('path').notNullable()
  219. table.string('hash').notNullable()
  220. table.string('alias')
  221. table.string('title').notNullable()
  222. table.string('description')
  223. table.string('icon')
  224. table.enu('publishState', ['draft', 'published', 'scheduled']).notNullable().defaultTo('draft')
  225. table.timestamp('publishStartDate')
  226. table.timestamp('publishEndDate')
  227. table.jsonb('config').notNullable().defaultTo('{}')
  228. table.jsonb('relations').notNullable().defaultTo('[]')
  229. table.text('content')
  230. table.text('render')
  231. table.text('searchContent')
  232. table.specificType('ts', 'tsvector').index('ts_idx', { indexType: 'GIN' })
  233. table.specificType('tags', 'text[]').index('tags_idx', { indexType: 'GIN' })
  234. table.jsonb('toc')
  235. table.string('editor').notNullable()
  236. table.string('contentType').notNullable()
  237. table.boolean('isBrowsable').notNullable().defaultTo(true)
  238. table.boolean('isSearchable').notNullable().defaultTo(true)
  239. table.specificType('isSearchableComputed', `boolean GENERATED ALWAYS AS ("publishState" != 'draft' AND "isSearchable") STORED`).index()
  240. table.string('password')
  241. table.integer('ratingScore').notNullable().defaultTo(0)
  242. table.integer('ratingCount').notNullable().defaultTo(0)
  243. table.jsonb('scripts').notNullable().defaultTo('{}')
  244. table.jsonb('historyData').notNullable().defaultTo('{}')
  245. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  246. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  247. })
  248. // RENDERERS ---------------------------
  249. .createTable('renderers', table => {
  250. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  251. table.string('module').notNullable()
  252. table.boolean('isEnabled').notNullable().defaultTo(false)
  253. table.jsonb('config').notNullable().defaultTo('{}')
  254. })
  255. // SETTINGS ----------------------------
  256. .createTable('settings', table => {
  257. table.string('key').notNullable().primary()
  258. table.jsonb('value')
  259. })
  260. // SITES -------------------------------
  261. .createTable('sites', table => {
  262. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  263. table.string('hostname').notNullable()
  264. table.boolean('isEnabled').notNullable().defaultTo(false)
  265. table.jsonb('config').notNullable()
  266. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  267. })
  268. // STORAGE -----------------------------
  269. .createTable('storage', table => {
  270. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  271. table.string('module').notNullable()
  272. table.boolean('isEnabled').notNullable().defaultTo(false)
  273. table.jsonb('contentTypes')
  274. table.jsonb('assetDelivery')
  275. table.jsonb('versioning')
  276. table.jsonb('schedule')
  277. table.jsonb('config')
  278. table.jsonb('state')
  279. })
  280. // TAGS --------------------------------
  281. .createTable('tags', table => {
  282. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  283. table.string('tag').notNullable()
  284. table.integer('usageCount').notNullable().defaultTo(0)
  285. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  286. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  287. })
  288. // TREE --------------------------------
  289. .createTable('tree', table => {
  290. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  291. table.specificType('folderPath', 'ltree').index().index('tree_folderpath_gist_index', { indexType: 'GIST' })
  292. table.string('fileName').notNullable().index()
  293. table.string('hash').notNullable().index()
  294. table.enu('type', ['folder', 'page', 'asset']).notNullable().index()
  295. table.string('locale', 10).notNullable().defaultTo('en').index()
  296. table.string('title').notNullable()
  297. table.enum('navigationMode', ['inherit', 'override', 'overrideExact', 'hide', 'hideExact']).notNullable().defaultTo('inherit').index()
  298. table.uuid('navigationId').index()
  299. table.jsonb('meta').notNullable().defaultTo('{}')
  300. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  301. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  302. })
  303. // USER AVATARS ------------------------
  304. .createTable('userAvatars', table => {
  305. table.uuid('id').notNullable().primary()
  306. table.binary('data').notNullable()
  307. })
  308. // USER EDITOR SETTINGS ----------------
  309. .createTable('userEditorSettings', table => {
  310. table.uuid('id').notNullable()
  311. table.string('editor').notNullable()
  312. table.jsonb('config').notNullable().defaultTo('{}')
  313. table.primary(['id', 'editor'])
  314. })
  315. // USER KEYS ---------------------------
  316. .createTable('userKeys', table => {
  317. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  318. table.string('kind').notNullable()
  319. table.string('token').notNullable()
  320. table.jsonb('meta').notNullable().defaultTo('{}')
  321. table.timestamp('validUntil').notNullable()
  322. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  323. })
  324. // USERS -------------------------------
  325. .createTable('users', table => {
  326. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  327. table.string('email').notNullable()
  328. table.string('name').notNullable()
  329. table.jsonb('auth').notNullable().defaultTo('{}')
  330. table.jsonb('meta').notNullable().defaultTo('{}')
  331. table.jsonb('prefs').notNullable().defaultTo('{}')
  332. table.boolean('hasAvatar').notNullable().defaultTo(false)
  333. table.boolean('isSystem').notNullable().defaultTo(false)
  334. table.boolean('isActive').notNullable().defaultTo(false)
  335. table.boolean('isVerified').notNullable().defaultTo(false)
  336. table.timestamp('lastLoginAt').index()
  337. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  338. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  339. })
  340. // =====================================
  341. // RELATION TABLES
  342. // =====================================
  343. // USER GROUPS -------------------------
  344. .createTable('userGroups', table => {
  345. table.increments('id').primary()
  346. table.uuid('userId').references('id').inTable('users').onDelete('CASCADE')
  347. table.uuid('groupId').references('id').inTable('groups').onDelete('CASCADE')
  348. })
  349. // =====================================
  350. // REFERENCES
  351. // =====================================
  352. .table('activityLogs', table => {
  353. table.uuid('userId').notNullable().references('id').inTable('users')
  354. })
  355. .table('analytics', table => {
  356. table.uuid('siteId').notNullable().references('id').inTable('sites')
  357. })
  358. .table('assets', table => {
  359. table.uuid('authorId').notNullable().references('id').inTable('users')
  360. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  361. })
  362. .table('commentProviders', table => {
  363. table.uuid('siteId').notNullable().references('id').inTable('sites')
  364. })
  365. .table('comments', table => {
  366. table.uuid('pageId').notNullable().references('id').inTable('pages').index()
  367. table.uuid('authorId').notNullable().references('id').inTable('users').index()
  368. })
  369. .table('navigation', table => {
  370. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  371. })
  372. .table('pageHistory', table => {
  373. table.uuid('authorId').notNullable().references('id').inTable('users')
  374. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  375. })
  376. .table('pageLinks', table => {
  377. table.uuid('pageId').notNullable().references('id').inTable('pages').onDelete('CASCADE')
  378. table.index(['path', 'locale'])
  379. })
  380. .table('pages', table => {
  381. table.uuid('authorId').notNullable().references('id').inTable('users').index()
  382. table.uuid('creatorId').notNullable().references('id').inTable('users').index()
  383. table.uuid('ownerId').notNullable().references('id').inTable('users').index()
  384. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  385. })
  386. .table('storage', table => {
  387. table.uuid('siteId').notNullable().references('id').inTable('sites')
  388. })
  389. .table('tags', table => {
  390. table.uuid('siteId').notNullable().references('id').inTable('sites')
  391. table.unique(['siteId', 'tag'])
  392. })
  393. .table('tree', table => {
  394. table.uuid('siteId').notNullable().references('id').inTable('sites')
  395. })
  396. .table('userKeys', table => {
  397. table.uuid('userId').notNullable().references('id').inTable('users')
  398. })
  399. // =====================================
  400. // TS WORD SUGGESTION TABLE
  401. // =====================================
  402. .createTable('autocomplete', table => {
  403. table.text('word')
  404. })
  405. .raw(`CREATE INDEX "autocomplete_idx" ON "autocomplete" USING GIN (word gin_trgm_ops)`)
  406. // =====================================
  407. // DEFAULT DATA
  408. // =====================================
  409. // -> GENERATE IDS
  410. const groupAdminId = uuid()
  411. const groupGuestId = '10000000-0000-4000-8000-000000000001'
  412. const siteId = uuid()
  413. const authModuleId = uuid()
  414. const userAdminId = uuid()
  415. const userGuestId = uuid()
  416. // -> SYSTEM CONFIG
  417. WIKI.logger.info('Generating certificates...')
  418. const secret = crypto.randomBytes(32).toString('hex')
  419. const certs = crypto.generateKeyPairSync('rsa', {
  420. modulusLength: 2048,
  421. publicKeyEncoding: {
  422. type: 'pkcs1',
  423. format: 'pem'
  424. },
  425. privateKeyEncoding: {
  426. type: 'pkcs1',
  427. format: 'pem',
  428. cipher: 'aes-256-cbc',
  429. passphrase: secret
  430. }
  431. })
  432. await knex('settings').insert([
  433. {
  434. key: 'auth',
  435. value: {
  436. audience: 'urn:wiki.js',
  437. tokenExpiration: '30m',
  438. tokenRenewal: '14d',
  439. certs: {
  440. jwk: pem2jwk(certs.publicKey),
  441. public: certs.publicKey,
  442. private: certs.privateKey
  443. },
  444. secret,
  445. rootAdminUserId: userAdminId,
  446. guestUserId: userGuestId
  447. }
  448. },
  449. {
  450. key: 'flags',
  451. value: {
  452. experimental: false,
  453. authDebug: false,
  454. sqlLog: false
  455. }
  456. },
  457. {
  458. key: 'icons',
  459. value: {
  460. fa: {
  461. isActive: true,
  462. config: {
  463. version: 6,
  464. license: 'free',
  465. token: ''
  466. }
  467. },
  468. la: {
  469. isActive: true
  470. }
  471. }
  472. },
  473. {
  474. key: 'mail',
  475. value: {
  476. senderName: '',
  477. senderEmail: '',
  478. host: '',
  479. port: 465,
  480. name: '',
  481. secure: true,
  482. verifySSL: true,
  483. user: '',
  484. pass: '',
  485. useDKIM: false,
  486. dkimDomainName: '',
  487. dkimKeySelector: '',
  488. dkimPrivateKey: ''
  489. }
  490. },
  491. {
  492. key: 'search',
  493. value: {
  494. termHighlighting: true,
  495. dictOverrides: {}
  496. }
  497. },
  498. {
  499. key: 'security',
  500. value: {
  501. corsConfig: '',
  502. corsMode: 'OFF',
  503. cspDirectives: '',
  504. disallowFloc: true,
  505. disallowIframe: true,
  506. disallowOpenRedirect: true,
  507. enforceCsp: false,
  508. enforceHsts: false,
  509. enforceSameOriginReferrerPolicy: true,
  510. forceAssetDownload: true,
  511. hstsDuration: 0,
  512. trustProxy: false,
  513. authJwtAudience: 'urn:wiki.js',
  514. authJwtExpiration: '30m',
  515. authJwtRenewablePeriod: '14d',
  516. uploadMaxFileSize: 10485760,
  517. uploadMaxFiles: 20,
  518. uploadScanSVG: true
  519. }
  520. },
  521. {
  522. key: 'update',
  523. value: {
  524. lastCheckedAt: null,
  525. version: WIKI.version,
  526. versionDate: WIKI.releaseDate
  527. }
  528. },
  529. {
  530. key: 'userDefaults',
  531. value: {
  532. timezone: 'America/New_York',
  533. dateFormat: 'YYYY-MM-DD',
  534. timeFormat: '12h'
  535. }
  536. }
  537. ])
  538. // -> DEFAULT SITE
  539. await knex('sites').insert({
  540. id: siteId,
  541. hostname: '*',
  542. isEnabled: true,
  543. config: {
  544. title: 'My Wiki Site',
  545. description: '',
  546. company: '',
  547. contentLicense: '',
  548. footerExtra: '',
  549. pageExtensions: ['md', 'html', 'txt'],
  550. pageCasing: true,
  551. discoverable: false,
  552. defaults: {
  553. tocDepth: {
  554. min: 1,
  555. max: 2
  556. }
  557. },
  558. features: {
  559. browse: true,
  560. ratings: false,
  561. ratingsMode: 'off',
  562. comments: false,
  563. contributions: false,
  564. profile: true,
  565. reasonForChange: 'required',
  566. search: true
  567. },
  568. logoText: true,
  569. sitemap: true,
  570. robots: {
  571. index: true,
  572. follow: true
  573. },
  574. authStrategies: [{ id: authModuleId, order: 0, isVisible: true }],
  575. locales: {
  576. primary: 'en',
  577. active: ['en']
  578. },
  579. assets: {
  580. logo: false,
  581. logoExt: 'svg',
  582. favicon: false,
  583. faviconExt: 'svg',
  584. loginBg: false
  585. },
  586. editors: {
  587. asciidoc: {
  588. isActive: true,
  589. config: {}
  590. },
  591. markdown: {
  592. isActive: true,
  593. config: {
  594. allowHTML: true,
  595. kroki: false,
  596. krokiServerUrl: 'https://kroki.io',
  597. latexEngine: 'katex',
  598. lineBreaks: true,
  599. linkify: true,
  600. multimdTable: true,
  601. plantuml: false,
  602. plantumlServerUrl: 'https://www.plantuml.com/plantuml/',
  603. quotes: 'english',
  604. tabWidth: 2,
  605. typographer: false,
  606. underline: true
  607. }
  608. },
  609. wysiwyg: {
  610. isActive: true,
  611. config: {}
  612. }
  613. },
  614. theme: {
  615. dark: false,
  616. codeBlocksTheme: 'github-dark',
  617. colorPrimary: '#1976D2',
  618. colorSecondary: '#02C39A',
  619. colorAccent: '#FF9800',
  620. colorHeader: '#000000',
  621. colorSidebar: '#1976D2',
  622. injectCSS: '',
  623. injectHead: '',
  624. injectBody: '',
  625. contentWidth: 'full',
  626. sidebarPosition: 'left',
  627. tocPosition: 'right',
  628. showSharingMenu: true,
  629. showPrintBtn: true,
  630. baseFont: 'roboto',
  631. contentFont: 'roboto'
  632. },
  633. uploads: {
  634. conflictBehavior: 'overwrite',
  635. normalizeFilename: true
  636. }
  637. }
  638. })
  639. // -> DEFAULT GROUPS
  640. await knex('groups').insert([
  641. {
  642. id: groupAdminId,
  643. name: 'Administrators',
  644. permissions: JSON.stringify(['manage:system']),
  645. rules: JSON.stringify([]),
  646. isSystem: true
  647. },
  648. {
  649. id: groupGuestId,
  650. name: 'Guests',
  651. permissions: JSON.stringify(['read:pages', 'read:assets', 'read:comments']),
  652. rules: JSON.stringify([
  653. {
  654. id: uuid(),
  655. name: 'Default Rule',
  656. roles: ['read:pages', 'read:assets', 'read:comments'],
  657. match: 'START',
  658. mode: 'DENY',
  659. path: '',
  660. locales: [],
  661. sites: []
  662. }
  663. ]),
  664. isSystem: true
  665. }
  666. ])
  667. // -> AUTHENTICATION MODULE
  668. await knex('authentication').insert({
  669. id: authModuleId,
  670. module: 'local',
  671. isEnabled: true,
  672. displayName: 'Local Authentication'
  673. })
  674. // -> USERS
  675. await knex('users').insert([
  676. {
  677. id: userAdminId,
  678. email: process.env.ADMIN_EMAIL ?? 'admin@example.com',
  679. auth: {
  680. [authModuleId]: {
  681. password: await bcrypt.hash(process.env.ADMIN_PASS || '12345678', 12),
  682. mustChangePwd: false, // TODO: Revert to true (below) once change password flow is implemented
  683. // mustChangePwd: !process.env.ADMIN_PASS,
  684. restrictLogin: false,
  685. tfaRequired: false,
  686. tfaSecret: ''
  687. }
  688. },
  689. name: 'Administrator',
  690. isSystem: false,
  691. isActive: true,
  692. isVerified: true,
  693. meta: {
  694. location: '',
  695. jobTitle: '',
  696. pronouns: ''
  697. },
  698. prefs: {
  699. timezone: 'America/New_York',
  700. dateFormat: 'YYYY-MM-DD',
  701. timeFormat: '12h',
  702. appearance: 'site',
  703. cvd: 'none'
  704. }
  705. },
  706. {
  707. id: userGuestId,
  708. email: 'guest@example.com',
  709. auth: {},
  710. name: 'Guest',
  711. isSystem: true,
  712. isActive: true,
  713. isVerified: true,
  714. meta: {},
  715. prefs: {
  716. timezone: 'America/New_York',
  717. dateFormat: 'YYYY-MM-DD',
  718. timeFormat: '12h',
  719. appearance: 'site',
  720. cvd: 'none'
  721. }
  722. }
  723. ])
  724. await knex('userGroups').insert([
  725. {
  726. userId: userAdminId,
  727. groupId: groupAdminId
  728. },
  729. {
  730. userId: userGuestId,
  731. groupId: groupGuestId
  732. }
  733. ])
  734. // -> NAVIGATION
  735. await knex('navigation').insert({
  736. id: siteId,
  737. items: JSON.stringify([
  738. {
  739. id: uuid(),
  740. type: 'header',
  741. label: 'Sample Header',
  742. visibilityGroups: []
  743. },
  744. {
  745. id: uuid(),
  746. type: 'link',
  747. icon: 'mdi-file-document-outline',
  748. label: 'Sample Link 1',
  749. target: '/',
  750. openInNewWindow: false,
  751. visibilityGroups: [],
  752. children: []
  753. },
  754. {
  755. id: uuid(),
  756. type: 'link',
  757. icon: 'mdi-book-open-variant',
  758. label: 'Sample Link 2',
  759. target: '/',
  760. openInNewWindow: false,
  761. visibilityGroups: [],
  762. children: []
  763. },
  764. {
  765. id: uuid(),
  766. type: 'separator',
  767. visibilityGroups: []
  768. },
  769. {
  770. id: uuid(),
  771. type: 'link',
  772. icon: 'mdi-airballoon',
  773. label: 'Sample Link 3',
  774. target: '/',
  775. openInNewWindow: false,
  776. visibilityGroups: [],
  777. children: []
  778. }
  779. ]),
  780. siteId: siteId
  781. })
  782. // -> STORAGE MODULE
  783. await knex('storage').insert({
  784. module: 'db',
  785. siteId,
  786. isEnabled: true,
  787. contentTypes: {
  788. activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
  789. largeThreshold: '5MB'
  790. },
  791. assetDelivery: {
  792. streaming: true,
  793. directAccess: false
  794. },
  795. versioning: {
  796. enabled: false
  797. },
  798. state: {
  799. current: 'ok'
  800. }
  801. })
  802. // -> SCHEDULED JOBS
  803. await knex('jobSchedule').insert([
  804. {
  805. task: 'checkVersion',
  806. cron: '0 0 * * *',
  807. type: 'system'
  808. },
  809. {
  810. task: 'cleanJobHistory',
  811. cron: '5 0 * * *',
  812. type: 'system'
  813. },
  814. // {
  815. // task: 'refreshAutocomplete',
  816. // cron: '0 */6 * * *',
  817. // type: 'system'
  818. // },
  819. {
  820. task: 'updateLocales',
  821. cron: '0 0 * * *',
  822. type: 'system'
  823. }
  824. ])
  825. await knex('jobLock').insert({
  826. key: 'cron',
  827. lastCheckedBy: 'init',
  828. lastCheckedAt: DateTime.utc().minus({ hours: 1 }).toISO()
  829. })
  830. WIKI.logger.info('Completed 3.0.0 database migration.')
  831. }
  832. export function down (knex) { }