2
0

2.0.0-beta.1.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. exports.up = knex => {
  2. const dbCompat = {
  3. charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`)
  4. }
  5. return knex.schema
  6. // =====================================
  7. // MODEL TABLES
  8. // =====================================
  9. // ASSETS ------------------------------
  10. .createTable('assets', table => {
  11. if (dbCompat.charset) { table.charset('utf8mb4') }
  12. table.increments('id').primary()
  13. table.string('filename').notNullable()
  14. table.string('basename').notNullable()
  15. table.string('ext').notNullable()
  16. table.enum('kind', ['binary', 'image']).notNullable().defaultTo('binary')
  17. table.string('mime').notNullable().defaultTo('application/octet-stream')
  18. table.integer('fileSize').unsigned().comment('In kilobytes')
  19. table.json('metadata')
  20. table.string('createdAt').notNullable()
  21. table.string('updatedAt').notNullable()
  22. })
  23. // ASSET FOLDERS -----------------------
  24. .createTable('assetFolders', table => {
  25. if (dbCompat.charset) { table.charset('utf8mb4') }
  26. table.increments('id').primary()
  27. table.string('name').notNullable()
  28. table.string('slug').notNullable()
  29. table.integer('parentId').unsigned().references('id').inTable('assetFolders')
  30. })
  31. // AUTHENTICATION ----------------------
  32. .createTable('authentication', table => {
  33. if (dbCompat.charset) { table.charset('utf8mb4') }
  34. table.string('key').notNullable().primary()
  35. table.boolean('isEnabled').notNullable().defaultTo(false)
  36. table.json('config').notNullable()
  37. table.boolean('selfRegistration').notNullable().defaultTo(false)
  38. table.json('domainWhitelist').notNullable()
  39. table.json('autoEnrollGroups').notNullable()
  40. })
  41. // COMMENTS ----------------------------
  42. .createTable('comments', table => {
  43. if (dbCompat.charset) { table.charset('utf8mb4') }
  44. table.increments('id').primary()
  45. table.text('content').notNullable()
  46. table.string('createdAt').notNullable()
  47. table.string('updatedAt').notNullable()
  48. })
  49. // EDITORS -----------------------------
  50. .createTable('editors', table => {
  51. if (dbCompat.charset) { table.charset('utf8mb4') }
  52. table.string('key').notNullable().primary()
  53. table.boolean('isEnabled').notNullable().defaultTo(false)
  54. table.json('config').notNullable()
  55. })
  56. // GROUPS ------------------------------
  57. .createTable('groups', table => {
  58. if (dbCompat.charset) { table.charset('utf8mb4') }
  59. table.increments('id').primary()
  60. table.string('name').notNullable()
  61. table.json('permissions').notNullable()
  62. table.json('pageRules').notNullable()
  63. table.boolean('isSystem').notNullable().defaultTo(false)
  64. table.string('createdAt').notNullable()
  65. table.string('updatedAt').notNullable()
  66. })
  67. // LOCALES -----------------------------
  68. .createTable('locales', table => {
  69. if (dbCompat.charset) { table.charset('utf8mb4') }
  70. table.string('code', 2).notNullable().primary()
  71. table.json('strings')
  72. table.boolean('isRTL').notNullable().defaultTo(false)
  73. table.string('name').notNullable()
  74. table.string('nativeName').notNullable()
  75. table.string('createdAt').notNullable()
  76. table.string('updatedAt').notNullable()
  77. })
  78. // LOGGING ----------------------------
  79. .createTable('loggers', table => {
  80. if (dbCompat.charset) { table.charset('utf8mb4') }
  81. table.string('key').notNullable().primary()
  82. table.boolean('isEnabled').notNullable().defaultTo(false)
  83. table.string('level').notNullable().defaultTo('warn')
  84. table.json('config')
  85. })
  86. // NAVIGATION ----------------------------
  87. .createTable('navigation', table => {
  88. if (dbCompat.charset) { table.charset('utf8mb4') }
  89. table.string('key').notNullable().primary()
  90. table.json('config')
  91. })
  92. // PAGE HISTORY ------------------------
  93. .createTable('pageHistory', table => {
  94. if (dbCompat.charset) { table.charset('utf8mb4') }
  95. table.increments('id').primary()
  96. table.string('path').notNullable()
  97. table.string('hash').notNullable()
  98. table.string('title').notNullable()
  99. table.string('description')
  100. table.boolean('isPrivate').notNullable().defaultTo(false)
  101. table.boolean('isPublished').notNullable().defaultTo(false)
  102. table.string('publishStartDate')
  103. table.string('publishEndDate')
  104. table.text('content')
  105. table.string('contentType').notNullable()
  106. table.string('createdAt').notNullable()
  107. })
  108. // PAGES -------------------------------
  109. .createTable('pages', table => {
  110. if (dbCompat.charset) { table.charset('utf8mb4') }
  111. table.increments('id').primary()
  112. table.string('path').notNullable()
  113. table.string('hash').notNullable()
  114. table.string('title').notNullable()
  115. table.string('description')
  116. table.boolean('isPrivate').notNullable().defaultTo(false)
  117. table.boolean('isPublished').notNullable().defaultTo(false)
  118. table.string('privateNS')
  119. table.string('publishStartDate')
  120. table.string('publishEndDate')
  121. table.text('content')
  122. table.text('render')
  123. table.json('toc')
  124. table.string('contentType').notNullable()
  125. table.string('createdAt').notNullable()
  126. table.string('updatedAt').notNullable()
  127. })
  128. // PAGE TREE ---------------------------
  129. .createTable('pageTree', table => {
  130. if (dbCompat.charset) { table.charset('utf8mb4') }
  131. table.increments('id').primary()
  132. table.string('path').notNullable()
  133. table.integer('depth').unsigned().notNullable()
  134. table.string('title').notNullable()
  135. table.boolean('isPrivate').notNullable().defaultTo(false)
  136. table.boolean('isFolder').notNullable().defaultTo(false)
  137. table.string('privateNS')
  138. })
  139. // RENDERERS ---------------------------
  140. .createTable('renderers', table => {
  141. if (dbCompat.charset) { table.charset('utf8mb4') }
  142. table.string('key').notNullable().primary()
  143. table.boolean('isEnabled').notNullable().defaultTo(false)
  144. table.json('config')
  145. })
  146. // SEARCH ------------------------------
  147. .createTable('searchEngines', table => {
  148. if (dbCompat.charset) { table.charset('utf8mb4') }
  149. table.string('key').notNullable().primary()
  150. table.boolean('isEnabled').notNullable().defaultTo(false)
  151. table.json('config')
  152. })
  153. // SETTINGS ----------------------------
  154. .createTable('settings', table => {
  155. if (dbCompat.charset) { table.charset('utf8mb4') }
  156. table.string('key').notNullable().primary()
  157. table.json('value')
  158. table.string('updatedAt').notNullable()
  159. })
  160. // STORAGE -----------------------------
  161. .createTable('storage', table => {
  162. if (dbCompat.charset) { table.charset('utf8mb4') }
  163. table.string('key').notNullable().primary()
  164. table.boolean('isEnabled').notNullable().defaultTo(false)
  165. table.string('mode', ['sync', 'push', 'pull']).notNullable().defaultTo('push')
  166. table.json('config')
  167. })
  168. // TAGS --------------------------------
  169. .createTable('tags', table => {
  170. if (dbCompat.charset) { table.charset('utf8mb4') }
  171. table.increments('id').primary()
  172. table.string('tag').notNullable().unique()
  173. table.string('title')
  174. table.string('createdAt').notNullable()
  175. table.string('updatedAt').notNullable()
  176. })
  177. // USER KEYS ---------------------------
  178. .createTable('userKeys', table => {
  179. if (dbCompat.charset) { table.charset('utf8mb4') }
  180. table.increments('id').primary()
  181. table.string('kind').notNullable()
  182. table.string('token').notNullable()
  183. table.string('createdAt').notNullable()
  184. table.string('validUntil').notNullable()
  185. })
  186. // USERS -------------------------------
  187. .createTable('users', table => {
  188. if (dbCompat.charset) { table.charset('utf8mb4') }
  189. table.increments('id').primary()
  190. table.string('email').notNullable()
  191. table.string('name').notNullable()
  192. table.string('providerId')
  193. table.string('password')
  194. table.boolean('tfaIsActive').notNullable().defaultTo(false)
  195. table.string('tfaSecret')
  196. table.string('jobTitle').defaultTo('')
  197. table.string('location').defaultTo('')
  198. table.string('pictureUrl')
  199. table.string('timezone').notNullable().defaultTo('America/New_York')
  200. table.boolean('isSystem').notNullable().defaultTo(false)
  201. table.boolean('isActive').notNullable().defaultTo(false)
  202. table.boolean('isVerified').notNullable().defaultTo(false)
  203. table.string('createdAt').notNullable()
  204. table.string('updatedAt').notNullable()
  205. })
  206. // =====================================
  207. // RELATION TABLES
  208. // =====================================
  209. // PAGE HISTORY TAGS ---------------------------
  210. .createTable('pageHistoryTags', table => {
  211. if (dbCompat.charset) { table.charset('utf8mb4') }
  212. table.increments('id').primary()
  213. table.integer('pageId').unsigned().references('id').inTable('pageHistory').onDelete('CASCADE')
  214. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  215. })
  216. // PAGE TAGS ---------------------------
  217. .createTable('pageTags', table => {
  218. if (dbCompat.charset) { table.charset('utf8mb4') }
  219. table.increments('id').primary()
  220. table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
  221. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  222. })
  223. // USER GROUPS -------------------------
  224. .createTable('userGroups', table => {
  225. if (dbCompat.charset) { table.charset('utf8mb4') }
  226. table.increments('id').primary()
  227. table.integer('userId').unsigned().references('id').inTable('users').onDelete('CASCADE')
  228. table.integer('groupId').unsigned().references('id').inTable('groups').onDelete('CASCADE')
  229. })
  230. // =====================================
  231. // REFERENCES
  232. // =====================================
  233. .table('assets', table => {
  234. table.integer('folderId').unsigned().references('id').inTable('assetFolders')
  235. table.integer('authorId').unsigned().references('id').inTable('users')
  236. })
  237. .table('comments', table => {
  238. table.integer('pageId').unsigned().references('id').inTable('pages')
  239. table.integer('authorId').unsigned().references('id').inTable('users')
  240. })
  241. .table('pageHistory', table => {
  242. table.integer('pageId').unsigned().references('id').inTable('pages')
  243. table.string('editorKey').references('key').inTable('editors')
  244. table.string('localeCode', 2).references('code').inTable('locales')
  245. table.integer('authorId').unsigned().references('id').inTable('users')
  246. })
  247. .table('pages', table => {
  248. table.string('editorKey').references('key').inTable('editors')
  249. table.string('localeCode', 2).references('code').inTable('locales')
  250. table.integer('authorId').unsigned().references('id').inTable('users')
  251. table.integer('creatorId').unsigned().references('id').inTable('users')
  252. })
  253. .table('pageTree', table => {
  254. table.integer('parent').unsigned().references('id').inTable('pageTree')
  255. table.integer('pageId').unsigned().references('id').inTable('pages')
  256. table.string('localeCode', 2).references('code').inTable('locales')
  257. })
  258. .table('userKeys', table => {
  259. table.integer('userId').unsigned().references('id').inTable('users')
  260. })
  261. .table('users', table => {
  262. table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
  263. table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
  264. table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
  265. table.unique(['providerKey', 'email'])
  266. })
  267. }
  268. exports.down = knex => {
  269. return knex.schema
  270. .dropTableIfExists('userGroups')
  271. .dropTableIfExists('pageHistoryTags')
  272. .dropTableIfExists('pageHistory')
  273. .dropTableIfExists('pageTags')
  274. .dropTableIfExists('assets')
  275. .dropTableIfExists('assetFolders')
  276. .dropTableIfExists('comments')
  277. .dropTableIfExists('editors')
  278. .dropTableIfExists('groups')
  279. .dropTableIfExists('locales')
  280. .dropTableIfExists('navigation')
  281. .dropTableIfExists('pages')
  282. .dropTableIfExists('renderers')
  283. .dropTableIfExists('settings')
  284. .dropTableIfExists('storage')
  285. .dropTableIfExists('tags')
  286. .dropTableIfExists('userKeys')
  287. .dropTableIfExists('users')
  288. }