import { MigrationInterface, QueryRunner } from 'typeorm';

export class addLogTable1671501326625 implements MigrationInterface {
  name = 'addLogTable1671501326625';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE IF NOT EXISTS \`logs\` (
        \`id\` int(11) NOT NULL AUTO_INCREMENT,
        \`operatorUserId\` char(36) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
        \`type\` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
        \`action\` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
        \`affectedId\` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
        \`params\` text COLLATE utf8_unicode_ci,
        \`message\` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        \`client\` enum('panel','site','app','system') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'site',
        \`ip\` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
        \`createdAt\` datetime NOT NULL,
        \`updatedAt\` datetime NOT NULL,
        PRIMARY KEY (\`id\`),
        KEY \`fk_logs_users1_idx\` (\`operatorUserId\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;`,
    );

    await queryRunner.query(
      `ALTER TABLE \`logs\` ADD CONSTRAINT \`fk_logs_users1\` FOREIGN KEY (\`operatorUserId\`) REFERENCES \`users\` (\`id\`)`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    //
  }
}
