import { Base , CallbackFunction } from 'db-migrate-base' ; interface Row { name : string ; type : string ; } export default function removeColumn ( db , tableName , columnName , cb ) { db . all ( `PRAGMA table_info(${ tableName });` , async ( err , rows ) => { if ( err ) { cb ( err , null ) ; } else { try { const sqlNamesWithTypes = rows . filter ( row => row . name !== columnName ) . map ( row => `${ row . name } ${ row . type }` ) . join ( ', ' ) ; const sqlNames = rows . filter ( row => row . name !== columnName ) . map ( row => row . name ) . join ( ', ' ) ; const removeColumnSql = `
        CREATE TEMPORARY TABLE ${ tableName + '_backup' }(${ sqlNamesWithTypes });
        INSERT INTO ${ tableName + '_backup' } SELECT ${ sqlNames } FROM ${ tableName };
        DROP TABLE ${ tableName };
        CREATE TABLE ${ tableName }(${ sqlNamesWithTypes });
        INSERT INTO ${ tableName } SELECT ${ sqlNames } FROM ${ tableName + '_backup' };
        DROP TABLE ${ tableName + '_backup' };
       ` ; db . runSql ( removeColumnSql ) ; cb ( null , null ) ; } catch ( e ) { cb ( e , null ) ; } } } ) ; }