import { promises as fs } from "fs" ; import path from "path" ; import { CLIOptions } from "./types/cli" ; import { GenerateQueryResult } from "./types/core" ; import log from "./utils/logger" ; param name param destinationDir param format param schema param deletes param inserts param updates export default async function createMigrationFile ( name , destinationDir , format , schema , deletes , inserts , updates ) { const migrationFileContent = getContent ( format , schema , deletes , inserts , updates ) ; try { await fs . mkdir ( path . resolve ( process . cwd ( ) , destinationDir ) ) ; } catch ( error ) { if ( error . code != "EEXIST" ) { throw error ; } } const fileName = await getFileName ( name , destinationDir , format ) ; await fs . writeFile ( path . resolve ( process . cwd ( ) , fileName ) , migrationFileContent ) ; return fileName ; } const getFileName = async ( migrationName , destinationDir , format ) : Promise < string > => { const date = new Date ( ) ; const year = new Intl . DateTimeFormat ( "en" , { year : "numeric" } ) . format ( date ) ; const month = new Intl . DateTimeFormat ( "en" , { month : "2-digit" } ) . format ( date ) ; const day = new Intl . DateTimeFormat ( "en" , { day : "2-digit" } ) . format ( date ) ; const migrationTimestamp = `${ year }${ month }${ day }` ; const dailyMigrationsCount = ( await fs . readdir ( path . resolve ( process . cwd ( ) , destinationDir ) ) ) . filter ( file => file . startsWith ( migrationTimestamp ) ) . length ; const suffix = `000${ dailyMigrationsCount + 1 }` . substr ( - 3 ) ; const extension = format === "javascript" ? "js" : "ts" ; const nameEscaped = migrationName . toLowerCase ( ) . replace ( / /g , "-" ) ; return `${ destinationDir }/${ migrationTimestamp }${ suffix }-${ nameEscaped }.${ extension }` ; } ; const queriesMerge = ( queryCollection ) : string => queryCollection . map ( queries => queries . join ( ";\n" ) ) . filter ( item => ! ! item ) . join ( ";\n \n" ) ; const getContent = ( format , schema , deletes , inserts , updates ) => { const upQuery = queriesMerge ( [ schema . up , deletes . up , inserts . up , updates . up ] ) ; const downQuery = queriesMerge ( [ schema . down , deletes . down , inserts . down , updates . down ] ) ; log . message ( "update" , "Up Query" , upQuery ) ; log . message ( "update" , "Down Query" , downQuery ) ; if ( format === "javascript" ) { return generateJSMigration ( upQuery , downQuery ) ; } return generateTSMigration ( upQuery , downQuery ) ; } ; const generateTSMigration = ( up , down ) => `/* eslint-disable no-irregular-whitespace */
import { Knex } from "knex";

export async function up(knex: Knex): Promise<void> {
  await knex.transaction(async transaction => {
    await transaction.raw(\`
${ up }
    \`);
  });
}

export async function down(knex: Knex): Promise<void> {
  await knex.transaction(async transaction => {
    await transaction.raw(\`
${ down }
    \`);
  });
}
` ; const generateJSMigration = ( up , down ) : string => `/* eslint-disable no-irregular-whitespace, no-undef */
module.exports = {
  async up(knex) {
    await knex.transaction(async (transaction) => {
      await transaction.raw(\`
${ up }
      \`);
    });
  },
  async down(knex) {
    await knex.transaction(async (transaction) => {
      await transaction.raw(\`
${ down }
      \`);
    });
  },
};
` ;