From adc68225008b481df6b47050638677fd936c22c9 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Mon, 11 Mar 2024 10:40:19 +0000 Subject: [PATCH] feat(store-sync): adjust DB schema/table names for consistency (#2379) --- .changeset/wise-bees-repair.md | 27 ++++ .../src/postgres-decoded/buildTable.test.ts | 8 +- .../src/postgres-decoded/buildTable.ts | 6 +- .../createStorageAdapter.test.ts | 2 +- .../store-sync/src/postgres/cleanDatabase.ts | 5 +- .../src/postgres/createStorageAdapter.test.ts | 2 +- packages/store-sync/src/postgres/version.ts | 2 +- .../store-sync/src/sqlite/buildTable.test.ts | 148 +++++++++--------- packages/store-sync/src/sqlite/buildTable.ts | 5 +- .../store-sync/src/sqlite/getTableName.ts | 4 +- .../store-sync/src/sqlite/schemaVersion.ts | 2 +- .../src/sqlite/sqliteStorage.test.ts | 10 +- 12 files changed, 125 insertions(+), 96 deletions(-) create mode 100644 .changeset/wise-bees-repair.md diff --git a/.changeset/wise-bees-repair.md b/.changeset/wise-bees-repair.md new file mode 100644 index 0000000000..c8ec0537d8 --- /dev/null +++ b/.changeset/wise-bees-repair.md @@ -0,0 +1,27 @@ +--- +"@latticexyz/store-indexer": major +"@latticexyz/store-sync": major +--- + +PostgreSQL sync/indexer now uses `{storeAddress}` for its database schema names and `{namespace}__{tableName}` for its database table names (or just `{tableName}` for root namespace), to be more consistent with the rest of the MUD codebase. + +For namespaced tables: + +```diff +- SELECT * FROM 0xfff__some_ns.some_table ++ SELECT * FROM 0xfff.some_ns__some_table +``` + +For root tables: + +```diff +- SELECT * FROM 0xfff__.some_table ++ SELECT * FROM 0xfff.some_table +``` + +SQLite sync/indexer now uses snake case for its table names and column names for easier writing of queries and to better match PostgreSQL sync/indexer naming. + +```diff +- SELECT * FROM 0xfFf__someNS__someTable ++ SELECT * FROM 0xfff__some_ns__some_table +``` diff --git a/packages/store-sync/src/postgres-decoded/buildTable.test.ts b/packages/store-sync/src/postgres-decoded/buildTable.test.ts index b6369772f5..1171b5323a 100644 --- a/packages/store-sync/src/postgres-decoded/buildTable.test.ts +++ b/packages/store-sync/src/postgres-decoded/buildTable.test.ts @@ -14,8 +14,8 @@ describe("buildTable", () => { valueSchema: { name: "string", walletAddress: "address" }, }); - expect(getTableConfig(table).schema).toMatch(/0xffffffffffffffffffffffffffffffffffffffff__testNS$/); - expect(getTableConfig(table).name).toMatchInlineSnapshot('"users_table"'); + expect(getTableConfig(table).schema).toMatch(/0xffffffffffffffffffffffffffffffffffffffff$/); + expect(getTableConfig(table).name).toMatchInlineSnapshot('"test_ns__users_table"'); expect( mapObject(getTableColumns(table), (column) => ({ name: column.name, @@ -74,8 +74,8 @@ describe("buildTable", () => { valueSchema: { addrs: "address[]" }, }); - expect(getTableConfig(table).schema).toMatch(/0xffffffffffffffffffffffffffffffffffffffff__testNS$/); - expect(getTableConfig(table).name).toMatchInlineSnapshot('"users_table"'); + expect(getTableConfig(table).schema).toMatch(/0xffffffffffffffffffffffffffffffffffffffff$/); + expect(getTableConfig(table).name).toMatchInlineSnapshot('"test_ns__users_table"'); expect( mapObject(getTableColumns(table), (column) => ({ name: column.name, diff --git a/packages/store-sync/src/postgres-decoded/buildTable.ts b/packages/store-sync/src/postgres-decoded/buildTable.ts index 56797a8438..be5ef290fb 100644 --- a/packages/store-sync/src/postgres-decoded/buildTable.ts +++ b/packages/store-sync/src/postgres-decoded/buildTable.ts @@ -1,5 +1,5 @@ import { PgColumnBuilderBase, PgTableWithColumns, pgSchema } from "drizzle-orm/pg-core"; -import { Address, getAddress } from "viem"; +import { Address } from "viem"; import { snakeCase } from "change-case"; import { KeySchema, ValueSchema } from "@latticexyz/protocol-parser"; import { asBigInt, asHex } from "../postgres/columnTypes"; @@ -50,8 +50,8 @@ export function buildTable { { "blockNumber": 21n, "chainId": 31337, - "version": "0.0.6", + "version": "0.0.7", }, ] `); diff --git a/packages/store-sync/src/postgres/cleanDatabase.ts b/packages/store-sync/src/postgres/cleanDatabase.ts index b39a846e99..3622388d8d 100644 --- a/packages/store-sync/src/postgres/cleanDatabase.ts +++ b/packages/store-sync/src/postgres/cleanDatabase.ts @@ -12,9 +12,10 @@ const schemata = pgSchema("information_schema").table("schemata", { }); function isMudSchemaName(schemaName: string): boolean { - // address-prefixed schemas like {address}__{namespace} used by decoded postgres tables + // each store address has its own schema used by decoded postgres tables // optional prefix for schemas created in tests - if (/(^|__)0x[0-9a-f]{40}__/i.test(schemaName)) { + // optional suffix for schemas that used to also include namespace (pre 0.0.7) + if (/(^|__)0x[0-9a-f]{40}($|__)/i.test(schemaName)) { return true; } // schema for internal tables diff --git a/packages/store-sync/src/postgres/createStorageAdapter.test.ts b/packages/store-sync/src/postgres/createStorageAdapter.test.ts index 84d5b09e85..5a6cb9cc8a 100644 --- a/packages/store-sync/src/postgres/createStorageAdapter.test.ts +++ b/packages/store-sync/src/postgres/createStorageAdapter.test.ts @@ -51,7 +51,7 @@ describe("createStorageAdapter", async () => { { "blockNumber": 21n, "chainId": 31337, - "version": "0.0.6", + "version": "0.0.7", }, ] `); diff --git a/packages/store-sync/src/postgres/version.ts b/packages/store-sync/src/postgres/version.ts index a7f2787858..4d224bd1d2 100644 --- a/packages/store-sync/src/postgres/version.ts +++ b/packages/store-sync/src/postgres/version.ts @@ -1 +1 @@ -export const version = "0.0.6"; +export const version = "0.0.7"; diff --git a/packages/store-sync/src/sqlite/buildTable.test.ts b/packages/store-sync/src/sqlite/buildTable.test.ts index 22b1eb0a42..c68e5352d6 100644 --- a/packages/store-sync/src/sqlite/buildTable.test.ts +++ b/packages/store-sync/src/sqlite/buildTable.test.ts @@ -26,7 +26,7 @@ describe("buildTable", () => { "name": "__dynamicData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -40,7 +40,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "__encodedLengths": SQLiteText { @@ -56,7 +56,7 @@ describe("buildTable", () => { "name": "__encodedLengths", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "dataType": "string", @@ -70,7 +70,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "__isDeleted": SQLiteBoolean { @@ -87,7 +87,7 @@ describe("buildTable", () => { "name": "__isDeleted", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "dataType": "boolean", @@ -101,7 +101,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "__key": SQLiteText { @@ -117,7 +117,7 @@ describe("buildTable", () => { "name": "__key", "notNull": true, "primaryKey": true, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "dataType": "string", @@ -131,7 +131,7 @@ describe("buildTable", () => { "notNull": true, "primary": true, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "__lastUpdatedBlockNumber": SQLiteBigInt { @@ -145,7 +145,7 @@ describe("buildTable", () => { "name": "__lastUpdatedBlockNumber", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "dataType": "bigint", @@ -158,7 +158,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "__staticData": SQLiteText { @@ -174,7 +174,7 @@ describe("buildTable", () => { "name": "__staticData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -188,7 +188,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "addr": SQLiteCustomColumn { @@ -208,7 +208,7 @@ describe("buildTable", () => { "name": "addr", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addr_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addr_unique", "uniqueType": undefined, }, "dataType": "custom", @@ -224,7 +224,7 @@ describe("buildTable", () => { "primary": false, "sqlName": "text", "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addr_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addr_unique", "uniqueType": undefined, }, "name": SQLiteText { @@ -240,7 +240,7 @@ describe("buildTable", () => { "name": "name", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_name_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_name_unique", "uniqueType": undefined, }, "dataType": "string", @@ -254,7 +254,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_name_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_name_unique", "uniqueType": undefined, }, "x": SQLiteInteger { @@ -270,7 +270,7 @@ describe("buildTable", () => { "name": "x", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_x_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_x_unique", "uniqueType": undefined, }, "dataType": "number", @@ -283,7 +283,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_x_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_x_unique", "uniqueType": undefined, }, "y": SQLiteInteger { @@ -299,7 +299,7 @@ describe("buildTable", () => { "name": "y", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_y_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_y_unique", "uniqueType": undefined, }, "dataType": "number", @@ -312,11 +312,11 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_y_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_y_unique", "uniqueType": undefined, }, - Symbol(drizzle:Name): "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users", - Symbol(drizzle:OriginalName): "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users", + Symbol(drizzle:Name): "0xffffffffffffffffffffffffffffffffffffffff__test__users", + Symbol(drizzle:OriginalName): "0xffffffffffffffffffffffffffffffffffffffff__test__users", Symbol(drizzle:Schema): undefined, Symbol(drizzle:Columns): { "__dynamicData": SQLiteText { @@ -332,7 +332,7 @@ describe("buildTable", () => { "name": "__dynamicData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -346,7 +346,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "__encodedLengths": SQLiteText { @@ -362,7 +362,7 @@ describe("buildTable", () => { "name": "__encodedLengths", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "dataType": "string", @@ -376,7 +376,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "__isDeleted": SQLiteBoolean { @@ -393,7 +393,7 @@ describe("buildTable", () => { "name": "__isDeleted", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "dataType": "boolean", @@ -407,7 +407,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "__key": SQLiteText { @@ -423,7 +423,7 @@ describe("buildTable", () => { "name": "__key", "notNull": true, "primaryKey": true, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "dataType": "string", @@ -437,7 +437,7 @@ describe("buildTable", () => { "notNull": true, "primary": true, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "__lastUpdatedBlockNumber": SQLiteBigInt { @@ -451,7 +451,7 @@ describe("buildTable", () => { "name": "__lastUpdatedBlockNumber", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "dataType": "bigint", @@ -464,7 +464,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "__staticData": SQLiteText { @@ -480,7 +480,7 @@ describe("buildTable", () => { "name": "__staticData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -494,7 +494,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "addr": SQLiteCustomColumn { @@ -514,7 +514,7 @@ describe("buildTable", () => { "name": "addr", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addr_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addr_unique", "uniqueType": undefined, }, "dataType": "custom", @@ -530,7 +530,7 @@ describe("buildTable", () => { "primary": false, "sqlName": "text", "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addr_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addr_unique", "uniqueType": undefined, }, "name": SQLiteText { @@ -546,7 +546,7 @@ describe("buildTable", () => { "name": "name", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_name_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_name_unique", "uniqueType": undefined, }, "dataType": "string", @@ -560,7 +560,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_name_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_name_unique", "uniqueType": undefined, }, "x": SQLiteInteger { @@ -576,7 +576,7 @@ describe("buildTable", () => { "name": "x", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_x_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_x_unique", "uniqueType": undefined, }, "dataType": "number", @@ -589,7 +589,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_x_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_x_unique", "uniqueType": undefined, }, "y": SQLiteInteger { @@ -605,7 +605,7 @@ describe("buildTable", () => { "name": "y", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_y_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_y_unique", "uniqueType": undefined, }, "dataType": "number", @@ -618,11 +618,11 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_y_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_y_unique", "uniqueType": undefined, }, }, - Symbol(drizzle:BaseName): "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users", + Symbol(drizzle:BaseName): "0xffffffffffffffffffffffffffffffffffffffff__test__users", Symbol(drizzle:IsAlias): false, Symbol(drizzle:ExtraConfigBuilder): undefined, Symbol(drizzle:IsDrizzleTable): true, @@ -655,7 +655,7 @@ describe("buildTable", () => { "name": "__dynamicData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -669,7 +669,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "__encodedLengths": SQLiteText { @@ -685,7 +685,7 @@ describe("buildTable", () => { "name": "__encodedLengths", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "dataType": "string", @@ -699,7 +699,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "__isDeleted": SQLiteBoolean { @@ -716,7 +716,7 @@ describe("buildTable", () => { "name": "__isDeleted", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "dataType": "boolean", @@ -730,7 +730,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "__key": SQLiteText { @@ -746,7 +746,7 @@ describe("buildTable", () => { "name": "__key", "notNull": true, "primaryKey": true, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "dataType": "string", @@ -760,7 +760,7 @@ describe("buildTable", () => { "notNull": true, "primary": true, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "__lastUpdatedBlockNumber": SQLiteBigInt { @@ -774,7 +774,7 @@ describe("buildTable", () => { "name": "__lastUpdatedBlockNumber", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "dataType": "bigint", @@ -787,7 +787,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "__staticData": SQLiteText { @@ -803,7 +803,7 @@ describe("buildTable", () => { "name": "__staticData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -817,7 +817,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "addrs": SQLiteCustomColumn { @@ -837,7 +837,7 @@ describe("buildTable", () => { "name": "addrs", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addrs_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addrs_unique", "uniqueType": undefined, }, "dataType": "custom", @@ -853,11 +853,11 @@ describe("buildTable", () => { "primary": false, "sqlName": "text", "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addrs_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addrs_unique", "uniqueType": undefined, }, - Symbol(drizzle:Name): "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users", - Symbol(drizzle:OriginalName): "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users", + Symbol(drizzle:Name): "0xffffffffffffffffffffffffffffffffffffffff__test__users", + Symbol(drizzle:OriginalName): "0xffffffffffffffffffffffffffffffffffffffff__test__users", Symbol(drizzle:Schema): undefined, Symbol(drizzle:Columns): { "__dynamicData": SQLiteText { @@ -873,7 +873,7 @@ describe("buildTable", () => { "name": "__dynamicData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -887,7 +887,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___dynamicData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___dynamicData_unique", "uniqueType": undefined, }, "__encodedLengths": SQLiteText { @@ -903,7 +903,7 @@ describe("buildTable", () => { "name": "__encodedLengths", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "dataType": "string", @@ -917,7 +917,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___encodedLengths_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___encodedLengths_unique", "uniqueType": undefined, }, "__isDeleted": SQLiteBoolean { @@ -934,7 +934,7 @@ describe("buildTable", () => { "name": "__isDeleted", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "dataType": "boolean", @@ -948,7 +948,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___isDeleted_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___isDeleted_unique", "uniqueType": undefined, }, "__key": SQLiteText { @@ -964,7 +964,7 @@ describe("buildTable", () => { "name": "__key", "notNull": true, "primaryKey": true, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "dataType": "string", @@ -978,7 +978,7 @@ describe("buildTable", () => { "notNull": true, "primary": true, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___key_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___key_unique", "uniqueType": undefined, }, "__lastUpdatedBlockNumber": SQLiteBigInt { @@ -992,7 +992,7 @@ describe("buildTable", () => { "name": "__lastUpdatedBlockNumber", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "dataType": "bigint", @@ -1005,7 +1005,7 @@ describe("buildTable", () => { "notNull": true, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___lastUpdatedBlockNumber_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___lastUpdatedBlockNumber_unique", "uniqueType": undefined, }, "__staticData": SQLiteText { @@ -1021,7 +1021,7 @@ describe("buildTable", () => { "name": "__staticData", "notNull": false, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "dataType": "string", @@ -1035,7 +1035,7 @@ describe("buildTable", () => { "notNull": false, "primary": false, "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users___staticData_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users___staticData_unique", "uniqueType": undefined, }, "addrs": SQLiteCustomColumn { @@ -1055,7 +1055,7 @@ describe("buildTable", () => { "name": "addrs", "notNull": true, "primaryKey": false, - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addrs_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addrs_unique", "uniqueType": undefined, }, "dataType": "custom", @@ -1071,11 +1071,11 @@ describe("buildTable", () => { "primary": false, "sqlName": "text", "table": [Circular], - "uniqueName": "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users_addrs_unique", + "uniqueName": "0xffffffffffffffffffffffffffffffffffffffff__test__users_addrs_unique", "uniqueType": undefined, }, }, - Symbol(drizzle:BaseName): "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF__test__users", + Symbol(drizzle:BaseName): "0xffffffffffffffffffffffffffffffffffffffff__test__users", Symbol(drizzle:IsAlias): false, Symbol(drizzle:ExtraConfigBuilder): undefined, Symbol(drizzle:IsDrizzleTable): true, diff --git a/packages/store-sync/src/sqlite/buildTable.ts b/packages/store-sync/src/sqlite/buildTable.ts index d37d77d8e7..5e419747da 100644 --- a/packages/store-sync/src/sqlite/buildTable.ts +++ b/packages/store-sync/src/sqlite/buildTable.ts @@ -3,6 +3,7 @@ import { buildColumn } from "./buildColumn"; import { Address } from "viem"; import { getTableName } from "./getTableName"; import { KeySchema, ValueSchema } from "@latticexyz/protocol-parser"; +import { snakeCase } from "change-case"; export const metaColumns = { __key: buildColumn("__key", "bytes").primaryKey(), @@ -53,11 +54,11 @@ export function buildTable [name, buildColumn(name, type).notNull()]), + Object.entries(keySchema).map(([name, type]) => [name, buildColumn(snakeCase(name), type).notNull()]), ); const valueColumns = Object.fromEntries( - Object.entries(valueSchema).map(([name, type]) => [name, buildColumn(name, type).notNull()]), + Object.entries(valueSchema).map(([name, type]) => [name, buildColumn(snakeCase(name), type).notNull()]), ); // TODO: unique constraint on key columns? diff --git a/packages/store-sync/src/sqlite/getTableName.ts b/packages/store-sync/src/sqlite/getTableName.ts index 2fd6f6c09b..b9ace03826 100644 --- a/packages/store-sync/src/sqlite/getTableName.ts +++ b/packages/store-sync/src/sqlite/getTableName.ts @@ -1,5 +1,5 @@ -import { getAddress } from "viem"; +import { snakeCase } from "change-case"; export function getTableName(address: string, namespace: string, tableName: string): string { - return `${getAddress(address)}__${namespace}__${tableName}`; + return `${address.toLowerCase()}__${snakeCase(namespace)}__${snakeCase(tableName)}`; } diff --git a/packages/store-sync/src/sqlite/schemaVersion.ts b/packages/store-sync/src/sqlite/schemaVersion.ts index fb06a9bdc7..676dc68375 100644 --- a/packages/store-sync/src/sqlite/schemaVersion.ts +++ b/packages/store-sync/src/sqlite/schemaVersion.ts @@ -1,3 +1,3 @@ // When this is incremented, it forces all indexers to reindex from scratch the next time they start up. // Only use this when the schemas change, until we get proper schema migrations. -export const schemaVersion = 1; +export const schemaVersion = 2; diff --git a/packages/store-sync/src/sqlite/sqliteStorage.test.ts b/packages/store-sync/src/sqlite/sqliteStorage.test.ts index f683ef58a4..e12e6574a8 100644 --- a/packages/store-sync/src/sqlite/sqliteStorage.test.ts +++ b/packages/store-sync/src/sqlite/sqliteStorage.test.ts @@ -66,7 +66,7 @@ describe("sqliteStorage", async () => { "chainId": 31337, "lastError": null, "lastUpdatedBlockNumber": 21n, - "schemaVersion": 1, + "schemaVersion": 2, }, ] `); @@ -75,13 +75,13 @@ describe("sqliteStorage", async () => { [ { "address": "0x7C78d585F136d7247f9deA68f60CE8A2D3F311E2", - "id": "0x7C78d585F136d7247f9deA68f60CE8A2D3F311E2____NumberList", + "id": "0x7c78d585f136d7247f9dea68f60ce8a2d3f311e2____number_list", "keySchema": {}, "lastError": null, "lastUpdatedBlockNumber": 21n, "name": "NumberList", "namespace": "", - "schemaVersion": 1, + "schemaVersion": 2, "tableId": "0x746200000000000000000000000000004e756d6265724c697374000000000000", "valueSchema": { "value": "uint32[]", @@ -95,13 +95,13 @@ describe("sqliteStorage", async () => { [ { "address": "0x7C78d585F136d7247f9deA68f60CE8A2D3F311E2", - "id": "0x7C78d585F136d7247f9deA68f60CE8A2D3F311E2____NumberList", + "id": "0x7c78d585f136d7247f9dea68f60ce8a2d3f311e2____number_list", "keySchema": {}, "lastError": null, "lastUpdatedBlockNumber": 21n, "name": "NumberList", "namespace": "", - "schemaVersion": 1, + "schemaVersion": 2, "tableId": "0x746200000000000000000000000000004e756d6265724c697374000000000000", "valueSchema": { "value": "uint32[]",