Skip to content

Commit 21725f4

Browse files
committed
refactored some code
1 parent 4bfdf33 commit 21725f4

6 files changed

+60
-127
lines changed

pddev-nukedb.js

+1-40
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,7 @@ args.option('databaseName', 'The database you want to act on');
3939
masterPool = await new sql.ConnectionPool(sqlMasterConnectionOptions).connect();
4040
console.log('connected to master using instance ${c}...');
4141

42-
// **** drop database
43-
let result = await sqlService.runScript(
44-
masterPool,
45-
sqlService.scriptNames.dropDatabase,
46-
`attempting to drop database ${options.databaseName}...`,
47-
{databaseName: options.databaseName});
48-
49-
// **** create database
50-
result = await sqlService.runScript(
51-
masterPool,
52-
sqlService.scriptNames.createDatabase,
53-
`creating database ${options.databaseName}...`,
54-
{databaseName: options.databaseName});
55-
56-
// **** create local user if not exists
57-
result = await sqlService.runScript(
58-
masterPool,
59-
sqlService.scriptNames.createLocalUserIfNotExists,
60-
`ensuring localuser exists...`,
61-
{
62-
username: config.username, password: config.password
63-
});
64-
masterPool.close();
65-
66-
// **** initialize security
67-
dbPool = await new sql.ConnectionPool(sqlDbConnectionOptions).connect();
68-
console.log(`connected to ${options.databaseName}...`);
69-
70-
result = await sqlService.runScript(
71-
dbPool,
72-
sqlService.scriptNames.initSecurityForLocalUser,
73-
`initializing security for localuser on database ${options.databaseName}...`,
74-
{
75-
databaseName: options.databaseName,
76-
username: config.username,
77-
password: config.password,
78-
});
79-
80-
console.log(`${options.databaseName} dropped and created - DONE`);
81-
dbPool.close();
42+
await sqlService.nukeDb(masterPool, sqlDbConnectionOptions, {databaseName: options.databaseName, username: config.username, password: config.password});
8243
}
8344
catch (err) {
8445
console.log(err);

pddev-rebuilddb.js

+2-42
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ args.option('databaseName', 'The database you want to act on');
1414
// console.log(process.argv);
1515
const options = args.parse(process.argv);
1616

17-
//const sqlScripts = await sqlService.transformSqlScripts({ databaseName: options.databaseName }); //await require('./sqlScripts/sqlScriptService')({ databaseName: options.databaseName });
18-
1917
let masterPool = {};
2018
let dbPool = {};
2119

@@ -39,46 +37,8 @@ args.option('databaseName', 'The database you want to act on');
3937
masterPool = await new sql.ConnectionPool(sqlMasterConnectionOptions).connect();
4038
console.log('connected to master...');
4139

42-
// **** drop database
43-
let result = await sqlService.runScript(
44-
masterPool,
45-
sqlService.scriptNames.dropDatabase,
46-
`attempting to drop database ${options.databaseName}...`,
47-
{databaseName: options.databaseName});
48-
49-
// **** create database
50-
result = await sqlService.runScript(
51-
masterPool,
52-
sqlService.scriptNames.createDatabase,
53-
`creating database ${options.databaseName}...`,
54-
{databaseName: options.databaseName});
55-
56-
// **** create local user if not exists
57-
result = await sqlService.runScript(
58-
masterPool,
59-
sqlService.scriptNames.createLocalUserIfNotExists,
60-
`ensuring localuser exists...`,
61-
{
62-
username: config.username, password: config.password
63-
});
64-
masterPool.close();
65-
66-
// **** initialize security
67-
dbPool = await new sql.ConnectionPool(sqlDbConnectionOptions).connect();
68-
console.log(`connected to ${options.databaseName}...`);
69-
70-
result = await sqlService.runScript(
71-
dbPool,
72-
sqlService.scriptNames.initSecurityForLocalUser,
73-
`initializing security for localuser on database ${options.databaseName}...`,
74-
{
75-
databaseName: options.databaseName,
76-
username: config.username,
77-
password: config.password,
78-
});
79-
80-
console.log(`${options.databaseName} dropped and created - DONE`);
81-
dbPool.close();
40+
// nuke database
41+
await sqlService.nukeDb(masterPool, sqlDbConnectionOptions, {databaseName: options.databaseName, username: config.username, password: config.password});
8242

8343
console.log(`migrating sql in local sql folder...`);
8444
sqlMigrationService.migrateSql();

pddev-restoredb.js

+3-41
Original file line numberDiff line numberDiff line change
@@ -33,58 +33,20 @@ args.option('databaseName', 'The database you want to act on');
3333
connectionString: `Driver=SQL Server;Server=(local);Database=${options.databaseName};Trusted_Connection=true;`,
3434
};
3535

36-
let dbPool = {};
3736
masterPool = await new sql.ConnectionPool(sqlMasterConnectionOptions).connect();
3837
masterPool.config.requestTimeout = 300000;
3938
console.log('connected to master...');
4039

