2.0.0.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // PAGE HISTORY ------------------------
  69. .createTable('pageHistory', table => {
  70. table.increments('id').primary()
  71. table.string('path').notNullable()
  72. table.string('title').notNullable()
  73. table.string('description')
  74. table.boolean('isPrivate').notNullable().defaultTo(false)
  75. table.boolean('isPublished').notNullable().defaultTo(false)
  76. table.string('publishStartDate')
  77. table.string('publishEndDate')
  78. table.text('content')
  79. table.string('contentType').notNullable()
  80. table.string('createdAt').notNullable()
  81. })
  82. // PAGES -------------------------------
  83. .createTable('pages', table => {
  84. table.increments('id').primary()
  85. table.string('path').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('privateNS')
  91. table.string('publishStartDate')
  92. table.string('publishEndDate')
  93. table.text('content')
  94. table.text('render')
  95. table.string('contentType').notNullable()
  96. table.string('createdAt').notNullable()
  97. table.string('updatedAt').notNullable()
  98. })
  99. // RENDERERS ---------------------------
  100. .createTable('renderers', table => {
  101. table.increments('id').primary()
  102. table.string('key').notNullable().unique()
  103. table.boolean('isEnabled').notNullable().defaultTo(false)
  104. table.json('config')
  105. })
  106. // SETTINGS ----------------------------
  107. .createTable('settings', table => {
  108. table.increments('id').primary()
  109. table.string('key').notNullable().unique()
  110. table.json('value')
  111. table.string('updatedAt').notNullable()
  112. })
  113. // STORAGE -----------------------------
  114. .createTable('storage', table => {
  115. table.increments('id').primary()
  116. table.string('key').notNullable().unique()
  117. table.boolean('isEnabled').notNullable().defaultTo(false)
  118. table.string('mode', ['sync', 'push', 'pull']).notNullable().defaultTo('push')
  119. table.json('config')
  120. })
  121. // TAGS --------------------------------
  122. .createTable('tags', table => {
  123. table.increments('id').primary()
  124. table.string('tag').notNullable().unique()
  125. table.string('title')
  126. table.string('createdAt').notNullable()
  127. table.string('updatedAt').notNullable()
  128. })
  129. // USERS -------------------------------
  130. .createTable('users', table => {
  131. table.increments('id').primary()
  132. table.string('email').notNullable()
  133. table.string('name').notNullable()
  134. table.string('providerId')
  135. table.string('password')
  136. table.boolean('tfaIsActive').notNullable().defaultTo(false)
  137. table.string('tfaSecret')
  138. table.enum('role', ['admin', 'guest', 'user']).notNullable().defaultTo('guest')
  139. table.string('jobTitle').defaultTo('')
  140. table.string('location').defaultTo('')
  141. table.string('pictureUrl')
  142. table.string('timezone').notNullable().defaultTo('America/New_York')
  143. table.string('createdAt').notNullable()
  144. table.string('updatedAt').notNullable()
  145. })
  146. // =====================================
  147. // RELATION TABLES
  148. // =====================================
  149. // PAGE HISTORY TAGS ---------------------------
  150. .createTable('pageHistoryTags', table => {
  151. table.increments('id').primary()
  152. table.integer('pageId').unsigned().references('id').inTable('pageHistory').onDelete('CASCADE')
  153. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  154. })
  155. // PAGE TAGS ---------------------------
  156. .createTable('pageTags', table => {
  157. table.increments('id').primary()
  158. table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
  159. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  160. })
  161. // USER GROUPS -------------------------
  162. .createTable('userGroups', table => {
  163. table.increments('id').primary()
  164. table.integer('userId').unsigned().references('id').inTable('users').onDelete('CASCADE')
  165. table.integer('groupId').unsigned().references('id').inTable('groups').onDelete('CASCADE')
  166. })
  167. // =====================================
  168. // REFERENCES
  169. // =====================================
  170. .table('assets', table => {
  171. table.integer('folderId').unsigned().references('id').inTable('assetFolders')
  172. table.integer('authorId').unsigned().references('id').inTable('users')
  173. })
  174. .table('comments', table => {
  175. table.integer('pageId').unsigned().references('id').inTable('pages')
  176. table.integer('authorId').unsigned().references('id').inTable('users')
  177. })
  178. .table('pageHistory', table => {
  179. table.integer('pageId').unsigned().references('id').inTable('pages')
  180. table.string('editorKey').references('key').inTable('editors')
  181. table.string('localeCode', 2).references('code').inTable('locales')
  182. table.integer('authorId').unsigned().references('id').inTable('users')
  183. })
  184. .table('pages', table => {
  185. table.string('editorKey').references('key').inTable('editors')
  186. table.string('localeCode', 2).references('code').inTable('locales')
  187. table.integer('authorId').unsigned().references('id').inTable('users')
  188. table.integer('creatorId').unsigned().references('id').inTable('users')
  189. })
  190. .table('users', table => {
  191. table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
  192. table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
  193. table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
  194. table.unique(['providerKey', 'email'])
  195. })
  196. }
  197. exports.down = knex => {
  198. return knex.schema
  199. .dropTableIfExists('userGroups')
  200. .dropTableIfExists('pageHistoryTags')
  201. .dropTableIfExists('pageHistory')
  202. .dropTableIfExists('pageTags')
  203. .dropTableIfExists('assets')
  204. .dropTableIfExists('assetFolders')
  205. .dropTableIfExists('comments')
  206. .dropTableIfExists('editors')
  207. .dropTableIfExists('groups')
  208. .dropTableIfExists('locales')
  209. .dropTableIfExists('pages')
  210. .dropTableIfExists('renderers')
  211. .dropTableIfExists('settings')
  212. .dropTableIfExists('storage')
  213. .dropTableIfExists('tags')
  214. .dropTableIfExists('users')
  215. }