Project

General

Profile

Download (2.09 KB) Statistics
| Branch: | Tag: | Revision:

foreman-docker / db / migrate / 20141209182008_remove_docker_tables.rb @ 7ff105d1

1
class RemoveDockerTables < ActiveRecord::Migration
2
  class DockerImage
3
  end
4

    
5
  class DockerTag
6
  end
7

    
8
  def up
9
    change_table :containers do |t|
10
      t.string :repository_name
11
      t.string :tag
12
      t.belongs_to :registry
13
    end
14

    
15
    Container.reset_column_information
16
    Container.all do |container|
17
      image = DockerImage.find(container.docker_image_id)
18
      container.update_attribute(:repository_name, image.image_id)
19

    
20
      tag = DockerTag.find(container.docker_tag_id)
21
      container.update_attribute(:tag, tag.tag)
22
    end
23
    remove_foreign_key :containers, :name => :containers_docker_image_id_fk
24
    remove_foreign_key :containers, :name => :containers_docker_tag_id_fk
25
    remove_column :containers, :docker_image_id
26
    remove_column :containers, :docker_tag_id
27

    
28
    # these tables might have foreign keys from plugins like katello so use cascade
29
    cascade_drop(:docker_images)
30
    cascade_drop(:docker_tags)
31
    cascade_drop(:docker_image_docker_registries)
32
  end
33

    
34
  def down
35
    remove_column :containers, :repository_name
36
    remove_column :containers, :registry_id
37
    remove_column :containers, :tag
38
    add_column :containers, :docker_image_id, :integer
39
    add_column :containers, :docker_tag_id, :integer
40

    
41
    create_table :docker_images do |t|
42
      t.string  :image_id
43
      t.integer :size
44
      t.timestamps
45
    end
46
    create_table :docker_tags do |t|
47
      t.string :tag
48
      t.references :docker_image, :null => false
49
      t.timestamps
50
    end
51
    create_table :docker_image_docker_registries do |t|
52
      t.integer :id
53
      t.integer :docker_registry_id
54
      t.integer :docker_image_id
55
    end
56
  end
57

    
58
  def cascade_drop(table_name)
59
    case connection.adapter_name.downcase.to_sym
60
    when :mysql, :mysql2
61
      execute "SET FOREIGN_KEY_CHECKS=0"
62
      execute "DROP TABLE #{table_name}"
63
      execute "SET FOREIGN_KEY_CHECKS=1"
64
    when :pg, :postgresql
65
      execute "DROP TABLE #{table_name} CASCADE"
66
    when :sqlite
67
      execute "DROP TABLE #{table_name}"
68
    else
69
      fail NotImplementedError, "Unknown adapter type '#{connection.adapter_name.downcase.to_sym}'"
70
    end
71
  end
72
end