SET @db_name = DATABASE();

SET @sql = IF(
  EXISTS(
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @db_name
      AND TABLE_NAME = 'loan_monthly_transactions'
      AND COLUMN_NAME = 'unit_price'
  ),
  'SELECT 1',
  'ALTER TABLE loan_monthly_transactions ADD COLUMN unit_price DECIMAL(14,2) NOT NULL DEFAULT 0.00 AFTER qty'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS(
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @db_name
      AND TABLE_NAME = 'loan_monthly_transactions'
      AND COLUMN_NAME = 'line_total'
  ),
  'SELECT 1',
  'ALTER TABLE loan_monthly_transactions ADD COLUMN line_total DECIMAL(14,2) NOT NULL DEFAULT 0.00 AFTER unit_price'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS(
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @db_name
      AND TABLE_NAME = 'loan_monthly_closing_items'
      AND COLUMN_NAME = 'unit_price'
  ),
  'SELECT 1',
  'ALTER TABLE loan_monthly_closing_items ADD COLUMN unit_price DECIMAL(14,2) NOT NULL DEFAULT 0.00 AFTER qty'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS(
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @db_name
      AND TABLE_NAME = 'loan_monthly_closing_items'
      AND COLUMN_NAME = 'line_total'
  ),
  'SELECT 1',
  'ALTER TABLE loan_monthly_closing_items ADD COLUMN line_total DECIMAL(14,2) NOT NULL DEFAULT 0.00 AFTER unit_price'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
