| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 18:13:22 +02:00
										 |  |  | /* eslint-disable prefer-template */ | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | // Node.js core.
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Check if connection is valid | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 13:03:35 +02:00
										 |  |  | module.exports = scope => { | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // First, make sure the application we have access to
 | 
					
						
							|  |  |  |   // the migration generator.
 | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2018-07-11 16:23:14 +02:00
										 |  |  |     require.resolve(path.resolve(scope.rootPath, 'node_modules', 'strapi-hook-knex')); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-08-05 20:25:57 +02:00
										 |  |  |     console.error('Impossible to call the Knex migration tool.'); | 
					
						
							| 
									
										
										
										
											2018-07-11 16:23:14 +02:00
										 |  |  |     console.error('You can install it with `$ npm install strapi-hook-knex --save`.'); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Try to access the databases config and register connections
 | 
					
						
							|  |  |  |   // in the Knex query builder.
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     fs.accessSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'databases.json'), fs.F_OK | fs.R_OK); | 
					
						
							|  |  |  |   } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-08-05 20:25:57 +02:00
										 |  |  |     console.error('No `databases.json` file detected at `' + path.resolve(scope.rootPath, 'config', 'environments', scope.environment) + '`.'); | 
					
						
							|  |  |  |     console.error(err); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Save the connections and the current DB config.
 | 
					
						
							|  |  |  |   scope.connections = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'databases.json'))).connections; | 
					
						
							|  |  |  |   scope.dbConfig = _.merge(scope.connections[scope.connection], { | 
					
						
							|  |  |  |     migrations: { | 
					
						
							|  |  |  |       directory: path.resolve(scope.rootPath, 'data', 'migrations', scope.connection) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     seeds: { | 
					
						
							|  |  |  |       directory: path.resolve(scope.rootPath, 'data', 'seeds', scope.connection) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Make sure the specified connection exists in config.
 | 
					
						
							|  |  |  |   if (!_.has(scope.connections, scope.connection)) { | 
					
						
							| 
									
										
										
										
											2017-08-05 20:25:57 +02:00
										 |  |  |     console.error('No connection found for `' + scope.connection + '`.'); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Make sure the needed client is installed.
 | 
					
						
							| 
									
										
										
										
											2016-07-11 13:03:35 +02:00
										 |  |  |   _.forEach(scope.connections, config => { | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |       scope.db = require(path.resolve(scope.rootPath, 'node_modules', 'knex'))(scope.dbConfig); | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-08-05 20:25:57 +02:00
										 |  |  |       console.error('The client `' + config.client + '` is not installed.'); | 
					
						
							|  |  |  |       console.error(err); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |       process.exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; |