2.0.0.js 10.0 KB

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