@secondlayer/subgraphs 4.3.4 → 4.3.5

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/src/index.js CHANGED
@@ -370,6 +370,24 @@ function safeIndexName(name) {
370
370
  const hash = createHash("sha256").update(name).digest("hex").slice(0, 8);
371
371
  return `${name.slice(0, MAX_IDENT_BYTES - hash.length - 1)}_${hash}`;
372
372
  }
373
+ function emitIndexedColumnIndexDDL(schemaName, tableName, colName, opts) {
374
+ const concurrently = opts?.concurrently ? " CONCURRENTLY" : "";
375
+ const qualified = `${quotePgIdent(schemaName)}.${quotePgIdent(tableName)}`;
376
+ const plain = `idx_${schemaName}_${tableName}_${colName}`;
377
+ const composite = safeIndexName(`idx_${schemaName}_${tableName}_${colName}_id`);
378
+ const col = quotePgIdent(colName);
379
+ const id = quotePgIdent("_id");
380
+ return {
381
+ create: [
382
+ `CREATE INDEX${concurrently} IF NOT EXISTS ${quotePgIdent(plain)} ON ${qualified} (${col})`,
383
+ `CREATE INDEX${concurrently} IF NOT EXISTS ${quotePgIdent(composite)} ON ${qualified} (${col}, ${id})`
384
+ ],
385
+ drop: [
386
+ `DROP INDEX${concurrently} IF EXISTS ${quotePgIdent(schemaName)}.${quotePgIdent(plain)}`,
387
+ `DROP INDEX${concurrently} IF EXISTS ${quotePgIdent(schemaName)}.${quotePgIdent(composite)}`
388
+ ]
389
+ };
390
+ }
373
391
  function escapeLiteralDefault(value) {
374
392
  if (value === null || value === undefined)
375
393
  return "NULL";
@@ -411,8 +429,7 @@ function emitTableDDL(schemaName, tableName, tableDef) {
411
429
  statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_tx_id`)} ON ${qualifiedName} (_tx_id)`);
412
430
  for (const [colName, col] of Object.entries(tableDef.columns)) {
413
431
  if (col.indexed) {
414
- statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_${colName}`)} ON ${qualifiedName} (${quotePgIdent(colName)})`);
415
- statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(safeIndexName(`idx_${schemaName}_${tableName}_${colName}_id`))} ON ${qualifiedName} (${quotePgIdent(colName)}, ${quotePgIdent("_id")})`);
432
+ statements.push(...emitIndexedColumnIndexDDL(schemaName, tableName, colName).create);
416
433
  }
417
434
  }
418
435
  for (const [colName, col] of Object.entries(tableDef.columns)) {
@@ -5657,7 +5674,9 @@ async function deploySchema(db, def, handlerPath, opts) {
5657
5674
  const nullable = col.nullable ? "" : ` NOT NULL DEFAULT ${getDefault(col.type)}`;
5658
5675
  await sql6.raw(`ALTER TABLE ${qualifiedName} ADD COLUMN ${colName} ${sqlType}${nullable}`).execute(ddlDb);
5659
5676
  if (col.indexed) {
5660
- await sql6.raw(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`).execute(ddlDb);
5677
+ for (const stmt of emitIndexedColumnIndexDDL(schemaName, tableName, colName).create) {
5678
+ await sql6.raw(stmt).execute(ddlDb);
5679
+ }
5661
5680
  }
5662
5681
  if (col.search) {
5663
5682
  await sql6.raw(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`).execute(ddlDb);
@@ -5680,12 +5699,16 @@ async function deploySchema(db, def, handlerPath, opts) {
5680
5699
  const now = def.schema[tableName]?.columns[colName];
5681
5700
  if (!now)
5682
5701
  continue;
5683
- const plainIdx = `idx_${schemaName}_${tableName}_${colName}`;
5684
5702
  const trgmIdx = `idx_${schemaName}_${tableName}_${colName}_trgm`;
5703
+ const indexedIdx = emitIndexedColumnIndexDDL(schemaName, tableName, colName, { concurrently: true });
5685
5704
  if (now.indexed && !was?.indexed) {
5686
- await sql6.raw(`CREATE INDEX CONCURRENTLY IF NOT EXISTS ${plainIdx} ON ${qualifiedName} (${colName})`).execute(ddlDb);
5705
+ for (const stmt of indexedIdx.create) {
5706
+ await sql6.raw(stmt).execute(ddlDb);
5707
+ }
5687
5708
  } else if (!now.indexed && was?.indexed) {
5688
- await sql6.raw(`DROP INDEX CONCURRENTLY IF EXISTS ${schemaName}.${plainIdx}`).execute(ddlDb);
5709
+ for (const stmt of indexedIdx.drop) {
5710
+ await sql6.raw(stmt).execute(ddlDb);
5711
+ }
5689
5712
  }
5690
5713
  if (now.search && !was?.search) {
5691
5714
  await sql6.raw(`CREATE INDEX CONCURRENTLY IF NOT EXISTS ${trgmIdx} ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`).execute(ddlDb);
@@ -5798,5 +5821,5 @@ export {
5798
5821
  ChainReadError
5799
5822
  };
5800
5823
 
5801
- //# debugId=ADB058A57FECD09264756E2164756E21
5824
+ //# debugId=DBCE838E06681C8A64756E2164756E21
5802
5825
  //# sourceMappingURL=index.js.map