-- +goose Up
SET @col_default_shift_start_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'default_shift_start'
);
SET @add_default_shift_start_sql := IF(
  @col_default_shift_start_exists = 0,
  'ALTER TABLE bases ADD COLUMN default_shift_start TIME NULL AFTER leg_time',
  'SELECT 1'
);
PREPARE stmt_add_default_shift_start FROM @add_default_shift_start_sql;
EXECUTE stmt_add_default_shift_start;
DEALLOCATE PREPARE stmt_add_default_shift_start;

SET @col_default_shift_end_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'default_shift_end'
);
SET @add_default_shift_end_sql := IF(
  @col_default_shift_end_exists = 0,
  'ALTER TABLE bases ADD COLUMN default_shift_end TIME NULL AFTER default_shift_start',
  'SELECT 1'
);
PREPARE stmt_add_default_shift_end FROM @add_default_shift_end_sql;
EXECUTE stmt_add_default_shift_end;
DEALLOCATE PREPARE stmt_add_default_shift_end;

SET @col_foto_attachment_id_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'foto_attachment_id'
);
SET @add_foto_attachment_id_sql := IF(
  @col_foto_attachment_id_exists = 0,
  'ALTER TABLE bases ADD COLUMN foto_attachment_id BINARY(16) NULL AFTER foto',
  'SELECT 1'
);
PREPARE stmt_add_foto_attachment_id FROM @add_foto_attachment_id_sql;
EXECUTE stmt_add_foto_attachment_id;
DEALLOCATE PREPARE stmt_add_foto_attachment_id;

SET @col_deleted_at_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'deleted_at'
);
SET @add_deleted_at_sql := IF(
  @col_deleted_at_exists = 0,
  'ALTER TABLE bases ADD COLUMN deleted_at DATETIME(3) NULL AFTER updated_at',
  'SELECT 1'
);
PREPARE stmt_add_deleted_at FROM @add_deleted_at_sql;
EXECUTE stmt_add_deleted_at;
DEALLOCATE PREPARE stmt_add_deleted_at;

SET @col_deleted_by_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'deleted_by'
);
SET @add_deleted_by_sql := IF(
  @col_deleted_by_exists = 0,
  'ALTER TABLE bases ADD COLUMN deleted_by BINARY(16) NULL AFTER deleted_at',
  'SELECT 1'
);
PREPARE stmt_add_deleted_by FROM @add_deleted_by_sql;
EXECUTE stmt_add_deleted_by;
DEALLOCATE PREPARE stmt_add_deleted_by;

SET @idx_bases_deleted_at_exists := (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND index_name = 'idx_bases_deleted_at'
);
SET @add_idx_bases_deleted_at_sql := IF(
  @idx_bases_deleted_at_exists = 0,
  'ALTER TABLE bases ADD INDEX idx_bases_deleted_at (deleted_at)',
  'SELECT 1'
);
PREPARE stmt_add_idx_bases_deleted_at FROM @add_idx_bases_deleted_at_sql;
EXECUTE stmt_add_idx_bases_deleted_at;
DEALLOCATE PREPARE stmt_add_idx_bases_deleted_at;

SET @idx_bases_foto_attachment_id_exists := (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND index_name = 'idx_bases_foto_attachment_id'
);
SET @add_idx_bases_foto_attachment_id_sql := IF(
  @idx_bases_foto_attachment_id_exists = 0,
  'ALTER TABLE bases ADD INDEX idx_bases_foto_attachment_id (foto_attachment_id)',
  'SELECT 1'
);
PREPARE stmt_add_idx_bases_foto_attachment_id FROM @add_idx_bases_foto_attachment_id_sql;
EXECUTE stmt_add_idx_bases_foto_attachment_id;
DEALLOCATE PREPARE stmt_add_idx_bases_foto_attachment_id;

SET @attachments_table_exists := (
  SELECT COUNT(*)
  FROM information_schema.tables
  WHERE table_schema = DATABASE()
    AND table_name = 'attachments'
);
SET @fk_bases_foto_attachment_exists := (
  SELECT COUNT(*)
  FROM information_schema.table_constraints
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND constraint_name = 'fk_bases_foto_attachment'
    AND constraint_type = 'FOREIGN KEY'
);
SET @add_fk_bases_foto_attachment_sql := IF(
  @attachments_table_exists > 0 AND @fk_bases_foto_attachment_exists = 0,
  'ALTER TABLE bases ADD CONSTRAINT fk_bases_foto_attachment FOREIGN KEY (foto_attachment_id) REFERENCES attachments(id) ON UPDATE CASCADE ON DELETE SET NULL',
  'SELECT 1'
);
PREPARE stmt_add_fk_bases_foto_attachment FROM @add_fk_bases_foto_attachment_sql;
EXECUTE stmt_add_fk_bases_foto_attachment;
DEALLOCATE PREPARE stmt_add_fk_bases_foto_attachment;

