package migrate

import (
	"github.com/pingcap/log"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

var _ = AddDBMigrateRecord(
	"202403281354",
	func(db *gorm.DB) error {
		ddl := "CREATE TABLE `imports` (" +
			"`id` varchar(255) NOT NULL," +
			"`created_at` TIMESTAMP NOT NULL," +
			"`updated_at` TIMESTAMP NULL DEFAULT NULL," +
			"`status` ENUM('unknown','preparing', 'importing','completed','failed', 'canceling', 'canceled') NOT NULL DEFAULT 'preparing'," +
			"`started_at` TIMESTAMP NULL DEFAULT NULL," +
			"`completed_at` TIMESTAMP NULL DEFAULT NULL," +
			"`creator` VARCHAR(255)," +
			"`cluster_id` bigint unsigned NOT NULL," +
			"`project_id` bigint unsigned NOT NULL," +
			"`tenant_id` bigint unsigned NOT NULL," +
			"`message` TEXT," +
			"`clean_up` boolean NOT NULL DEFAULT false," +
			"`bucket_region` VARCHAR(255)," +
			"`data_format` int unsigned," +
			"`data_source_type` int unsigned," +
			"`csv_format` VARCHAR(1024)," +
			"`total_size` bigint unsigned DEFAULT NULL," +
			"`processed_data_size` bigint unsigned DEFAULT NULL," +
			"`total_files` int unsigned DEFAULT NULL," +
			"`source_url` VARCHAR(1024)," +
			"`current_tables` JSON," +
			"`total_tables_count` int unsigned," +
			"`completed_tables` int unsigned," +
			"`pending_tables` int unsigned," +
			"`completed_percent` int unsigned," +
			"`post_import_completed_percent` int unsigned," +
			"`table_results` JSON," +
			"`task_id` bigint," +
			"`local_import_cfg` JSON DEFAULT NULL," +
			"`target_table_infos` JSON," +
			"`need_drop_tables` tinyint(1)," +
			"PRIMARY KEY (`id`)," +
			"INDEX `idx_imports_clean_up` (`clean_up`)," +
			"INDEX `idx_imports_completed_at` (`completed_at`)," +
			"INDEX `idx_imports_cluster_id` (`cluster_id`))"
		err := db.Exec(ddl).Error
		if err != nil {
			log.Error("create table imports failed", zap.Error(err))
		}
		return nil
	},
)
