Class: CreatePackageStatus

Inherits:
Debci::DB::LEGACY_MIGRATION
  • Object
show all
Defined in:
lib/debci/db/migrations/20200427000000_create_package_status.rb

Instance Method Summary collapse

Instance Method Details

#downObject



32
33
34
# File 'lib/debci/db/migrations/20200427000000_create_package_status.rb', line 32

def down
  drop_table :package_statuses
end

#upObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/debci/db/migrations/20200427000000_create_package_status.rb', line 2

def up
  create_table :package_statuses do |t|
    t.integer :package_id
    t.integer :job_id
    t.string :suite, null: false
    t.string :arch, null: false

    t.index [:package_id, :suite, :arch], unique: true
  end
  add_foreign_key :package_statuses, :packages
  add_foreign_key :package_statuses, :jobs, primary_key: :run_id

  ids = exec_query(%[
    SELECT max(run_id) as run_id
    FROM jobs
    WHERE pin_packages is NULL
          AND status is not NULL
    GROUP BY package_id, suite, arch
  ]).map { |item| item['run_id'] }
  return if ids.empty?

  populate_sql = %[
    INSERT INTO package_statuses(package_id, job_id, suite, arch)
    SELECT package_id, run_id, suite, arch
    FROM jobs
    WHERE run_id IN (%s)
  ] % ids.join(',')
  exec_query(populate_sql)
end