HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/openproject/db/migrate/20171106074835_move_hashed_token_to_core.rb
class MoveHashedTokenToCore < ActiveRecord::Migration[5.1]
  class OldToken < ActiveRecord::Base
    self.table_name = :plaintext_tokens
  end

  def up
    rename_table :tokens, :plaintext_tokens
    create_tokens_table
    migrate_existing_tokens
  end

  def down
    drop_table :tokens
    rename_table :plaintext_tokens, :tokens
  end

  private

  def create_tokens_table
    create_table :tokens, id: :integer do |t|
      t.references :user, index: true
      t.string :type
      t.string :value, default: "", null: false, limit: 128
      t.datetime :created_on, null: false
      t.datetime :expires_on, null: true
    end
  end

  def migrate_existing_tokens
    # API tokens
    ::Token::API.transaction do
      OldToken.where(action: 'api').find_each do |token|
        result = ::Token::API.create(user_id: token.user_id, value: ::Token::API.hash_function(token.value))
        warn "Failed to migrate API token for ##{user.id}" unless result
      end
    end

    # RSS tokens
    ::Token::RSS.transaction do
      OldToken.where(action: 'feeds').find_each do |token|
        result = ::Token::RSS.create(user_id: token.user_id, value: token.value)
        warn "Failed to migrate RSS token for ##{user.id}" unless result
      end
    end

    # We do not migrate the rest, they are short-lived anyway.
  end
end