@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) {
@@ -3071,5 +3076,5 @@ export {
3071
3076
  handleSubgraphReorg
3072
3077
  };
3073
3078
 
3074
- //# debugId=314C75DB8B9D518F64756E2164756E21
3079
+ //# debugId=3CC5B780D31E790E64756E2164756E21
3075
3080
  //# sourceMappingURL=reorg.js.map