2.0.0.js 8.6 KB

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