20190531180304_initialize_knowledge_base.rb 7.8 KB

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