| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const backupService = require('./backup'); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | const sqlInit = require('./sql_init'); | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const optionService = require('./options'); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | const fs = require('fs-extra'); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const resourceDir = require('./resource_dir'); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function migrate() { | 
					
						
							|  |  |  |     const migrations = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  |     // backup before attempting migration
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     await backupService.backupNow(); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:47:46 -04:00
										 |  |  |     const currentDbVersion = parseInt(await optionService.getOption('dbVersion')); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     fs.readdirSync(resourceDir.MIGRATIONS_DIR).forEach(file => { | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |         const match = file.match(/([0-9]{4})__([a-zA-Z0-9_ ]+)\.(sql|js)/); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (match) { | 
					
						
							|  |  |  |             const dbVersion = parseInt(match[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (dbVersion > currentDbVersion) { | 
					
						
							|  |  |  |                 const name = match[2]; | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |                 const type = match[3]; | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 const migrationRecord = { | 
					
						
							| 
									
										
										
										
											2017-10-22 20:29:31 -04:00
										 |  |  |                     dbVersion: dbVersion, | 
					
						
							|  |  |  |                     name: name, | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |                     file: file, | 
					
						
							|  |  |  |                     type: type | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 migrations.push(migrationRecord); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 18:59:58 -05:00
										 |  |  |     migrations.sort((a, b) => a.dbVersion - b.dbVersion); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const mig of migrations) { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |             log.info("Attempting migration to version " + mig.dbVersion); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 22:30:17 -05:00
										 |  |  |             // needs to happen outside of the transaction (otherwise it's a NO-OP)
 | 
					
						
							|  |  |  |             await sql.execute("PRAGMA foreign_keys = OFF"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |             await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |                 if (mig.type === 'sql') { | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |                     const migrationSql = fs.readFileSync(resourceDir.MIGRATIONS_DIR + "/" + mig.file).toString('utf8'); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     console.log("Migration with SQL script: " + migrationSql); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |                     await sql.executeScript(migrationSql); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else if (mig.type === 'js') { | 
					
						
							|  |  |  |                     console.log("Migration with JS module"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |                     const migrationModule = require("../" + resourceDir.MIGRATIONS_DIR + "/" + mig.file); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |                     await migrationModule(db); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2017-12-06 20:58:59 -05:00
										 |  |  |                     throw new Error("Unknown migration type " + mig.type); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:47:46 -04:00
										 |  |  |                 await optionService.setOption("dbVersion", mig.dbVersion); | 
					
						
							| 
									
										
										
										
											2017-12-20 22:30:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  |             log.info("Migration to version " + mig.dbVersion + " has been successful."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  |             mig['success'] = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch (e) { | 
					
						
							|  |  |  |             mig['success'] = false; | 
					
						
							|  |  |  |             mig['error'] = e.stack; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  |             log.error("error during migration to version " + mig.dbVersion + ": " + e.stack); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-20 22:30:17 -05:00
										 |  |  |         finally { | 
					
						
							|  |  |  |             // make sure foreign keys are enabled even if migration script disables them
 | 
					
						
							|  |  |  |             await sql.execute("PRAGMA foreign_keys = ON"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  |     if (sqlInit.isDbUpToDate()) { | 
					
						
							| 
									
										
										
										
											2018-07-22 19:56:20 +02:00
										 |  |  |         await sqlInit.initDbConnection(); | 
					
						
							| 
									
										
										
										
											2017-12-12 23:35:41 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  |     return migrations; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     migrate | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | }; |