Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store-sync): adjust DB schema/table names for consistency #2379

Merged
merged 8 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/wise-bees-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"@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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = "0.0.6";
export const version = "0.0.7";
Loading
Loading