Project

General

Profile

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

foreman_docker / db / migrate / 20141209182008_remove_docker_tables.rb @ fb471374

1
class RemoveDockerTables < ActiveRecord::Migration[4.2]
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

    
24
    if foreign_key_exists?(:containers, :name => :containers_docker_image_id_fk)
25
      remove_foreign_key :containers, :name => :containers_docker_image_id_fk
26
    end
27
    if foreign_key_exists?(:containers, :name => :containers_docker_tag_id_fk)
28
      remove_foreign_key :containers, :name => :containers_docker_tag_id_fk
29
    end
30

    
31
    remove_reference :containers, :docker_image, :foreign_key => true
32
    remove_reference :containers, :docker_tag, :foreign_key => true
33

    
34
    # these tables might have foreign keys from plugins like katello so use cascade
35
    cascade_drop(:docker_images)
36
    cascade_drop(:docker_tags)
37
    cascade_drop(:docker_image_docker_registries)
38
  end
39

    
40
  def down
41
    remove_column :containers, :repository_name
42
    remove_column :containers, :registry_id
43
    remove_column :containers, :tag
44
    add_column :containers, :docker_image_id, :integer
45
    add_column :containers, :docker_tag_id, :integer
46

    
47
    create_table :docker_images do |t|
48
      t.string  :image_id
49
      t.integer :size
50
      t.timestamps
51
    end
52
    create_table :docker_tags do |t|
53
      t.string :tag
54
      t.references :docker_image, :null => false
55
      t.timestamps
56
    end
57
    create_table :docker_image_docker_registries do |t|
58
      t.integer :id
59
      t.integer :docker_registry_id
60
      t.integer :docker_image_id
61
    end
62
  end
63

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