I am adding a new table to katello and would like to learn how to properly define the migration. Here are the details:
ManifestPlatform (new model)
- required to reference one ManifestList
- required to reference one Manifest
- fields arch, os
ManifestList (existing model)
- may reference zero or more ManifestPlatform
- on ManifestList destroy, dependent destroy associated ManifestPlatform
Manifest (existing model)
- may be referenced by zero or more ManifestPlatform
Common use cases:
- For a Manifest, show its ManifestPlatforms
- For a ManifestList, show its ManifestPlatforms
- Find all ManifestLists with ManifestPlatform.arch == “x86_64”
Here’s what I have so far below. It was based on other migrations but I’d like to know the “why” of the lines and get corrections.
class AddDockerManifestPlatform < ActiveRecord::Migration[5.1]
def up
create_table :katello_docker_manifest_platforms do |t|
t.string :arch, :limit => 255
t.string :os, :limit => 255
t.timestamps
end
create_table :katello_manifest_manifest_platforms do |t|
t.references :docker_manifest, :null => false
t.references :docker_manifest_platform, :null => false
t.timestamps
end
create_table :katello_manifest_list_manifest_platforms do |t|
t.references :docker_manifest_list, :null => false
t.references :docker_manifest_platform, :null => false
t.timestamps
end
add_index :katello_docker_manifest_manifest_platforms, [:docker_manifest_id, :docker_manifest_platform_id],
:name => :katello_docker_manifest_manifest_platform_id
add_index :katello_docker_manifest_list_manifest_platforms, [:docker_manifest_list_id, :docker_manifest_platform_id],
:name => :katello_docker_manifest_list_manifest_platform_id, :unique => true
add_foreign_key :katello_docker_manifest_manifest_platforms, :katello_docker_manifest_platforms,
:column => :docker_manifest_platform_id
add_foreign_key :katello_docker_manifest_list_manifest_platforms, :katello_docker_manifest_platforms,
:column => :docker_manifest_platform_id
end
end