MAJOR=4
MINOR=11
DEVNAME=tty11
  E   Kˆl–Ãa¾”*i?u8 …p/Üiå1hß�þD)ZYÇ )deæÞ‘÷¿ŸØƒyöž×_�uÐb&=LyZËcõÅÖ "ô    K# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

# Using older 5.0 migration to stick to Integer primary keys
class InitializeKnowledgeBase < ActiveRecord::Migration[5.0]
  def change
    return if ActiveRecord::Base.connection.table_exists? 'knowledge_bases'

    create_table :knowledge_bases do |t|
      t.string :iconset, limit: 30, null: false

      t.string :color_highlight,   limit: 25, null: false
      t.string :color_header,      limit: 25, null: false
      t.string :color_header_link, limit: 25, null: false

      t.string :homepage_layout, null: false
      t.string :category_layout, null: false

      # New content sorts alphabetically (KnowledgeBase::DEFAULT_SORTING_MODE); an editor who wants
      # a hand-made order switches the list to `manual` and arranges it. Upgraded installations
      # reach the same default by a longer road, see KnowledgeBaseSortingMode.
      t.string :category_sorting_mode, null: false, limit: 30, default: 'alphabetical'

      t.boolean :active, null: false, default: true
      t.boolean :show_feed_icon, default: false, null: false

      t.string :custom_address

      t.timestamps limit: 3, null: false
    end

    create_table :knowledge_base_locales do |t|
      t.belongs_to :knowledge_base, null: false, foreign_key: { to_table: :knowledge_bases }
      t.belongs_to :system_locale,  null: false, foreign_key: { to_table: :locales }
      t.boolean    :primary, null: false, default: false

      t.timestamps limit: 3, null: false
    end
    add_index :knowledge_base_locales, %i[system_locale_id knowledge_base_id], name: 'index_kb_locale_on_kb_system_locale_kb', unique: true

    create_table :knowledge_base_translations do |t|
      t.string :title, limit: 250, null: false
      t.string :footer_note,       null: false

      t.references :kb_locale,      null: false, foreign_key: { to_table: :knowledge_base_locales }
      t.references :knowledge_base, null: false, foreign_key: { to_table: :knowledge_bases, on_delete: :cascade }

      t.timestamps limit: 3, null: false
    end
    add_index :knowledge_base_translations, %i[kb_locale_id knowledge_base_id], name: 'index_kb_t_on_kb_locale_kb', unique: true

    create_table :knowledge_base_categories do |t|
      t.references :knowledge_base, null: false, foreign_key: { to_table: :knowledge_bases }
      t.references :parent,         null: true,  foreign_key: { to_table: :knowledge_base_categories }

      t.string  :category_icon, null: false, limit: 30
      t.integer :position,      null: false, index: true
      t.string  :category_sorting_mode, null: false, limit: 30, default: 'alphabetical'
      t.string  :answer_sorting_mode,   null: false, limit: 30, default: 'alphabetical'

      t.timestamps limit: 3, null: false
    end

    create_table :knowledge_base_category_translations do |t|
      t.string :title, limit: 250, null: false

      t.references :kb_locale, null: false, foreign_key: { to_table: :knowledge_base_locales }
      t.references :category,  null: false, foreign_key: { to_table: :knowledge_base_categories, on_delete: :cascade }

      t.timestamp :edited_at, limit: 3, null: false

      t.timestamps limit: 3, null: false
    end
    add_index :knowledge_base_category_translations, %i[kb_locale_id category_id], name: 'index_kb_c_t_on_kb_locale_category', unique: true

    create_table :knowledge_base_answers do |t|
      t.references :category, null: false, foreign_key: { to_table: :knowledge_base_categories }

      t.boolean :promoted,      null: false, default: false
      t.text    :internal_note, null: true,  limit: 1.megabyte
      t.integer :position,      null: false, index: true

      t.timestamp  :archived_at,  limit: 3, null: true
      t.references :archived_by,  foreign_key: { to_table: :users }
      t.timestamp  :internal_at,  limit: 3, null: true
      t.references :internal_by,  foreign_key: { to_table: :users }
      t.timestamp  :published_