|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | +const sql = require('mssql/msnodesqlv8'); |
| 4 | +const args = require('args'); |
| 5 | +const SqlService = require('./sql/sqlService'); |
| 6 | +const SqlMigrationService = require('./sqlMigrationService'); |
| 7 | +const config = require('./config.json'); |
| 8 | + |
| 9 | +args.option('databaseName', 'The database you want to act on'); |
| 10 | + |
| 11 | +(async () => { |
| 12 | + const sqlService = new SqlService(); |
| 13 | + const sqlMigrationService = new SqlMigrationService(); |
| 14 | + // console.log(process.argv); |
| 15 | + const options = args.parse(process.argv); |
| 16 | + |
| 17 | + //const sqlScripts = await sqlService.transformSqlScripts({ databaseName: options.databaseName }); //await require('./sqlScripts/sqlScriptService')({ databaseName: options.databaseName }); |
| 18 | + |
| 19 | + let masterPool = {}; |
| 20 | + let dbPool = {}; |
| 21 | + |
| 22 | + try { |
| 23 | + const sqlMasterConnectionOptions = { |
| 24 | + driver: 'msnodesqlv8', |
| 25 | + server: '(local)', |
| 26 | + database: 'master', |
| 27 | + options: { |
| 28 | + trustedConnection: true |
| 29 | + } |
| 30 | + }; |
| 31 | + const sqlDbConnectionOptions = { |
| 32 | + connectionString: `Driver=SQL Server;Server=(local);Database=${options.databaseName};Trusted_Connection=true;`, |
| 33 | + }; |
| 34 | + |
| 35 | + masterPool = await new sql.ConnectionPool(sqlMasterConnectionOptions).connect(); |
| 36 | + console.log('connected to master...'); |
| 37 | + |
| 38 | + // **** drop database |
| 39 | + let result = await sqlService.runScript( |
| 40 | + masterPool, |
| 41 | + sqlService.scriptNames.dropDatabase, |
| 42 | + `attempting to drop database ${options.databaseName}...`, |
| 43 | + {databaseName: options.databaseName}); |
| 44 | + |
| 45 | + // **** create database |
| 46 | + result = await sqlService.runScript( |
| 47 | + masterPool, |
| 48 | + sqlService.scriptNames.createDatabase, |
| 49 | + `creating database ${options.databaseName}...`, |
| 50 | + {databaseName: options.databaseName}); |
| 51 | + |
| 52 | + // **** create local user if not exists |
| 53 | + result = await sqlService.runScript( |
| 54 | + masterPool, |
| 55 | + sqlService.scriptNames.createLocalUserIfNotExists, |
| 56 | + `ensuring localuser exists...`, |
| 57 | + { |
| 58 | + username: config.username, password: config.password |
| 59 | + }); |
| 60 | + masterPool.close(); |
| 61 | + |
| 62 | + // **** initialize security |
| 63 | + dbPool = await new sql.ConnectionPool(sqlDbConnectionOptions).connect(); |
| 64 | + console.log(`connected to ${options.databaseName}...`); |
| 65 | + |
| 66 | + result = await sqlService.runScript( |
| 67 | + dbPool, |
| 68 | + sqlService.scriptNames.initSecurityForLocalUser, |
| 69 | + `initializing security for localuser on database ${options.databaseName}...`, |
| 70 | + { |
| 71 | + databaseName: options.databaseName, |
| 72 | + username: config.username, |
| 73 | + password: config.password, |
| 74 | + }); |
| 75 | + |
| 76 | + console.log(`${options.databaseName} dropped and created - DONE`); |
| 77 | + dbPool.close(); |
| 78 | + } |
| 79 | + catch (err) { |
| 80 | + console.log(err); |
| 81 | + } |
| 82 | + finally { |
| 83 | + if (masterPool) { |
| 84 | + masterPool.close(); |
| 85 | + } |
| 86 | + if (dbPool) { |
| 87 | + dbPool.close(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +})(); |
0 commit comments