41-
// **** drop database
42-
let result = await sqlService.runScript(
43-
masterPool,
44-
sqlService.scriptNames.dropDatabase,
45-
`attempting to drop database ${options.databaseName}...`,
46-
{databaseName: options.databaseName});
47-
48-
// **** create database
49-
result = await sqlService.runScript(
50-
masterPool,
51-
sqlService.scriptNames.createDatabase,
52-
`creating database ${options.databaseName}...`,
53-
{databaseName: options.databaseName});
54-
55-
// **** create local user if not exists
56-
result = await sqlService.runScript(
57-
masterPool,
58-
sqlService.scriptNames.createLocalUserIfNotExists,
59-
`ensuring localuser exists...`,
60-
{
61-
username: config.username, password: config.password
62-
});
63-
64-
// **** initialize security
65-
dbPool = await new sql.ConnectionPool(sqlDbConnectionOptions).connect();
66-
console.log(`connected to ${options.databaseName}...`);
67-
68-
result = await sqlService.runScript(
69-
dbPool,
70-
sqlService.scriptNames.initSecurityForLocalUser,
71-
`initializing security for localuser on database ${options.databaseName}...`,
72-
{
73-
databaseName: options.databaseName,
74-
username: config.username,
75-
password: config.password,
76-
});
77-
78-
console.log(`${options.databaseName} dropped and created - DONE`);
79-
dbPool.close();
40+
// nuke database
41+
await sqlService.nukeDb(masterPool, sqlDbConnectionOptions, {databaseName: options.databaseName, username: config.username, password: config.password});
8042

8143
// **** restore database
8244
console.log(`attempting to restore database ${options.databaseName}...`);
8345
//const sqlFilePathResult = await masterPool.request().query(sqlScripts.getSqlFilePath);
8446
const sqlFilePathResult = await sqlService.runScript(masterPool, sqlService.scriptNames.getSqlFilePath);
8547
const sqlFilePath = sqlFilePathResult.recordsets[0] && sqlFilePathResult.recordsets[0][0] ? sqlFilePathResult.recordsets[0][0].sqlFilePath : null;
8648

87-
result = await sqlService.restoreDatabase(masterPool,{databaseName: options.databaseName, restoreFilePath: './sql/restore.bak', sqlFilePath});
49+
let result = await sqlService.restoreDatabase(masterPool,{databaseName: options.databaseName, restoreFilePath: './sql/restore.bak', sqlFilePath});
8850

8951
console.log(`${options.databaseName} restored - DONE`);
9052
masterPool.close();

readme.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Globally install pddev from npm...
88
npm install -g pddev
99
```
1010

11+
Globally install the following support packages from npm...
12+
```
13+
npm install -g postgrator, postgrator-cli, mssql
14+
```
15+
1116
Create a config.json file wherever your pddev got installed globally -
1217
usually C:\users\\<username>\AppData\Roaming\npm\node_modules\pddev
1318

@@ -56,7 +61,7 @@ pddev n -d databaseName
5661
****** The following require a sql folder relative to where the commands are invoked ******
5762

5863
### RestoreDb
59-
Restore will look for ./sql/restore.bak relative to where the command is invoked. It will restore the restore.bak into the named database. The name provided must match the name in the restore.bak file.
64+
Restore will perform a nuke, then it will look for ./sql/restore.bak relative to where the command is invoked. It will restore the restore.bak into the named database. The databaseName provided must match the filenames in the restore.bak file.
6065
```
6166
pddev-restoredb -d databaseName
6267
```
+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
IF NOT EXISTS (SELECT * from sys.sysusers WHERE name = '${username}')
1+
IF EXISTS (SELECT * from sys.sysusers WHERE name = 'localuser')
22
BEGIN
3-
create USER [${username}] from LOGIN [${username}]
4-
ALTER ROLE [db_owner] ADD MEMBER [${username}]
3+
DROP USER localuser
54
END
65

6+
create USER localuser from LOGIN localuser
7+
ALTER ROLE [db_owner] ADD MEMBER localuser
8+
79
--================================================================
810
PRINT '<<< ${databaseName} Security set >>>'
911
--================================================================

sql/sqlService.js

+43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const glob = require('glob');
22
const path = require('path');
33
const fs = require("fs-extra");
4+
const sql = require('mssql/msnodesqlv8');
45

56
class SqlService {
67
constructor() {
@@ -105,6 +106,48 @@ class SqlService {
105106
}
106107
return transformedTemplate;
107108
}
109+
110+
async nukeDb(masterPool, sqlDbConnectionOptions, data) {
111+
// **** drop database
112+
let result = await this.runScript(
113+
masterPool,
114+
this.scriptNames.dropDatabase,
115+
`attempting to drop database ${data.databaseName}...`,
116+
{databaseName: data.databaseName});
117+
118+
// **** create database
119+
result = await this.runScript(
120+
masterPool,
121+
this.scriptNames.createDatabase,
122+
`creating database ${data.databaseName}...`,
123+
{databaseName: data.databaseName});
124+
125+
// **** create local user if not exists
126+
result = await this.runScript(
127+
masterPool,
128+
this.scriptNames.createLocalUserIfNotExists,
129+
`ensuring localuser exists...`,
130+
{
131+
username: data.username, password: data.password
132+
});
133+
134+
// **** initialize security
135+
let dbPool = await new sql.ConnectionPool(sqlDbConnectionOptions).connect();
136+
console.log(`connected to ${data.databaseName}...`);
137+
138+
result = await this.runScript(
139+
dbPool,
140+
this.scriptNames.initSecurityForLocalUser,
141+
`initializing security for localuser on database ${data.databaseName}...`,
142+
{
143+
databaseName: data.databaseName,
144+
username: data.username,
145+
password: data.password,
146+
});
147+
148+
console.log(`${data.databaseName} dropped and created - DONE`);
149+
dbPool.close();
150+
}
108151
}
109152

110153

0 commit comments

Comments
 (0)