2.0.0.js 9.6 KB

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