in api/v1/src/admin/dataManager.js [187:294]
async function setupDatasharePrerequisites(projectId) {
const bigqueryUtil = new BigQueryUtil(projectId);
const viewOptions = {
labels: { [cfg.googPackagedSolutionKey] : cfg.googPackagedSolutionValue }
};
if (await bigqueryUtil.datasetExists(cfg.cdsDatasetId) === false) {
console.log('Creating datashare dataset');
let labels = {
[cfg.cdsMetadataLabelKey]: true,
[cfg.googPackagedSolutionKey] : cfg.googPackagedSolutionValue
};
const options = { description: 'Datashare Master Dataset', labels: labels };
await bigqueryUtil.createDataset(cfg.cdsDatasetId, options);
} else {
console.log('Datashare dataset already exists');
// Update existing dataset to add the label
// This can be split out later to an update process if we need to make bigger changes
await bigqueryUtil.setDatasetLabel(cfg.cdsDatasetId, cfg.cdsMetadataLabelKey, true);
await bigqueryUtil.setDatasetLabel(cfg.cdsDatasetId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue);
}
if (await bigqueryUtil.tableExists(cfg.cdsDatasetId, cfg.cdsPolicyTableId) === false) {
console.log("Creating policy table");
const options = Object.assign(viewOptions, require('./bq/schema/policy.json'));
await bigqueryUtil.createTable(cfg.cdsDatasetId, cfg.cdsPolicyTableId, options);
} else {
// Patch the schema
const options = require('./bq/schema/policy.json');
await bigqueryUtil.patchTableSchema(cfg.cdsDatasetId, cfg.cdsPolicyTableId, options.schema)
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsPolicyTableId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Policy table already exists');
}
if (await bigqueryUtil.viewExists(cfg.cdsDatasetId, cfg.cdsPolicyViewId) === false) {
console.log("Creating latest policies view");
const sql = sqlReplacements(projectId, require('./bq/view/currentPolicy.sql'));
await bigqueryUtil.createView(cfg.cdsDatasetId, cfg.cdsPolicyViewId, sql, viewOptions);
} else {
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsPolicyViewId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Policies view already exists');
}
if (await bigqueryUtil.tableExists(cfg.cdsDatasetId, cfg.cdsAccountTableId) === false) {
console.log("Creating account table");
const options = Object.assign(viewOptions, require('./bq/schema/account.json'));
await bigqueryUtil.createTable(cfg.cdsDatasetId, cfg.cdsAccountTableId, options);
} else {
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsAccountTableId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Account table already exists');
}
if (await bigqueryUtil.viewExists(cfg.cdsDatasetId, cfg.cdsAccountViewId) === false) {
console.log("Creating latest account view");
const sql = sqlReplacements(projectId, require('./bq/view/currentAccount.sql'));
await bigqueryUtil.createView(cfg.cdsDatasetId, cfg.cdsAccountViewId, sql, viewOptions);
} else {
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsAccountViewId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Account view already exists');
}
if (await bigqueryUtil.tableExists(cfg.cdsDatasetId, cfg.cdsAuthorizedViewTableId) === false) {
console.log("Creating authorizedView table");
const options = Object.assign(viewOptions, require('./bq/schema/authorizedView.json'));
await bigqueryUtil.createTable(cfg.cdsDatasetId, cfg.cdsAuthorizedViewTableId, options);
} else {
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsAuthorizedViewTableId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Authorized view table already exists');
}
if (await bigqueryUtil.viewExists(cfg.cdsDatasetId, cfg.cdsAuthorizedViewViewId) === false) {
console.log("Creating latest authorizedView view");
const sql = sqlReplacements(projectId, require('./bq/view/currentAuthorizedView.sql'));
await bigqueryUtil.createView(cfg.cdsDatasetId, cfg.cdsAuthorizedViewViewId, sql, viewOptions);
} else {
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsAuthorizedViewViewId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Authorized view view already exists');
}
if (await bigqueryUtil.viewExists(cfg.cdsDatasetId, cfg.cdsCurrentUserPermissionViewId) === false) {
console.log("Creating latest currentUserPermission view");
const sql = sqlReplacements(projectId, require('./bq/view/currentUserPermission.sql'));
await bigqueryUtil.createView(cfg.cdsDatasetId, cfg.cdsCurrentUserPermissionViewId, sql, viewOptions);
} else {
await bigqueryUtil.setTableLabel(cfg.cdsDatasetId, cfg.cdsCurrentUserPermissionViewId, cfg.googPackagedSolutionKey, cfg.googPackagedSolutionValue)
console.log('Current user dataset view already exists');
}
console.log("Creating bigQueryPermissionDiff procedure");
const bigQueryPermissionDiffProcSql = sqlReplacements(projectId, require('./bq/procedure/bigQueryPermissionDiff.sql'));
await bigqueryUtil.executeQuerySync({
query: bigQueryPermissionDiffProcSql
});
console.log("Creating bucketPermissionDiff procedure");
const bucketPermissionDiffProcSql = sqlReplacements(projectId, require('./bq/procedure/bucketPermissionDiff.sql'));
await bigqueryUtil.executeQuerySync({
query: bucketPermissionDiffProcSql
});
console.log("Creating topicPermissionDiff procedure");
const topicPermissionDiffProcSql = sqlReplacements(projectId, require('./bq/procedure/topicPermissionDiff.sql'));
await bigqueryUtil.executeQuerySync({
query: topicPermissionDiffProcSql
});
}