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.string('name').notNullable()
  180. table.jsonb('items').notNullable().defaultTo('[]')
  181. })
  182. // PAGE HISTORY ------------------------
  183. .createTable('pageHistory', table => {
  184. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  185. table.uuid('pageId').notNullable().index()
  186. table.string('action').defaultTo('updated')
  187. table.jsonb('affectedFields').notNullable().defaultTo('[]')
  188. table.string('locale', 10).notNullable().defaultTo('en')
  189. table.string('path').notNullable()
  190. table.string('hash').notNullable()
  191. table.string('alias')
  192. table.string('title').notNullable()
  193. table.string('description')
  194. table.string('icon')
  195. table.enu('publishState', ['draft', 'published', 'scheduled']).notNullable().defaultTo('draft')
  196. table.timestamp('publishStartDate')
  197. table.timestamp('publishEndDate')
  198. table.jsonb('config').notNullable().defaultTo('{}')
  199. table.jsonb('relations').notNullable().defaultTo('[]')
  200. table.text('content')
  201. table.text('render')
  202. table.jsonb('toc')
  203. table.string('editor').notNullable()
  204. table.string('contentType').notNullable()
  205. table.jsonb('scripts').notNullable().defaultTo('{}')
  206. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  207. table.timestamp('versionDate').notNullable().defaultTo(knex.fn.now())
  208. })
  209. // PAGE LINKS --------------------------
  210. .createTable('pageLinks', table => {
  211. table.increments('id').primary()
  212. table.string('path').notNullable()
  213. table.string('locale', 10).notNullable()
  214. })
  215. // PAGES -------------------------------
  216. .createTable('pages', table => {
  217. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  218. table.string('locale', 10).notNullable()
  219. table.string('path').notNullable()
  220. table.string('hash').notNullable()
  221. table.string('alias')
  222. table.string('title').notNullable()
  223. table.string('description')
  224. table.string('icon')
  225. table.enu('publishState', ['draft', 'published', 'scheduled']).notNullable().defaultTo('draft')
  226. table.timestamp('publishStartDate')
  227. table.timestamp('publishEndDate')
  228. table.jsonb('config').notNullable().defaultTo('{}')
  229. table.jsonb('relations').notNullable().defaultTo('[]')
  230. table.text('content')
  231. table.text('render')
  232. table.text('searchContent')
  233. table.specificType('ts', 'tsvector').index('ts_idx', { indexType: 'GIN' })
  234. table.specificType('tags', 'text[]').index('tags_idx', { indexType: 'GIN' })
  235. table.jsonb('toc')
  236. table.string('editor').notNullable()
  237. table.string('contentType').notNullable()
  238. table.boolean('isBrowsable').notNullable().defaultTo(true)
  239. table.boolean('isSearchable').notNullable().defaultTo(true)
  240. table.specificType('isSearchableComputed', `boolean GENERATED ALWAYS AS ("publishState" != 'draft' AND "isSearchable") STORED`).index()
  241. table.string('password')
  242. table.integer('ratingScore').notNullable().defaultTo(0)
  243. table.integer('ratingCount').notNullable().defaultTo(0)
  244. table.jsonb('scripts').notNullable().defaultTo('{}')
  245. table.jsonb('historyData').notNullable().defaultTo('{}')
  246. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  247. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  248. })
  249. // RENDERERS ---------------------------
  250. .createTable('renderers', table => {
  251. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  252. table.string('module').notNullable()
  253. table.boolean('isEnabled').notNullable().defaultTo(false)
  254. table.jsonb('config').notNullable().defaultTo('{}')
  255. })
  256. // SETTINGS ----------------------------
  257. .createTable('settings', table => {
  258. table.string('key').notNullable().primary()
  259. table.jsonb('value')
  260. })
  261. // SITES -------------------------------
  262. .createTable('sites', table => {
  263. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  264. table.string('hostname').notNullable()
  265. table.boolean('isEnabled').notNullable().defaultTo(false)
  266. table.jsonb('config').notNullable()
  267. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  268. })
  269. // STORAGE -----------------------------
  270. .createTable('storage', table => {
  271. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  272. table.string('module').notNullable()
  273. table.boolean('isEnabled').notNullable().defaultTo(false)
  274. table.jsonb('contentTypes')
  275. table.jsonb('assetDelivery')
  276. table.jsonb('versioning')
  277. table.jsonb('schedule')
  278. table.jsonb('config')
  279. table.jsonb('state')
  280. })
  281. // TAGS --------------------------------
  282. .createTable('tags', table => {
  283. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  284. table.string('tag').notNullable()
  285. table.integer('usageCount').notNullable().defaultTo(0)
  286. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  287. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  288. })
  289. // TREE --------------------------------
  290. .createTable('tree', table => {
  291. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  292. table.specificType('folderPath', 'ltree').index().index('tree_folderpath_gist_index', { indexType: 'GIST' })
  293. table.string('fileName').notNullable().index()
  294. table.string('hash').notNullable().index()
  295. table.enu('type', ['folder', 'page', 'asset']).notNullable().index()
  296. table.string('locale', 10).notNullable().defaultTo('en').index()
  297. table.string('title').notNullable()
  298. table.jsonb('meta').notNullable().defaultTo('{}')
  299. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  300. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  301. })
  302. // USER AVATARS ------------------------
  303. .createTable('userAvatars', table => {
  304. table.uuid('id').notNullable().primary()
  305. table.binary('data').notNullable()
  306. })
  307. // USER EDITOR SETTINGS ----------------
  308. .createTable('userEditorSettings', table => {
  309. table.uuid('id').notNullable()
  310. table.string('editor').notNullable()
  311. table.jsonb('config').notNullable().defaultTo('{}')
  312. table.primary(['id', 'editor'])
  313. })
  314. // USER KEYS ---------------------------
  315. .createTable('userKeys', table => {
  316. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  317. table.string('kind').notNullable()
  318. table.string('token').notNullable()
  319. table.jsonb('meta').notNullable().defaultTo('{}')
  320. table.timestamp('validUntil').notNullable()
  321. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  322. })
  323. // USERS -------------------------------
  324. .createTable('users', table => {
  325. table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
  326. table.string('email').notNullable()
  327. table.string('name').notNullable()
  328. table.jsonb('auth').notNullable().defaultTo('{}')
  329. table.jsonb('meta').notNullable().defaultTo('{}')
  330. table.jsonb('prefs').notNullable().defaultTo('{}')
  331. table.boolean('hasAvatar').notNullable().defaultTo(false)
  332. table.boolean('isSystem').notNullable().defaultTo(false)
  333. table.boolean('isActive').notNullable().defaultTo(false)
  334. table.boolean('isVerified').notNullable().defaultTo(false)
  335. table.timestamp('lastLoginAt').index()
  336. table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
  337. table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
  338. })
  339. // =====================================
  340. // RELATION TABLES
  341. // =====================================
  342. // USER GROUPS -------------------------
  343. .createTable('userGroups', table => {
  344. table.increments('id').primary()
  345. table.uuid('userId').references('id').inTable('users').onDelete('CASCADE')
  346. table.uuid('groupId').references('id').inTable('groups').onDelete('CASCADE')
  347. })
  348. // =====================================
  349. // REFERENCES
  350. // =====================================
  351. .table('activityLogs', table => {
  352. table.uuid('userId').notNullable().references('id').inTable('users')
  353. })
  354. .table('analytics', table => {
  355. table.uuid('siteId').notNullable().references('id').inTable('sites')
  356. })
  357. .table('assets', table => {
  358. table.uuid('authorId').notNullable().references('id').inTable('users')
  359. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  360. })
  361. .table('commentProviders', table => {
  362. table.uuid('siteId').notNullable().references('id').inTable('sites')
  363. })
  364. .table('comments', table => {
  365. table.uuid('pageId').notNullable().references('id').inTable('pages').index()
  366. table.uuid('authorId').notNullable().references('id').inTable('users').index()
  367. })
  368. .table('navigation', table => {
  369. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  370. })
  371. .table('pageHistory', table => {
  372. table.uuid('authorId').notNullable().references('id').inTable('users')
  373. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  374. })
  375. .table('pageLinks', table => {
  376. table.uuid('pageId').notNullable().references('id').inTable('pages').onDelete('CASCADE')
  377. table.index(['path', 'locale'])
  378. })
  379. .table('pages', table => {
  380. table.uuid('authorId').notNullable().references('id').inTable('users').index()
  381. table.uuid('creatorId').notNullable().references('id').inTable('users').index()
  382. table.uuid('ownerId').notNullable().references('id').inTable('users').index()
  383. table.uuid('siteId').notNullable().references('id').inTable('sites').index()
  384. })
  385. .table('storage', table => {
  386. table.uuid('siteId').notNullable().references('id').inTable('sites')
  387. })
  388. .table('tags', table => {
  389. table.uuid('siteId').notNullable().references('id').inTable('sites')
  390. table.unique(['siteId', 'tag'])
  391. })
  392. .table('tree', table => {
  393. table.uuid('navigationId').references('id').inTable('navigation').index()
  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 navDefaultId = uuid()
  413. const siteId = uuid()
  414. const authModuleId = uuid()
  415. const userAdminId = uuid()
  416. const userGuestId = uuid()
  417. // -> SYSTEM CONFIG
  418. WIKI.logger.info('Generating certificates...')
  419. const secret = crypto.randomBytes(32).toString('hex')
  420. const certs = crypto.generateKeyPairSync('rsa', {
  421. modulusLength: 2048,
  422. publicKeyEncoding: {
  423. type: 'pkcs1',
  424. format: 'pem'
  425. },
  426. privateKeyEncoding: {
  427. type: 'pkcs1',
  428. format: 'pem',
  429. cipher: 'aes-256-cbc',
  430. passphrase: secret
  431. }
  432. })
  433. await knex('settings').insert([
  434. {
  435. key: 'auth',
  436. value: {
  437. audience: 'urn:wiki.js',
  438. tokenExpiration: '30m',
  439. tokenRenewal: '14d',
  440. certs: {
  441. jwk: pem2jwk(certs.publicKey),
  442. public: certs.publicKey,
  443. private: certs.privateKey
  444. },
  445. secret,
  446. rootAdminUserId: userAdminId,
  447. guestUserId: userGuestId
  448. }
  449. },
  450. {
  451. key: 'flags',
  452. value: {
  453. experimental: false,
  454. authDebug: false,
  455. sqlLog: false
  456. }
  457. },
  458. {
  459. key: 'icons',
  460. value: {
  461. fa: {
  462. isActive: true,
  463. config: {
  464. version: 6,
  465. license: 'free',
  466. token: ''
  467. }
  468. },
  469. la: {
  470. isActive: true
  471. }
  472. }
  473. },
  474. {
  475. key: 'mail',
  476. value: {
  477. senderName: '',
  478. senderEmail: '',
  479. host: '',
  480. port: 465,
  481. name: '',
  482. secure: true,
  483. verifySSL: true,
  484. user: '',
  485. pass: '',
  486. useDKIM: false,
  487. dkimDomainName: '',
  488. dkimKeySelector: '',
  489. dkimPrivateKey: ''
  490. }
  491. },
  492. {
  493. key: 'search',
  494. value: {
  495. termHighlighting: true,
  496. dictOverrides: {}
  497. }
  498. },
  499. {
  500. key: 'security',
  501. value: {
  502. corsConfig: '',
  503. corsMode: 'OFF',
  504. cspDirectives: '',
  505. disallowFloc: true,
  506. disallowIframe: true,
  507. disallowOpenRedirect: true,
  508. enforceCsp: false,
  509. enforceHsts: false,
  510. enforceSameOriginReferrerPolicy: true,
  511. forceAssetDownload: true,
  512. hstsDuration: 0,
  513. trustProxy: false,
  514. authJwtAudience: 'urn:wiki.js',
  515. authJwtExpiration: '30m',
  516. authJwtRenewablePeriod: '14d',
  517. uploadMaxFileSize: 10485760,
  518. uploadMaxFiles: 20,
  519. uploadScanSVG: true
  520. }
  521. },
  522. {
  523. key: 'update',
  524. value: {
  525. lastCheckedAt: null,
  526. version: WIKI.version,
  527. versionDate: WIKI.releaseDate
  528. }
  529. },
  530. {
  531. key: 'userDefaults',
  532. value: {
  533. timezone: 'America/New_York',
  534. dateFormat: 'YYYY-MM-DD',
  535. timeFormat: '12h'
  536. }
  537. }
  538. ])
  539. // -> DEFAULT SITE
  540. await knex('sites').insert({
  541. id: siteId,
  542. hostname: '*',
  543. isEnabled: true,
  544. config: {
  545. title: 'My Wiki Site',
  546. description: '',
  547. company: '',
  548. contentLicense: '',
  549. footerExtra: '',
  550. pageExtensions: ['md', 'html', 'txt'],
  551. pageCasing: true,
  552. discoverable: false,
  553. defaults: {
  554. tocDepth: {
  555. min: 1,
  556. max: 2
  557. }
  558. },
  559. features: {
  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. nav: {
  615. mode: 'mixed',
  616. defaultId: navDefaultId,
  617. },
  618. theme: {
  619. dark: false,
  620. codeBlocksTheme: 'github-dark',
  621. colorPrimary: '#1976D2',
  622. colorSecondary: '#02C39A',
  623. colorAccent: '#FF9800',
  624. colorHeader: '#000000',
  625. colorSidebar: '#1976D2',
  626. injectCSS: '',
  627. injectHead: '',
  628. injectBody: '',
  629. contentWidth: 'full',
  630. sidebarPosition: 'left',
  631. tocPosition: 'right',
  632. showSharingMenu: true,
  633. showPrintBtn: true,
  634. baseFont: 'roboto',
  635. contentFont: 'roboto'
  636. },
  637. uploads: {
  638. conflictBehavior: 'overwrite',
  639. normalizeFilename: true
  640. }
  641. }
  642. })
  643. // -> DEFAULT GROUPS
  644. await knex('groups').insert([
  645. {
  646. id: groupAdminId,
  647. name: 'Administrators',
  648. permissions: JSON.stringify(['manage:system']),
  649. rules: JSON.stringify([]),
  650. isSystem: true
  651. },
  652. {
  653. id: groupGuestId,
  654. name: 'Guests',
  655. permissions: JSON.stringify(['read:pages', 'read:assets', 'read:comments']),
  656. rules: JSON.stringify([
  657. {
  658. id: uuid(),
  659. name: 'Default Rule',
  660. roles: ['read:pages', 'read:assets', 'read:comments'],
  661. match: 'START',
  662. mode: 'DENY',
  663. path: '',
  664. locales: [],
  665. sites: []
  666. }
  667. ]),
  668. isSystem: true
  669. }
  670. ])
  671. // -> AUTHENTICATION MODULE
  672. await knex('authentication').insert({
  673. id: authModuleId,
  674. module: 'local',
  675. isEnabled: true,
  676. displayName: 'Local Authentication'
  677. })
  678. // -> USERS
  679. await knex('users').insert([
  680. {
  681. id: userAdminId,
  682. email: process.env.ADMIN_EMAIL ?? 'admin@example.com',
  683. auth: {
  684. [authModuleId]: {
  685. password: await bcrypt.hash(process.env.ADMIN_PASS || '12345678', 12),
  686. mustChangePwd: false, // TODO: Revert to true (below) once change password flow is implemented
  687. // mustChangePwd: !process.env.ADMIN_PASS,
  688. restrictLogin: false,
  689. tfaRequired: false,
  690. tfaSecret: ''
  691. }
  692. },
  693. name: 'Administrator',
  694. isSystem: false,
  695. isActive: true,
  696. isVerified: true,
  697. meta: {
  698. location: '',
  699. jobTitle: '',
  700. pronouns: ''
  701. },
  702. prefs: {
  703. timezone: 'America/New_York',
  704. dateFormat: 'YYYY-MM-DD',
  705. timeFormat: '12h',
  706. appearance: 'site',
  707. cvd: 'none'
  708. }
  709. },
  710. {
  711. id: userGuestId,
  712. email: 'guest@example.com',
  713. auth: {},
  714. name: 'Guest',
  715. isSystem: true,
  716. isActive: true,
  717. isVerified: true,
  718. meta: {},
  719. prefs: {
  720. timezone: 'America/New_York',
  721. dateFormat: 'YYYY-MM-DD',
  722. timeFormat: '12h',
  723. appearance: 'site',
  724. cvd: 'none'
  725. }
  726. }
  727. ])
  728. await knex('userGroups').insert([
  729. {
  730. userId: userAdminId,
  731. groupId: groupAdminId
  732. },
  733. {
  734. userId: userGuestId,
  735. groupId: groupGuestId
  736. }
  737. ])
  738. // -> NAVIGATION
  739. await knex('navigation').insert({
  740. id: navDefaultId,
  741. name: 'Default',
  742. items: JSON.stringify([
  743. {
  744. id: uuid(),
  745. type: 'header',
  746. label: 'Sample Header'
  747. },
  748. {
  749. id: uuid(),
  750. type: 'link',
  751. icon: 'mdi-file-document-outline',
  752. label: 'Sample Link 1',
  753. target: '/',
  754. openInNewWindow: false,
  755. children: []
  756. },
  757. {
  758. id: uuid(),
  759. type: 'link',
  760. icon: 'mdi-book-open-variant',
  761. label: 'Sample Link 2',
  762. target: '/',
  763. openInNewWindow: false,
  764. children: []
  765. },
  766. {
  767. id: uuid(),
  768. type: 'separator',
  769. },
  770. {
  771. id: uuid(),
  772. type: 'link',
  773. icon: 'mdi-airballoon',
  774. label: 'Sample Link 3',
  775. target: '/',
  776. openInNewWindow: false,
  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) { }