diff options
Diffstat (limited to 'db/migrate/20201110221400_create_experiment_subjects.rb')
-rw-r--r-- | db/migrate/20201110221400_create_experiment_subjects.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/db/migrate/20201110221400_create_experiment_subjects.rb b/db/migrate/20201110221400_create_experiment_subjects.rb new file mode 100644 index 00000000000..0c04d5b219f --- /dev/null +++ b/db/migrate/20201110221400_create_experiment_subjects.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class CreateExperimentSubjects < ActiveRecord::Migration[6.0] + DOWNTIME = false + + def up + create_table :experiment_subjects do |t| + t.references :experiment, index: true, foreign_key: { on_delete: :cascade }, null: false + t.bigint :user_id, index: true + t.bigint :group_id, index: true + t.bigint :project_id, index: true + t.integer :variant, limit: 2, null: false, default: 0 + t.timestamps_with_timezone null: false + end + + # Require exactly one of user_id, group_id, or project_id to be NOT NULL + execute <<-SQL + ALTER TABLE experiment_subjects ADD CONSTRAINT chk_has_one_subject CHECK (num_nonnulls(user_id, group_id, project_id) = 1); + SQL + end + + def down + drop_table :experiment_subjects + end +end |