20190531180304_initialize_knowledge_base.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Using older 5.0 migration to stick to Integer primary keys. Otherwise migration fails in MySQL.
  2. class InitializeKnowledgeBase < ActiveRecord::Migration[5.0]
  3. def change
  4. return if ActiveRecord::Base.connection.table_exists? 'knowledge_bases'
  5. create_table :knowledge_bases do |t|
  6. t.string :iconset, limit: 30, null: false
  7. t.string :color_highlight, limit: 25, null: false
  8. t.string :color_header, limit: 25, null: false
  9. t.string :homepage_layout, null: false
  10. t.string :category_layout, null: false
  11. t.boolean :active, null: false, default: true
  12. t.string :custom_address
  13. t.timestamps null: false
  14. end
  15. create_table :knowledge_base_locales do |t|
  16. t.belongs_to :knowledge_base, null: false, foreign_key: { to_table: :knowledge_bases }
  17. t.belongs_to :system_locale, null: false, foreign_key: { to_table: :locales }
  18. t.boolean :primary, null: false, default: false
  19. t.timestamps null: false
  20. end
  21. create_table :knowledge_base_translations do |t|
  22. t.string :title, limit: 250, null: false
  23. t.string :footer_note, null: false
  24. t.references :kb_locale, null: false, foreign_key: { to_table: :knowledge_base_locales }
  25. t.references :knowledge_base, null: false, foreign_key: { to_table: :knowledge_bases, on_delete: :cascade }
  26. t.timestamps null: false
  27. end
  28. create_table :knowledge_base_categories do |t|
  29. t.references :knowledge_base, null: false, foreign_key: { to_table: :knowledge_bases }
  30. t.references :parent, null: true, foreign_key: { to_table: :knowledge_base_categories }
  31. t.string :category_icon, null: false, limit: 30
  32. t.integer :position, null: false, index: true
  33. t.timestamps null: false
  34. end
  35. create_table :knowledge_base_category_translations do |t|
  36. t.string :title, limit: 250, null: false
  37. t.references :kb_locale, null: false, foreign_key: { to_table: :knowledge_base_locales }
  38. t.references :category, null: false, foreign_key: { to_table: :knowledge_base_categories, on_delete: :cascade }
  39. t.timestamps null: false
  40. end
  41. create_table :knowledge_base_answers do |t|
  42. t.references :category, null: false, foreign_key: { to_table: :knowledge_base_categories }
  43. t.boolean :promoted, null: false, default: false
  44. t.text :internal_note, null: true, limit: 1.megabyte
  45. t.integer :position, null: false, index: true
  46. t.timestamp :archived_at, limit: 3, null: true
  47. t.references :archived_by, foreign_key: { to_table: :users }
  48. t.timestamp :internal_at, limit: 3, null: true
  49. t.references :internal_by, foreign_key: { to_table: :users }
  50. t.timestamp :published_at, limit: 3, null: true
  51. t.references :published_by, foreign_key: { to_table: :users }
  52. t.timestamps null: false
  53. end
  54. create_table :knowledge_base_answer_translation_contents do |t| # rubocop:disable Rails/CreateTableWithTimestamps
  55. t.text :body, null: true, limit: 20.megabytes + 1
  56. end
  57. create_table :knowledge_base_answer_translations do |t|
  58. t.string :title, limit: 250, null: false
  59. t.references :kb_locale, null: false, foreign_key: { to_table: :knowledge_base_locales }
  60. t.references :answer, null: false, foreign_key: { to_table: :knowledge_base_answers, on_delete: :cascade }
  61. t.references :content, null: false, foreign_key: { to_table: :knowledge_base_answer_translation_contents }
  62. t.references :created_by, null: false, foreign_key: { to_table: :users }
  63. t.references :updated_by, null: false, foreign_key: { to_table: :users }
  64. t.timestamps null: false
  65. end
  66. create_table :knowledge_base_menu_items do |t|
  67. t.references :kb_locale, null: false, foreign_key: { to_table: :knowledge_base_locales, on_delete: :cascade }
  68. t.string :location, null: false, index: true
  69. t.integer :position, null: false, index: true
  70. t.string :title, null: false, limit: 100
  71. t.string :url, null: false, limit: 500
  72. t.boolean :new_tab, null: false, default: false
  73. t.timestamps
  74. end
  75. Setting.create_if_not_exists(
  76. title: 'Kb multi-lingual support',
  77. name: 'kb_multi_lingual_support',
  78. area: 'Kb::Core',
  79. description: 'Support of multi-lingual Knowledge Base.',
  80. options: {},
  81. state: true,
  82. preferences: { online_service_disable: true },
  83. frontend: true
  84. )
  85. Setting.create_if_not_exists(
  86. title: 'Kb active',
  87. name: 'kb_active',
  88. area: 'Kb::Core',
  89. description: 'Defines if KB navbar button is enabled. Updated in KnowledgeBase callback.',
  90. state: false,
  91. preferences: {
  92. prio: 1,
  93. trigger: ['menu:render'],
  94. authentication: true,
  95. permission: ['admin.knowledge_base'],
  96. },
  97. frontend: true
  98. )
  99. Setting.create_if_not_exists(
  100. title: 'Kb active publicly',
  101. name: 'kb_active_publicly',
  102. area: 'Kb::Core',
  103. description: 'Defines if KB navbar button is enabled for users without KB permission. Updated in CanBePublished callback.',
  104. state: false,
  105. preferences: {
  106. prio: 1,
  107. trigger: ['menu:render'],
  108. authentication: true,
  109. permission: [],
  110. },
  111. frontend: true
  112. )
  113. return if !Setting.exists?(name: 'system_init_done')
  114. Permission.create_if_not_exists(
  115. name: 'admin.knowledge_base',
  116. note: 'Create and setup %s',
  117. preferences: {
  118. translations: ['Knowledge Base']
  119. }
  120. )
  121. Permission.create_if_not_exists(
  122. name: 'knowledge_base',
  123. note: 'Manage %s',
  124. preferences: {
  125. translations: ['Knowledge Base'],
  126. disabled: true,
  127. }
  128. )
  129. Permission.create_if_not_exists(
  130. name: 'knowledge_base.reader',
  131. note: 'Access %s',
  132. preferences: {
  133. translations: ['Knowledge Base']
  134. }
  135. )
  136. Permission.create_if_not_exists(
  137. name: 'knowledge_base.editor',
  138. note: 'Manage %s',
  139. preferences: {
  140. translations: ['Knowledge Base Editor']
  141. }
  142. )
  143. Role.with_permissions(['admin']).each do |role|
  144. role.permission_grant('knowledge_base.editor')
  145. end
  146. Role.with_permissions(['ticket.agent']).each do |role|
  147. role.permission_grant('knowledge_base.reader')
  148. end
  149. end
  150. end