-- +goose Up
ALTER TABLE file_files
  ADD COLUMN is_template TINYINT(1) NOT NULL DEFAULT 0 AFTER checksum_sha256;

UPDATE file_files
SET is_template = 1
WHERE EXISTS (
  SELECT 1
  FROM helicopter_files hf
  WHERE hf.source_file_id = file_files.id
)
OR EXISTS (
  SELECT 1
  FROM helicopter_files hf
  INNER JOIN attachments a ON a.id = hf.file_attachment_id
    AND a.file_id = file_files.id
);

-- +goose Down
ALTER TABLE file_files
  DROP COLUMN is_template;
