summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200214214934_create_environment_for_self_monitoring_project.rb
blob: a44efa3c46036952717ee4d59f59adf5a09cb399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

class CreateEnvironmentForSelfMonitoringProject < ActiveRecord::Migration[6.0]
  DOWNTIME = false

  def up
    execute <<~SQL
      INSERT INTO environments (project_id, name, slug, created_at, updated_at)
      SELECT instance_administration_project_id, 'production', 'production', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
      FROM application_settings
      WHERE instance_administration_project_id IS NOT NULL
      AND NOT EXISTS (
        SELECT 1
        FROM environments
        INNER JOIN application_settings
        ON application_settings.instance_administration_project_id = environments.project_id
      )
    SQL
  end

  def down
    # no-op

    # This migration cannot be reversed because it cannot be ensured that the environment for the Self Monitoring Project
    # did not already exist before the migration ran - in that case, the migration does nothing, and it would be unexpected
    # behavior for that environment to be deleted by reversing this migration.
  end
end