Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new table that allows for attributes in json form #505
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |