class RebuildSettingsAsTypedUserSettings < ActiveRecord::Migration[8.0]
  def change
    remove_foreign_key :settings, :users if foreign_key_exists?(:settings, :users)
    remove_index :settings, :user_id if index_exists?(:settings, :user_id)

    remove_column :settings, :key, :string if column_exists?(:settings, :key)
    remove_column :settings, :value, :json if column_exists?(:settings, :value)

    change_column_null :settings, :user_id, false

    add_column :settings, :theme, :string, null: false, default: 'system'
    add_column :settings,
               :auto_fetch_title,
               :string,
               null: false,
               default: 'manual'
    add_column :settings,
               :auto_fetch_thumbnail,
               :string,
               null: false,
               default: 'manual'
    add_column :settings,
               :wiki_editor_mode,
               :string,
               null: false,
               default: 'split'

    add_index :settings, :user_id, unique: true
    add_foreign_key :settings, :users
  end
end
