20190531180304_initialize_knowledge_base.rb 8.0 KB

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