-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlserver.js
More file actions
90 lines (79 loc) · 2.36 KB
/
Copy pathsqlserver.js
File metadata and controls
90 lines (79 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sql from 'mssql';
import { logger } from './logger.js';
export async function waitForSqlServer(config) {
const timeoutMs = config.waitTimeout * 1000;
const start = Date.now();
const sqlConfig = {
user: config.user,
password: config.password,
server: config.host,
port: config.port,
database: 'master',
options: {
encrypt: false,
trustServerCertificate: true,
connectTimeout: 5000,
},
};
logger.info('Waiting for SQL Server to accept connections...');
while (Date.now() - start < timeoutMs) {
try {
const pool = await sql.connect(sqlConfig);
await pool.close();
logger.info('SQL Server is ready.');
return;
} catch (err) {
logger.debug(`SQL Server not ready yet: ${err.message}`);
await new Promise((res) => setTimeout(res, 2000)); // wait 2s before retry
}
}
throw new Error(`Timeout waiting for SQL Server after ${config.waitTimeout} seconds.`);
}
export async function attachDatabase(config, dbName, mdfPath, ldfPath) {
const sqlConfig = {
user: config.user,
password: config.password,
server: config.host,
port: config.port,
database: 'master',
options: {
encrypt: false,
trustServerCertificate: true,
requestTimeout: 120000, // 2 minutes for attach operations
},
};
let pool;
try {
pool = await sql.connect(sqlConfig);
if (config.replace) {
// Check if DB exists
const checkResult = await pool
.request()
.input('dbName', sql.NVarChar, dbName)
.query('SELECT name FROM sys.databases WHERE name = @dbName');
if (checkResult.recordset.length > 0) {
logger.info(`Database ${dbName} exists. Replacing...`);
// Drop it safely
await pool.request().query(`
ALTER DATABASE [${dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [${dbName}];
`);
logger.debug(`Dropped existing database ${dbName}`);
}
}
logger.info(`Attaching database ${dbName}...`);
// Create database for attach
const query = `
CREATE DATABASE [${dbName}]
ON (FILENAME = '${mdfPath}'),
(FILENAME = '${ldfPath}')
FOR ATTACH;
`;
await pool.request().query(query);
logger.info(`Database ${dbName} attached successfully.`);
} finally {
if (pool) {
await pool.close();
}
}
}