-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-schema.js
48 lines (40 loc) · 1.41 KB
/
create-schema.js
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
const fs = require("fs");
const path = require("path");
// Replace with the actual path to your schema file
const inputFilePath = path.join(__dirname, "schema.sql.example");
const outputFilePath = path.join(__dirname, "schema.sql");
// Get the cluster name from the command-line arguments
const args = process.argv.slice(2);
const clusterName = args[0]; // First argument after the script name
if (!clusterName) {
console.error(
"Environment variable 'CLUSTER_NAME' is not set, Cluster and Replicated will be remove",
);
}
// Read the schema file
fs.readFile(inputFilePath, "utf8", (err, data) => {
if (err) {
console.error(`Error reading file: ${err}`);
process.exit(1);
}
let modifiedData;
if (clusterName) {
// Replace the cluster name in the schema
modifiedData = data.replace(
/ON CLUSTER\s+\w+/g,
`ON CLUSTER ${clusterName}`,
);
console.log(`Cluster name updated to: ${clusterName}`);
} else {
modifiedData = data.replace(/ON CLUSTER\s+\w+/g, "");
modifiedData = modifiedData.replace(/Replicated/g, "");
}
// Write the modified schema to a new file
fs.writeFile(outputFilePath, modifiedData, "utf8", (err) => {
if (err) {
console.error(`Error writing file: ${err}`);
process.exit(1);
}
console.log("Schema updated successfully!");
});
});