@sqlanvil/cli 1.1.1 → 1.2.0

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.
Files changed (2) hide show
  1. package/bundle.js +65 -7
  2. package/package.json +1 -1
package/bundle.js CHANGED
@@ -38608,7 +38608,7 @@ function collectEvaluationQueries(queryOrAction, concatenate, queryModifier = (q
38608
38608
  .filter(validationQuery => !!validationQuery.query);
38609
38609
  }
38610
38610
 
38611
- const version = "1.1.1";
38611
+ const version = "1.2.0";
38612
38612
  const dataformVersion = "3.0.59";
38613
38613
 
38614
38614
  async function build(compiledGraph, runConfig, dbadapter) {
@@ -38844,6 +38844,17 @@ class CompileChildProcess extends BaseWorker {
38844
38844
  }
38845
38845
  }
38846
38846
 
38847
+ var __rest = (undefined && undefined.__rest) || function (s, e) {
38848
+ var t = {};
38849
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
38850
+ t[p] = s[p];
38851
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
38852
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
38853
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
38854
+ t[p[i]] = s[p[i]];
38855
+ }
38856
+ return t;
38857
+ };
38847
38858
  const CREDENTIALS_FILENAME = ".df-credentials.json";
38848
38859
  function read(credentialsPath, warehouse = "bigquery") {
38849
38860
  var _a;
@@ -38857,22 +38868,36 @@ function read(credentialsPath, warehouse = "bigquery") {
38857
38868
  catch (e) {
38858
38869
  throw new Error(`Error reading credentials file: ${e.message}`);
38859
38870
  }
38871
+ const warehouseCredentials = __rest(credentialsAsJson, ["connections"]);
38860
38872
  const isPostgres = warehouse.toLowerCase() === "postgres" || warehouse.toLowerCase() === "supabase";
38861
38873
  if (isPostgres) {
38862
- const credentials = verifyObjectMatchesProto(sqlanvil.PostgresConnection, credentialsAsJson);
38874
+ const credentials = verifyObjectMatchesProto(sqlanvil.PostgresConnection, warehouseCredentials);
38863
38875
  if (!credentials.host) {
38864
38876
  throw new Error(`Error reading credentials file: the host field is required`);
38865
38877
  }
38866
38878
  return credentials;
38867
38879
  }
38868
38880
  else {
38869
- const credentials = verifyObjectMatchesProto(sqlanvil.BigQuery, credentialsAsJson);
38881
+ const credentials = verifyObjectMatchesProto(sqlanvil.BigQuery, warehouseCredentials);
38870
38882
  if (!((_a = Object.keys(credentials).find(key => key === "projectId")) === null || _a === void 0 ? void 0 : _a.length)) {
38871
38883
  throw new Error(`Error reading credentials file: the projectId field is required`);
38872
38884
  }
38873
38885
  return credentials;
38874
38886
  }
38875
38887
  }
38888
+ function readConnections(credentialsPath) {
38889
+ if (!fs__namespace$1.existsSync(credentialsPath)) {
38890
+ return {};
38891
+ }
38892
+ let credentialsAsJson;
38893
+ try {
38894
+ credentialsAsJson = JSON.parse(fs__namespace$1.readFileSync(credentialsPath, "utf8"));
38895
+ }
38896
+ catch (e) {
38897
+ throw new Error(`Error reading credentials file: ${e.message}`);
38898
+ }
38899
+ return credentialsAsJson.connections || {};
38900
+ }
38876
38901
  var TestResultStatus;
38877
38902
  (function (TestResultStatus) {
38878
38903
  TestResultStatus[TestResultStatus["SUCCESSFUL"] = 0] = "SUCCESSFUL";
@@ -39082,10 +39107,10 @@ function resolveConnection(projectDir, connectionName) {
39082
39107
  throw new Error(`Missing .df-credentials.json in ${projectDir}.`);
39083
39108
  }
39084
39109
  const allCreds = JSON.parse(fs__namespace.readFileSync(credsPath, "utf8"));
39085
- const credentials = allCreds[connectionName];
39110
+ const credentials = allCreds.connections && allCreds.connections[connectionName];
39086
39111
  if (!credentials) {
39087
39112
  throw new Error(`No credentials for connection "${connectionName}" in .df-credentials.json ` +
39088
- `(expected a top-level "${connectionName}" key).`);
39113
+ `(expected a "connections.${connectionName}" entry).`);
39089
39114
  }
39090
39115
  return { name: connectionName, definition, credentials };
39091
39116
  }
@@ -39183,6 +39208,31 @@ function introspectToSqlx(projectDir, connectionName, tableRef, options) {
39183
39208
  });
