Skip to content

Commit

Permalink
feat(store-sync): adjust DB schema/table names for consistency (#2379)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Mar 11, 2024
1 parent 307abab commit adc6822
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 96 deletions.
27 changes: 27 additions & 0 deletions .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
```
8 changes: 4 additions & 4 deletions packages/store-sync/src/postgres-decoded/buildTable.test.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions 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";
Expand Down Expand Up @@ -50,8 +50,8 @@ export function buildTable<TKeySchema extends KeySchema, TValueSchema extends Va
// We intentionally do not snake case the namespace due to potential conflicts
// with namespaces of a similar name (e.g. `MyNamespace` vs. `my_namespace`).
// TODO: consider snake case when we resolve https://github.com/latticexyz/mud/issues/1991
const schemaName = transformSchemaName(`${address.toLowerCase()}__${namespace}`);
const tableName = snakeCase(name);
const schemaName = transformSchemaName(address.toLowerCase());
const tableName = namespace ? `${snakeCase(namespace)}__${snakeCase(name)}` : snakeCase(name);

// Column names, however, are safe to snake case because they're scoped to tables, defined once per table, and there's a limited number of fields in total.

Expand Down
Expand Up @@ -53,7 +53,7 @@ describe("createStorageAdapter", async () => {
{
"blockNumber": 21n,
"chainId": 31337,
"version": "0.0.6",
"version": "0.0.7",
},
]
`);
Expand Down
5 changes: 3 additions & 2 deletions packages/store-sync/src/postgres/cleanDatabase.ts
Expand Up @@ -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
Expand Down
Expand Up @@ -51,7 +51,7 @@ describe("createStorageAdapter", async () => {
{
"blockNumber": 21n,
"chainId": 31337,
"version": "0.0.6",
"version": "0.0.7",
},
]
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/postgres/version.ts
@@ -1 +1 @@
export const version = "0.0.6";
export const version = "0.0.7";

0 comments on commit adc6822

Please sign in to comment.