--- autoreplies

-- autoreplies

ALTER TABLE autoreplies
  ADD
  project_id BIGINT REFERENCES projects (id),
  deleted_at DATETIMEOFFSET;

CREATE INDEX autoreplies_project_id_ix
  ON autoreplies (project_id);

-- autoreplies: drop channel_id

DECLARE @ConstraintName NVARCHAR(200)
SELECT @ConstraintName = KCU.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC
  INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU
    ON KCU.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG
       AND KCU.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
       AND KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
WHERE
  KCU.TABLE_NAME = 'autoreplies' AND
  KCU.COLUMN_NAME = 'channel_id'
IF @ConstraintName IS NOT NULL
  EXEC ('ALTER TABLE autoreplies DROP CONSTRAINT ' + @ConstraintName)

DROP INDEX autoreplies.autoreplies_channel_id_ix;
ALTER TABLE autoreplies
  DROP COLUMN channel_id;
