Skip to content

Commit

Permalink
attributes migration
Browse files Browse the repository at this point in the history
Add a new table that allows for attributes in json form #505
  • Loading branch information
robkooper committed Nov 30, 2018
1 parent 61ef1b8 commit 0a4d53c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions db/migrate/20181129000515_create_attributes_table.rb
@@ -0,0 +1,27 @@
class CreateAttributesTable < ActiveRecord::Migration[5.1]
def change
this_hostid = Machine.new.hostid

create_table :attributes, id: :bigint do |t|
t.string :container_type, null: false
t.integer :container_id, limit: 8, null: false
t.jsonb :value, null: false, default: '{}'
t.timestamps
end

add_index :attributes, :container_id
add_index :attributes, :value, using: :gin

reversible do |dir|
dir.up do
execute %{
SELECT setval('attributes_id_seq', GREATEST(1, 1 + CAST(1e9 * #{this_hostid}::int AS bigint)), FALSE);
ALTER TABLE "attributes"
ALTER COLUMN created_at SET DEFAULT utc_now(),
ALTER COLUMN updated_at SET DEFAULT utc_now(),
ADD CONSTRAINT container_type_id UNIQUE(container_type, container_id);
}
end
end
end
end

0 comments on commit 0a4d53c

Please sign in to comment.