@sqlanvil/cli 1.1.2 → 1.3.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 (3) hide show
  1. package/bundle.js +104 -12
  2. package/package.json +1 -1
  3. package/worker_bundle.js +28 -0
package/bundle.js CHANGED
@@ -12722,6 +12722,7 @@ const sqlanvil = $root.sqlanvil = (() => {
12722
12722
  * @interface IBound
12723
12723
  * @property {string|null} [name] Bound name
12724
12724
  * @property {string|null} [values] Bound values
12725
+ * @property {sqlanvil.PostgresOptions.IPartition|null} [subPartition] Bound subPartition
12725
12726
  */
12726
12727
 
12727
12728
  /**
@@ -12755,6 +12756,14 @@ const sqlanvil = $root.sqlanvil = (() => {
12755
12756
  */
12756
12757
  Bound.prototype.values = "";
12757
12758
 
12759
+ /**
12760
+ * Bound subPartition.
12761
+ * @member {sqlanvil.PostgresOptions.IPartition|null|undefined} subPartition
12762
+ * @memberof sqlanvil.PostgresOptions.Partition.Bound
12763
+ * @instance
12764
+ */
12765
+ Bound.prototype.subPartition = null;
12766
+
12758
12767
  /**
12759
12768
  * Creates a new Bound instance using the specified properties.
12760
12769
  * @function create
@@ -12783,6 +12792,8 @@ const sqlanvil = $root.sqlanvil = (() => {
12783
12792
  writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
12784
12793
  if (message.values != null && Object.hasOwnProperty.call(message, "values"))
12785
12794
  writer.uint32(/* id 2, wireType 2 =*/18).string(message.values);
12795
+ if (message.subPartition != null && Object.hasOwnProperty.call(message, "subPartition"))
12796
+ $root.sqlanvil.PostgresOptions.Partition.encode(message.subPartition, writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
12786
12797
  return writer;
12787
12798
  };
12788
12799
 
@@ -12827,6 +12838,10 @@ const sqlanvil = $root.sqlanvil = (() => {
12827
12838
  message.values = reader.string();
12828
12839
  break;
12829
12840
  }
12841
+ case 3: {
12842
+ message.subPartition = $root.sqlanvil.PostgresOptions.Partition.decode(reader, reader.uint32());
12843
+ break;
12844
+ }
12830
12845
  default:
12831
12846
  reader.skipType(tag & 7);
12832
12847
  break;
@@ -12868,6 +12883,11 @@ const sqlanvil = $root.sqlanvil = (() => {
12868
12883
  if (message.values != null && message.hasOwnProperty("values"))
12869
12884
  if (!$util.isString(message.values))
12870
12885
  return "values: string expected";
12886
+ if (message.subPartition != null && message.hasOwnProperty("subPartition")) {
12887
+ let error = $root.sqlanvil.PostgresOptions.Partition.verify(message.subPartition);
12888
+ if (error)
12889
+ return "subPartition." + error;
12890
+ }
12871
12891
  return null;
12872
12892
  };
12873
12893
 
@@ -12887,6 +12907,11 @@ const sqlanvil = $root.sqlanvil = (() => {
12887
12907
  message.name = String(object.name);
12888
12908
  if (object.values != null)
12889
12909
  message.values = String(object.values);
12910
+ if (object.subPartition != null) {
12911
+ if (typeof object.subPartition !== "object")
12912
+ throw TypeError(".sqlanvil.PostgresOptions.Partition.Bound.subPartition: object expected");
12913
+ message.subPartition = $root.sqlanvil.PostgresOptions.Partition.fromObject(object.subPartition);
12914
+ }
12890
12915
  return message;
12891
12916
  };
12892
12917
 
@@ -12906,11 +12931,14 @@ const sqlanvil = $root.sqlanvil = (() => {
12906
12931
  if (options.defaults) {
12907
12932
  object.name = "";
12908
12933
  object.values = "";
12934
+ object.subPartition = null;
12909
12935
  }
12910
12936
  if (message.name != null && message.hasOwnProperty("name"))
12911
12937
  object.name = message.name;
12912
12938
  if (message.values != null && message.hasOwnProperty("values"))
12913
12939
  object.values = message.values;
12940
+ if (message.subPartition != null && message.hasOwnProperty("subPartition"))
12941
+ object.subPartition = $root.sqlanvil.PostgresOptions.Partition.toObject(message.subPartition, options);
12914
12942
  return object;
12915
12943
  };
12916
12944
 
@@ -38415,27 +38443,44 @@ class PostgresExecutionSql {
38415
38443
  }
38416
38444
  }
38417
38445
  createPartitionedTableTasks(table) {
38446
+ var _a;
38418
38447
  const partition = table.postgres.partition;
38419
38448
  const target = this.resolveTarget(table.target);
38420
38449
  const stage = this.resolveTarget(Object.assign(Object.assign({}, table.target), { name: `${table.target.name}__sa_stage` }));
38421
38450
  const kind = this.partitionKindAsSql(partition.kind);
38422
38451
  const columns = (partition.columns || []).map(c => `"${c}"`).join(", ");
38452
+ const tablespace = ((_a = table.postgres) === null || _a === void 0 ? void 0 : _a.tablespace) ? ` tablespace "${table.postgres.tablespace}"` : "";
38423
38453
  const statements = [
38424
38454
  `drop table if exists ${stage} cascade`,
38425
38455
  `create unlogged table ${stage} as ${table.query} with no data`,
38426
38456
  `drop table if exists ${target} cascade`,
38427
- `create table ${target} (like ${stage} including defaults) partition by ${kind} (${columns})`
38457
+ `create table ${target} (like ${stage} including defaults) partition by ${kind} (${columns})${tablespace}`
38428
38458
  ];
38459
+ statements.push(...this.createChildPartitions(table.target, table.target.name, target, partition));
38460
+ statements.push(`insert into ${target} select * from (${table.query}) as q`);
38461
+ statements.push(`drop table if exists ${stage} cascade`);
38462
+ return statements;
38463
+ }
38464
+ createChildPartitions(baseTarget, parentName, parentSql, partition) {
38465
+ const statements = [];
38429
38466
  for (const bound of partition.partitions || []) {
38430
- const child = this.resolveTarget(Object.assign(Object.assign({}, table.target), { name: `${table.target.name}__${bound.name}` }));
38431
- statements.push(`create table ${child} partition of ${target} for values ${bound.values}`);
38467
+ const childName = `${parentName}__${bound.name}`;
38468
+ const childSql = this.resolveTarget(Object.assign(Object.assign({}, baseTarget), { name: childName }));
38469
+ const sub = bound.subPartition;
38470
+ if (sub && (sub.columns || []).length > 0) {
38471
+ const subKind = this.partitionKindAsSql(sub.kind);
38472
+ const subColumns = (sub.columns || []).map(c => `"${c}"`).join(", ");
38473
+ statements.push(`create table ${childSql} partition of ${parentSql} for values ${bound.values} partition by ${subKind} (${subColumns})`);
38474
+ statements.push(...this.createChildPartitions(baseTarget, childName, childSql, sub));
38475
+ }
38476
+ else {
38477
+ statements.push(`create table ${childSql} partition of ${parentSql} for values ${bound.values}`);
38478
+ }
38432
38479
  }
38433
38480
  if (partition.includeDefault) {
38434
- const def = this.resolveTarget(Object.assign(Object.assign({}, table.target), { name: `${table.target.name}__default` }));
38435
- statements.push(`create table ${def} partition of ${target} default`);
38481
+ const def = this.resolveTarget(Object.assign(Object.assign({}, baseTarget), { name: `${parentName}__default` }));
38482
+ statements.push(`create table ${def} partition of ${parentSql} default`);
38436
38483
  }
38437
- statements.push(`insert into ${target} select * from (${table.query}) as q`);
38438
- statements.push(`drop table if exists ${stage} cascade`);
38439
38484
  return statements;
38440
38485
  }
38441
38486
  partitionKindAsSql(kind) {
@@ -38608,7 +38653,7 @@ function collectEvaluationQueries(queryOrAction, concatenate, queryModifier = (q
38608
38653
  .filter(validationQuery => !!validationQuery.query);
38609
38654
  }
38610
38655
 
38611
- const version = "1.1.2";
38656
+ const version = "1.3.0";
38612
38657
  const dataformVersion = "3.0.59";
38613
38658
 
38614
38659
  async function build(compiledGraph, runConfig, dbadapter) {
@@ -38885,6 +38930,19 @@ function read(credentialsPath, warehouse = "bigquery") {
38885
38930
  return credentials;
38886
38931
  }
38887
38932
  }
38933
+ function readConnections(credentialsPath) {
38934
+ if (!fs__namespace$1.existsSync(credentialsPath)) {
38935
+ return {};
38936
+ }
38937
+ let credentialsAsJson;
38938
+ try {
38939
+ credentialsAsJson = JSON.parse(fs__namespace$1.readFileSync(credentialsPath, "utf8"));
38940
+ }
38941
+ catch (e) {
38942
+ throw new Error(`Error reading credentials file: ${e.message}`);
38943
+ }
38944
+ return credentialsAsJson.connections || {};
38945
+ }
38888
38946
  var TestResultStatus;
38889
38947
  (function (TestResultStatus) {
38890
38948
  TestResultStatus[TestResultStatus["SUCCESSFUL"] = 0] = "SUCCESSFUL";
@@ -39195,6 +39253,31 @@ function introspectToSqlx(projectDir, connectionName, tableRef, options) {
39195
39253
  });
39196
39254
  }
39197
39255
 
39256
+ const CONNECTION_TOKEN = /\$\{SA_CONN:([^:}]+):([^}]+)\}/g;
39257
+ function substituteConnectionCredentials(statement, connections) {
39258
+ return statement.replace(CONNECTION_TOKEN, (_match, connectionName, field) => {
39259
+ const entry = connections[connectionName];
39260
+ if (!entry) {
39261
+ throw new Error(`Connection "${connectionName}" is referenced by an FDW bridge but has no entry under ` +
39262
+ `"connections" in .df-credentials.json. Add connections.${connectionName} with the ` +
39263
+ `source user/password.`);
39264
+ }
39265
+ const value = entry[field];
39266
+ if (value === undefined || value === null || value === "") {
39267
+ throw new Error(`Connection "${connectionName}" is missing "${field}" under "connections.${connectionName}" ` +
39268
+ `in .df-credentials.json.`);
39269
+ }
39270
+ return String(value).replace(/'/g, "''");
39271
+ });
39272
+ }
39273
+ function assertConnectionCredentialsAvailable(graph, connections) {
39274
+ (graph.actions || []).forEach(action => (action.tasks || []).forEach(task => {
39275
+ if (task.statement) {
39276
+ substituteConnectionCredentials(task.statement, connections);
39277
+ }
39278
+ }));
39279
+ }
39280
+
39198
39281
  function parseArgvFlags(argv) {
39199
39282
  const parsedArgv = {};
39200
39283
  const splitArgv = [];
@@ -39595,7 +39678,10 @@ class Runner {
39595
39678
  }
39596
39679
  else {
39597
39680
  try {
39598
- const { rows, metadata } = await retry(() => client.execute(task.statement, {
39681
+ const statement = this.executionOptions.connectionCredentials
39682
+ ? substituteConnectionCredentials(task.statement, this.executionOptions.connectionCredentials)
39683
+ : task.statement;
39684
+ const { rows, metadata } = await retry(() => client.execute(statement, {
39599
39685
  onCancel: handleCancel => this.eEmitter.on(CANCEL_EVENT, handleCancel),
39600
39686
  rowLimit: 1,
39601
39687
  bigquery: options.bigquery
@@ -39652,12 +39738,13 @@ async function test(dbadapter, tests) {
39652
39738
  return await Promise.all(tests.map(testCase => runTest(dbadapter, testCase)));
39653
39739
  }
39654
39740
  async function runTest(dbadapter, testCase) {
39741
+ const noLimit = {};
39655
39742
  let actualResults;
39656
39743
  let expectedResults;
39657
39744
  try {
39658
39745
  [actualResults, expectedResults] = await Promise.all([
39659
- dbadapter.execute(testCase.testQuery, { byteLimit: 1024 * 1024 }),
39660
- dbadapter.execute(testCase.expectedOutputQuery, { byteLimit: 1024 * 1024 })
39746
+ dbadapter.execute(testCase.testQuery, noLimit),
39747
+ dbadapter.execute(testCase.expectedOutputQuery, noLimit)
39661
39748
  ]);
39662
39749
  }
39663
39750
  catch (e) {
@@ -42456,13 +42543,18 @@ function runCli() {
42456
42543
  print("No actions to run.\n");
42457
42544
  return 0;
42458
42545
  }
42546
+ const connectionCredentials = readConnections(getCredentialsPath(argv[projectDirOption.name], argv[credentialsOption.name]));
42547
+ assertConnectionCredentialsAvailable(executionGraph, connectionCredentials);
42459
42548
  if (argv[dryRunOptionName]) {
42460
42549
  print("Dry running (no changes to the warehouse will be applied)...");
42461
42550
  }
42462
42551
  else {
42463
42552
  print("Running...\n");
42464
42553
  }
42465
- const runner = run(dbadapter, executionGraph, { bigquery: bigqueryOptions });
42554
+ const runner = run(dbadapter, executionGraph, {
42555
+ bigquery: bigqueryOptions,
42556
+ connectionCredentials
42557
+ });
42466
42558
  process.on("SIGINT", () => {
42467
42559
  runner.cancel();
42468
42560
  });
package/package.json CHANGED
@@ -61,7 +61,7 @@
61
61
  "bin": {
62
62
  "sqlanvil": "bundle.js"
63
63
  },
64
- "version": "1.1.2",
64
+ "version": "1.3.0",
65
65
  "name": "@sqlanvil/cli",
66
66
  "description": "sqlanvil command line interface.",
67
67
  "main": "bundle.js"
package/worker_bundle.js CHANGED
@@ -12665,6 +12665,7 @@ const sqlanvil = $root.sqlanvil = (() => {
12665
12665
  * @interface IBound
12666
12666
  * @property {string|null} [name] Bound name
12667
12667
  * @property {string|null} [values] Bound values
12668
+ * @property {sqlanvil.PostgresOptions.IPartition|null} [subPartition] Bound subPartition
12668
12669
  */
12669
12670
 
12670
12671
  /**
@@ -12698,6 +12699,14 @@ const sqlanvil = $root.sqlanvil = (() => {
12698
12699
  */
12699
12700
  Bound.prototype.values = "";
12700
12701
 
12702
+ /**
12703
+ * Bound subPartition.
12704
+ * @member {sqlanvil.PostgresOptions.IPartition|null|undefined} subPartition
12705
+ * @memberof sqlanvil.PostgresOptions.Partition.Bound
12706
+ * @instance
12707
+ */
12708
+ Bound.prototype.subPartition = null;
12709
+
12701
12710
  /**
12702
12711
  * Creates a new Bound instance using the specified properties.
12703
12712
  * @function create
@@ -12726,6 +12735,8 @@ const sqlanvil = $root.sqlanvil = (() => {
12726
12735
  writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
12727
12736
  if (message.values != null && Object.hasOwnProperty.call(message, "values"))
12728
12737
  writer.uint32(/* id 2, wireType 2 =*/18).string(message.values);
12738
+ if (message.subPartition != null && Object.hasOwnProperty.call(message, "subPartition"))
12739
+ $root.sqlanvil.PostgresOptions.Partition.encode(message.subPartition, writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
12729
12740
  return writer;
12730
12741
  };
12731
12742
 
@@ -12770,6 +12781,10 @@ const sqlanvil = $root.sqlanvil = (() => {
12770
12781
  message.values = reader.string();
12771
12782
  break;
12772
12783
  }
12784
+ case 3: {
12785
+ message.subPartition = $root.sqlanvil.PostgresOptions.Partition.decode(reader, reader.uint32());
12786
+ break;
12787
+ }
12773
12788
  default:
12774
12789
  reader.skipType(tag & 7);
12775
12790
  break;
@@ -12811,6 +12826,11 @@ const sqlanvil = $root.sqlanvil = (() => {
12811
12826
  if (message.values != null && message.hasOwnProperty("values"))
12812
12827
  if (!$util.isString(message.values))
12813
12828
  return "values: string expected";
12829
+ if (message.subPartition != null && message.hasOwnProperty("subPartition")) {
12830
+ let error = $root.sqlanvil.PostgresOptions.Partition.verify(message.subPartition);
12831
+ if (error)
12832
+ return "subPartition." + error;
12833
+ }
12814
12834
  return null;
12815
12835
  };
12816
12836
 
@@ -12830,6 +12850,11 @@ const sqlanvil = $root.sqlanvil = (() => {
12830
12850
  message.name = String(object.name);
12831
12851
  if (object.values != null)
12832
12852
  message.values = String(object.values);
12853
+ if (object.subPartition != null) {
12854
+ if (typeof object.subPartition !== "object")
12855
+ throw TypeError(".sqlanvil.PostgresOptions.Partition.Bound.subPartition: object expected");
12856
+ message.subPartition = $root.sqlanvil.PostgresOptions.Partition.fromObject(object.subPartition);
12857
+ }
12833
12858
  return message;
12834
12859
  };
12835
12860
 
@@ -12849,11 +12874,14 @@ const sqlanvil = $root.sqlanvil = (() => {
12849
12874
  if (options.defaults) {
12850
12875
  object.name = "";
12851
12876
  object.values = "";
12877
+ object.subPartition = null;
12852
12878
  }
12853
12879
  if (message.name != null && message.hasOwnProperty("name"))
12854
12880
  object.name = message.name;
12855
12881
  if (message.values != null && message.hasOwnProperty("values"))
12856
12882
  object.values = message.values;
12883
+ if (message.subPartition != null && message.hasOwnProperty("subPartition"))
12884
+ object.subPartition = $root.sqlanvil.PostgresOptions.Partition.toObject(message.subPartition, options);
12857
12885
  return object;
12858
12886
  };
12859
12887