-- +goose Down
SET @fk_bases_foto_attachment_exists := (
  SELECT COUNT(*)
  FROM information_schema.table_constraints
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND constraint_name = 'fk_bases_foto_attachment'
    AND constraint_type = 'FOREIGN KEY'
);
SET @drop_fk_bases_foto_attachment_sql := IF(
  @fk_bases_foto_attachment_exists > 0,
  'ALTER TABLE bases DROP FOREIGN KEY fk_bases_foto_attachment',
  'SELECT 1'
);
PREPARE stmt_drop_fk_bases_foto_attachment FROM @drop_fk_bases_foto_attachment_sql;
EXECUTE stmt_drop_fk_bases_foto_attachment;
DEALLOCATE PREPARE stmt_drop_fk_bases_foto_attachment;

SET @idx_bases_foto_attachment_id_exists := (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND index_name = 'idx_bases_foto_attachment_id'
);
SET @drop_idx_bases_foto_attachment_id_sql := IF(
  @idx_bases_foto_attachment_id_exists > 0,
  'ALTER TABLE bases DROP INDEX idx_bases_foto_attachment_id',
  'SELECT 1'
);
PREPARE stmt_drop_idx_bases_foto_attachment_id FROM @drop_idx_bases_foto_attachment_id_sql;
EXECUTE stmt_drop_idx_bases_foto_attachment_id;
DEALLOCATE PREPARE stmt_drop_idx_bases_foto_attachment_id;

SET @idx_bases_deleted_at_exists := (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND index_name = 'idx_bases_deleted_at'
);
SET @drop_idx_bases_deleted_at_sql := IF(
  @idx_bases_deleted_at_exists > 0,
  'ALTER TABLE bases DROP INDEX idx_bases_deleted_at',
  'SELECT 1'
);
PREPARE stmt_drop_idx_bases_deleted_at FROM @drop_idx_bases_deleted_at_sql;
EXECUTE stmt_drop_idx_bases_deleted_at;
DEALLOCATE PREPARE stmt_drop_idx_bases_deleted_at;

SET @col_deleted_by_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'deleted_by'
);
SET @drop_deleted_by_sql := IF(
  @col_deleted_by_exists > 0,
  'ALTER TABLE bases DROP COLUMN deleted_by',
  'SELECT 1'
);
PREPARE stmt_drop_deleted_by FROM @drop_deleted_by_sql;
EXECUTE stmt_drop_deleted_by;
DEALLOCATE PREPARE stmt_drop_deleted_by;

SET @col_deleted_at_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'deleted_at'
);
SET @drop_deleted_at_sql := IF(
  @col_deleted_at_exists > 0,
  'ALTER TABLE bases DROP COLUMN deleted_at',
  'SELECT 1'
);
PREPARE stmt_drop_deleted_at FROM @drop_deleted_at_sql;
EXECUTE stmt_drop_deleted_at;
DEALLOCATE PREPARE stmt_drop_deleted_at;

SET @col_foto_attachment_id_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'foto_attachment_id'
);
SET @drop_foto_attachment_id_sql := IF(
  @col_foto_attachment_id_exists > 0,
  'ALTER TABLE bases DROP COLUMN foto_attachment_id',
  'SELECT 1'
);
PREPARE stmt_drop_foto_attachment_id FROM @drop_foto_attachment_id_sql;
EXECUTE stmt_drop_foto_attachment_id;
DEALLOCATE PREPARE stmt_drop_foto_attachment_id;

SET @col_default_shift_end_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'default_shift_end'
);
SET @drop_default_shift_end_sql := IF(
  @col_default_shift_end_exists > 0,
  'ALTER TABLE bases DROP COLUMN default_shift_end',
  'SELECT 1'
);
PREPARE stmt_drop_default_shift_end FROM @drop_default_shift_end_sql;
EXECUTE stmt_drop_default_shift_end;
DEALLOCATE PREPARE stmt_drop_default_shift_end;

SET @col_default_shift_start_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'bases'
    AND column_name = 'default_shift_start'
);
SET @drop_default_shift_start_sql := IF(
  @col_default_shift_start_exists > 0,
  'ALTER TABLE bases DROP COLUMN default_shift_start',
  'SELECT 1'
);
PREPARE stmt_drop_default_shift_start FROM @drop_default_shift_start_sql;
EXECUTE stmt_drop_default_shift_start;
DEALLOCATE PREPARE stmt_drop_default_shift_start;
