@@ -8,6 +8,7 @@ import { QueryFunction } from 'mysql';
88 */
99export interface MigrationConnection {
1010 query : QueryFunction ;
11+ end ( callback ?: ( err : Error | null ) => void ) : void ;
1112}
1213
1314/**
@@ -49,7 +50,7 @@ export class Migration {
4950 private maxVersion = 0 ;
5051 private isInit = false ;
5152
52- public constructor ( config : MigrationConfig ) {
53+ public constructor ( config : MigrationConfig ) {
5354 this . config = config ;
5455 this . isInit = false ;
5556 }
@@ -81,8 +82,8 @@ export class Migration {
8182 while ( infinite || countStep < steps ) {
8283 const script = await this . getNextUpgradeScript ( ) ;
8384 if ( ! script ) {
84- const ver = await this . getLastVersion ( ) ;
85- if ( ver != this . maxVersion ) {
85+ const { version } = await this . getLastVersion ( ) ;
86+ if ( version != this . maxVersion ) {
8687 throw new Error ( 'Next upgrade script not found!' ) ;
8788 } else {
8889 this . writeLog ( 'Upgrade complete!' ) ;
@@ -120,8 +121,8 @@ export class Migration {
120121 while ( infinite || countStep < steps ) {
121122 const script = await this . getNextDowngradeScript ( ) ;
122123 if ( ! script ) {
123- const ver = await this . getLastVersion ( ) ;
124- if ( ver ) {
124+ const { version } = await this . getLastVersion ( ) ;
125+ if ( version ) {
125126 throw new Error ( 'Next downgrade script not found!' ) ;
126127 } else {
127128 this . writeLog ( 'Downgrade complete!' ) ;
@@ -151,14 +152,28 @@ export class Migration {
151152 /**
152153 * Returns last version of database
153154 */
154- public async getLastVersion ( ) : Promise < number > {
155+ public async getLastVersion ( ) : Promise < { version : number , fileName : string } > {
155156 return await this . query (
156- `SELECT version
157+ `SELECT version, fileName
157158 FROM ${ this . config . tableName }
158159 ORDER BY version DESC
159- LIMIT 1` , null , true ) . then ( ( val : any ) => ! ! val && val . length ? val [ 0 ] . version : 0 ) ;
160+ LIMIT 1` , null , true
161+ ) . then (
162+ ( val : any ) =>
163+ ! ! val && val . length ?
164+ { version : val [ 0 ] . version , fileName : val [ 0 ] . fileName } :
165+ { version : 0 , fileName : null }
166+ ) ;
160167 }
161168
169+ public async destroy ( ) {
170+ await new Promise < void > ( ( resolve , reject ) => {
171+ this . config . conn . end ( ( e ) => {
172+ this . writeLog ( `Migrations: DB connection terminated: ${ e ?. message || 'CLOSED' } ` ) ;
173+ resolve ( ) ;
174+ } ) ;
175+ } ) ;
176+ }
162177 /**
163178 * Loads scripts from file system
164179 */
@@ -195,9 +210,9 @@ export class Migration {
195210 * Returns next upgrade script object
196211 */
197212 private async getNextUpgradeScript ( ) : Promise < MigrationScript > {
198- const ver = await this . getLastVersion ( ) ;
213+ const { version } = await this . getLastVersion ( ) ;
199214 for ( const script of this . scripts ) {
200- if ( script . version === ( ver + 1 ) ) {
215+ if ( script . version === ( version + 1 ) ) {
201216 return script ;
202217 }
203218 }
@@ -208,9 +223,12 @@ export class Migration {
208223 * Returns next downgrade script object
209224 */
210225 private async getNextDowngradeScript ( ) : Promise < MigrationScript > {
211- const ver = await this . getLastVersion ( ) ;
226+ const { version , fileName } = await this . getLastVersion ( ) ;
212227 for ( const script of this . scripts ) {
213- if ( script . version === ( ver ) ) {
228+ if ( script . version === version ) {
229+ if ( script . fileName !== fileName ) {
230+ throw new Error ( `Script version and filename mismatch! version: ${ version } -> file system: ${ script . fileName } | database: ${ fileName } ` ) ;
231+ }
214232 return script ;
215233 }
216234 }
@@ -269,8 +287,8 @@ export class Migration {
269287 * Sort migration files by prefix numbers.
270288 */
271289 private sortFiles ( a : string , b : string ) : number {
272- const verA = a . match ( / ^ ( \d * ) - / ) [ 1 ] ;
273- const verB = b . match ( / ^ ( \d * ) - / ) [ 1 ] ;
290+ const verA = a . match ( / ^ ( \d * ) - / ) [ 1 ] ;
291+ const verB = b . match ( / ^ ( \d * ) - / ) [ 1 ] ;
274292
275293 if ( ! parseInt ( verA ) || ! parseInt ( verB ) ) {
276294 return 0 ;
0 commit comments