20190531180304_initialize_knowledge_base.rb 7.4 KB

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