Class: CreateWorkers

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

Instance Method Summary collapse

Instance Method Details

#downObject



16
17
18
19
20
21
22
# File 'lib/debci/db/migrations/20211227000000_create_workers.rb', line 16

def down
  add_column :jobs, :worker, :string
  execute "UPDATE jobs SET worker = (SELECT workers.name FROM workers WHERE jobs.worker_id = workers.id)"
  remove_foreign_key :jobs, :workers
  remove_column :jobs, :worker_id
  drop_table :workers
end

#upObject



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/debci/db/migrations/20211227000000_create_workers.rb', line 2

def up
  create_table(:workers) do |t|
    t.string :name, limit: 128, unique: true
  end
  add_index :workers, :name

  add_column :jobs, :worker_id, :integer, null: true
  add_foreign_key :jobs, :workers, column: :worker_id

  execute "INSERT INTO workers(name) SELECT distinct(worker) FROM jobs"
  execute "UPDATE jobs SET worker_id = (SELECT workers.id FROM workers WHERE jobs.worker = workers.name)"
  remove_column :jobs, :worker
end