@secondlayer/subgraphs 3.24.0 → 3.24.1

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.
@@ -11,6 +11,11 @@ import { createHash } from "node:crypto";
11
11
 
12
12
  // src/schema/utils.ts
13
13
  import { pgSchemaName } from "@secondlayer/shared/db/queries/subgraphs";
14
+ function quotePgIdent(name) {
15
+ if (/^[a-z_][a-z0-9_]*$/i.test(name))
16
+ return name;
17
+ return `"${name.replace(/"/g, '""')}"`;
18
+ }
14
19
 
15
20
  // src/schema/generator.ts
16
21
  var TYPE_MAP = {
@@ -35,7 +40,7 @@ function tableNeedsTrgm(tableDef) {
35
40
  return Object.values(tableDef.columns).some((col) => col.search);
36
41
  }
37
42
  function emitTableDDL(schemaName, tableName, tableDef) {
38
- const qualifiedName = `${schemaName}.${tableName}`;
43
+ const qualifiedName = `${quotePgIdent(schemaName)}.${quotePgIdent(tableName)}`;
39
44
  const statements = [];
40
45
  const columnDefs = [
41
46
  "_id BIGSERIAL PRIMARY KEY",
@@ -46,12 +51,12 @@ function emitTableDDL(schemaName, tableName, tableDef) {
46
51
  for (const [colName, col] of Object.entries(tableDef.columns)) {
47
52
  const sqlType = TYPE_MAP[col.type];
48
53
  const nullable = col.nullable ? "" : " NOT NULL";
49
- let colDef = `${colName} ${sqlType}${nullable}`;
54
+ let colDef = `${quotePgIdent(colName)} ${sqlType}${nullable}`;
50
55
  if (col.default !== undefined) {
51
56
  colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
52
57
  }
53
58
  if (col.type === "uint") {
54
- colDef += ` CHECK (${colName} >= 0)`;
59
+ colDef += ` CHECK (${quotePgIdent(colName)} >= 0)`;
55
60
  }
56
61
  columnDefs.push(colDef);
57
62
  }
@@ -59,30 +64,30 @@ function emitTableDDL(schemaName, tableName, tableDef) {
59
64
  ${columnDefs.join(`,
60
65
  `)}
61
66
  )`);
62
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_block_height ON ${qualifiedName} (_block_height)`);
63
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_tx_id ON ${qualifiedName} (_tx_id)`);
67
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_block_height`)} ON ${qualifiedName} (_block_height)`);
68
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_tx_id`)} ON ${qualifiedName} (_tx_id)`);
64
69
  for (const [colName, col] of Object.entries(tableDef.columns)) {
65
70
  if (col.indexed) {
66
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`);
71
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_${colName}`)} ON ${qualifiedName} (${quotePgIdent(colName)})`);
67
72
  }
68
73
  }
69
74
  for (const [colName, col] of Object.entries(tableDef.columns)) {
70
75
  if (col.search) {
71
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`);
76
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_${colName}_trgm`)} ON ${qualifiedName} USING gin (${quotePgIdent(colName)} gin_trgm_ops)`);
72
77
  }
73
78
  }
74
79
  if (tableDef.indexes) {
75
80
  for (let i = 0;i < tableDef.indexes.length; i++) {
76
81
  const cols = tableDef.indexes[i];
77
- const idxName = `idx_${schemaName}_${tableName}_composite_${i}`;
78
- statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.join(", ")})`);
82
+ const idxName = quotePgIdent(`idx_${schemaName}_${tableName}_composite_${i}`);
83
+ statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.map(quotePgIdent).join(", ")})`);
79
84
  }
80
85
  }
81
86
  if (tableDef.uniqueKeys) {
82
87
  for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
83
88
  const cols = tableDef.uniqueKeys[i];
84
- const constraintName = `uq_${schemaName}_${tableName}_${cols.join("_")}`;
85
- statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.join(", ")})`);
89
+ const constraintName = quotePgIdent(`uq_${schemaName}_${tableName}_${cols.join("_")}`);
90
+ statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.map(quotePgIdent).join(", ")})`);
86
91
  }
87
92
  }
88
93
  return statements;
@@ -114,8 +119,8 @@ function emitFactoryDDL(schemaName) {
114
119
  }
115
120
  function emitForeignKeyDDL(schemaName, tableName, tableDef) {
116
121
  return (tableDef.relations ?? []).map((rel) => {
117
- const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
118
- return `ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`;
122
+ const constraintName = quotePgIdent(`fk_${schemaName}_${tableName}_${rel.name}`);
123
+ return `ALTER TABLE ${quotePgIdent(schemaName)}.${quotePgIdent(tableName)} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.map(quotePgIdent).join(", ")}) ` + `REFERENCES ${quotePgIdent(schemaName)}.${quotePgIdent(rel.references)} (${rel.referencedColumns.map(quotePgIdent).join(", ")})`;
119
124
  });
120
125
  }
121
126
  function generateSubgraphSQL(def, schemaNameOverride) {
@@ -2940,5 +2945,5 @@ export {
2940
2945
  bumpReorgEpoch
2941
2946
  };
2942
2947
 
2943
- //# debugId=05204354946D588C64756E2164756E21
2948
+ //# debugId=0EDBE541AA2E471264756E2164756E21
2944
2949
  //# sourceMappingURL=catchup.js.map