39184
39209
  }
39185
39210
 
39211
+ const CONNECTION_TOKEN = /\$\{SA_CONN:([^:}]+):([^}]+)\}/g;
39212
+ function substituteConnectionCredentials(statement, connections) {
39213
+ return statement.replace(CONNECTION_TOKEN, (_match, connectionName, field) => {
39214
+ const entry = connections[connectionName];
39215
+ if (!entry) {
39216
+ throw new Error(`Connection "${connectionName}" is referenced by an FDW bridge but has no entry under ` +
39217
+ `"connections" in .df-credentials.json. Add connections.${connectionName} with the ` +
39218
+ `source user/password.`);
39219
+ }
39220
+ const value = entry[field];
39221
+ if (value === undefined || value === null || value === "") {
39222
+ throw new Error(`Connection "${connectionName}" is missing "${field}" under "connections.${connectionName}" ` +
39223
+ `in .df-credentials.json.`);
39224
+ }
39225
+ return String(value).replace(/'/g, "''");
39226
+ });
39227
+ }
39228
+ function assertConnectionCredentialsAvailable(graph, connections) {
39229
+ (graph.actions || []).forEach(action => (action.tasks || []).forEach(task => {
39230
+ if (task.statement) {
39231
+ substituteConnectionCredentials(task.statement, connections);
39232
+ }
39233
+ }));
39234
+ }
39235
+
39186
39236
  function parseArgvFlags(argv) {
39187
39237
  const parsedArgv = {};
39188
39238
  const splitArgv = [];
@@ -39583,7 +39633,10 @@ class Runner {
39583
39633
  }
39584
39634
  else {
39585
39635
  try {
39586
- const { rows, metadata } = await retry(() => client.execute(task.statement, {
39636
+ const statement = this.executionOptions.connectionCredentials
39637
+ ? substituteConnectionCredentials(task.statement, this.executionOptions.connectionCredentials)
39638
+ : task.statement;
39639
+ const { rows, metadata } = await retry(() => client.execute(statement, {
39587
39640
  onCancel: handleCancel => this.eEmitter.on(CANCEL_EVENT, handleCancel),
39588
39641
  rowLimit: 1,
39589
39642
  bigquery: options.bigquery
@@ -42444,13 +42497,18 @@ function runCli() {
42444
42497
  print("No actions to run.\n");
42445
42498
  return 0;
42446
42499
  }
42500
+ const connectionCredentials = readConnections(getCredentialsPath(argv[projectDirOption.name], argv[credentialsOption.name]));
42501
+ assertConnectionCredentialsAvailable(executionGraph, connectionCredentials);
42447
42502
  if (argv[dryRunOptionName]) {
42448
42503
  print("Dry running (no changes to the warehouse will be applied)...");
42449
42504
  }
42450
42505
  else {
42451
42506
  print("Running...\n");
42452
42507
  }
42453
- const runner = run(dbadapter, executionGraph, { bigquery: bigqueryOptions });
42508
+ const runner = run(dbadapter, executionGraph, {
42509
+ bigquery: bigqueryOptions,
42510
+ connectionCredentials
42511
+ });
42454
42512
  process.on("SIGINT", () => {
42455
42513
  runner.cancel();
42456
42514
  });
package/package.json CHANGED
@@ -61,7 +61,7 @@
61
61
  "bin": {
62
62
  "sqlanvil": "bundle.js"
63
63
  },
64
- "version": "1.1.1",
64
+ "version": "1.2.0",
65
65
  "name": "@sqlanvil/cli",
66
66
  "description": "sqlanvil command line interface.",
67
67
  "main": "bundle.js"