@pittica/google-business-intelligence-helpers 1.15.1 → 1.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/contents/backup.js
CHANGED
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
15
|
const {
|
|
16
|
-
getBucket
|
|
16
|
+
getBucket,
|
|
17
|
+
groupJson
|
|
17
18
|
} = require("@pittica/google-cloud-storage-helpers");
|
|
18
19
|
const {
|
|
19
20
|
getDataset,
|
|
20
|
-
jobDone
|
|
21
|
+
jobDone,
|
|
22
|
+
getTable
|
|
21
23
|
} = require("@pittica/google-bigquery-helpers");
|
|
22
24
|
const log = require("@pittica/logger-helpers");
|
|
23
25
|
const {
|
|
@@ -39,7 +41,7 @@ exports.backup = async (id, bucket, projectId, dateFormat = "YYYY-MM-DD") => {
|
|
|
39
41
|
});
|
|
40
42
|
const [tables] = await dataset.getTables();
|
|
41
43
|
const date = format(new Date(), dateFormat);
|
|
42
|
-
return await exports.extract(bucket, date, tables, 0);
|
|
44
|
+
return await exports.extract(bucket, date, tables, 0, dataset.metadata.location);
|
|
43
45
|
};
|
|
44
46
|
|
|
45
47
|
/**
|
|
@@ -49,20 +51,21 @@ exports.backup = async (id, bucket, projectId, dateFormat = "YYYY-MM-DD") => {
|
|
|
49
51
|
* @param {string} date Date in format YYYY-MM-DD.
|
|
50
52
|
* @param {Array} tables An array of BigQuery tables.
|
|
51
53
|
* @param {number} index Array index.
|
|
54
|
+
* @param {string} location Location of the BigQuery dataset.
|
|
52
55
|
* @returns {boolean} A value indicating whehter the job has been done.
|
|
53
56
|
*/
|
|
54
|
-
exports.extract = async (bucket, date, tables, index) => {
|
|
57
|
+
exports.extract = async (bucket, date, tables, index = 0, location = "us-central1") => {
|
|
55
58
|
if (index < tables.length) {
|
|
56
59
|
const table = tables[index];
|
|
57
60
|
return await table.extract(getBucket(bucket).file(`${date}/${table.id}-*.json`), {
|
|
58
|
-
location
|
|
61
|
+
location,
|
|
59
62
|
format: "json",
|
|
60
63
|
gzip: false
|
|
61
64
|
}).then(async response => {
|
|
62
65
|
if (jobDone(response)) {
|
|
63
66
|
const [job] = response;
|
|
64
67
|
log.success(`Job backup "${job.id}" for table "${table.id}" done.`);
|
|
65
|
-
return await exports.extract(date, tables, index + 1);
|
|
68
|
+
return await exports.extract(bucket, date, tables, index + 1, location);
|
|
66
69
|
} else {
|
|
67
70
|
return false;
|
|
68
71
|
}
|
|
@@ -88,7 +91,7 @@ exports.rollback = async (date, id, projectId, sourceBucket, destinationDataset)
|
|
|
88
91
|
const dataset = await getDataset(id, {
|
|
89
92
|
projectId
|
|
90
93
|
});
|
|
91
|
-
const bucket = getBucket(sourceBucket);
|
|
94
|
+
const bucket = getBucket(sourceBucket, projectId);
|
|
92
95
|
const [response] = await bucket.getFiles({
|
|
93
96
|
prefix: `${date}/`
|
|
94
97
|
});
|
|
@@ -96,16 +99,19 @@ exports.rollback = async (date, id, projectId, sourceBucket, destinationDataset)
|
|
|
96
99
|
log.info(`Begin rollback "${date}".`);
|
|
97
100
|
Object.keys(files).forEach(async key => {
|
|
98
101
|
const [table] = await dataset.table(key).get();
|
|
99
|
-
files[key].forEach(filename =>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
files[key].forEach(async filename => {
|
|
103
|
+
const destination = await getTable(destinationDataset, key, {
|
|
104
|
+
projectId,
|
|
105
|
+
location: dataset.metadata.location
|
|
106
|
+
});
|
|
107
|
+
await destination.load(bucket.file(`${date}/${filename}`), {
|
|
108
|
+
sourceFormat: "NEWLINE_DELIMITED_JSON",
|
|
109
|
+
schema: table.metadata.schema,
|
|
110
|
+
timePartitioning: table.metadata.timePartitioning,
|
|
111
|
+
location: table.metadata.location,
|
|
112
|
+
tableConstraints: table.metadata.tableConstraints,
|
|
113
|
+
partitionDefinition: table.metadata.partitionDefinition
|
|
114
|
+
});
|
|
115
|
+
});
|
|
110
116
|
});
|
|
111
117
|
};
|
package/dist/contents/storage.js
CHANGED
|
@@ -169,9 +169,9 @@ exports.erase = async (bucket, days = process.env.BACKUP_KEEP_DAYS || 3) => {
|
|
|
169
169
|
});
|
|
170
170
|
const folders = Object.keys(result).sort();
|
|
171
171
|
const blacklist = folders.slice(0, folders.length - parseInt(days));
|
|
172
|
-
blacklist.
|
|
172
|
+
await Promise.all(blacklist.map(async prefix => await b.deleteFiles({
|
|
173
173
|
prefix
|
|
174
|
-
}));
|
|
174
|
+
})));
|
|
175
175
|
log.success("Backup bucket erased.");
|
|
176
176
|
return true;
|
|
177
177
|
};
|
package/dist/scripts/storage.js
CHANGED
|
@@ -36,12 +36,12 @@ exports.scriptUnlistened = async (bucket, folder) => {
|
|
|
36
36
|
const storage = getStorage();
|
|
37
37
|
const [files] = await getFiles(storage, bucket);
|
|
38
38
|
if (files) {
|
|
39
|
-
files.forEach(file => {
|
|
39
|
+
files.forEach(async file => {
|
|
40
40
|
const {
|
|
41
41
|
name
|
|
42
42
|
} = splitName(file.id);
|
|
43
43
|
if (!hasSchema(name, folder)) {
|
|
44
|
-
deleteFile(file);
|
|
44
|
+
await deleteFile(file);
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pittica/google-business-intelligence-helpers",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.15.
|
|
4
|
+
"version": "1.15.2",
|
|
5
5
|
"description": "Helpers for business intelligence for Google Cloud.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"node": ">=24.11.0"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@pittica/google-bigquery-helpers": "^1.
|
|
40
|
-
"@pittica/google-cloud-storage-helpers": "^1.
|
|
39
|
+
"@pittica/google-bigquery-helpers": "^1.7.0",
|
|
40
|
+
"@pittica/google-cloud-storage-helpers": "^1.6.0",
|
|
41
41
|
"@pittica/logger-helpers": "^1.2.2",
|
|
42
42
|
"date-and-time": "^4.7.0"
|
|
43
43
|
},
|