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

Add .default() as column definition modifier #1085

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
ponder-postgres:
container_name: ponder-postgres
image: postgres
shm_size: 8g
ports:
- 42070:5432
volumes:
- ./db:/data/postgres
networks:
- ponder
restart: unless-stopped
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d postgres" ]
interval: 30s
timeout: 10s
retries: 5
environment:
- POSTGRES_DB=ponder
- POSTGRES_USER=ponder
- POSTGRES_PASSWORD=password

networks:
ponder:
driver: bridge

volumes:
ponder:
external: true
171 changes: 171 additions & 0 deletions packages/core/src/database/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "@/utils/checkpoint.js";
import { wait } from "@/utils/wait.js";
import { sql } from "kysely";
import { hexToBytes, zeroAddress } from "viem";
import { beforeEach, expect, test } from "vitest";
import { type Database, createDatabase } from "./index.js";

Expand Down Expand Up @@ -58,6 +59,40 @@ const schemaTwo = createSchema((p) => ({
}),
}));

const dogWithDefaults = createSchema((p) => ({
Dog: p.createTable({
id: p.string().default("0"),
name: p.string().default("firstname"),
age: p.int().default(5).optional(),
bigAge: p.bigint().default(5n).optional(),
bowl: p.hex().default(zeroAddress),
toys: p.json().default({
bone: "sofa",
ball: "bed",
}),
commands: p.json().default([
"sit",
"stay",
{
paw: {
right: true,
left: false,
},
},
]),
}),
}));

type Dog = {
id: string;
name: string;
age: string;
bigAge: bigint;
bowl: string;
toys: {};
commands: (string | {})[];
};

function createCheckpoint(index: number): Checkpoint {
return { ...zeroCheckpoint, blockTimestamp: index };
}
Expand Down Expand Up @@ -759,6 +794,97 @@ test("revert() updates versions with intermediate logs", async (context) => {
await cleanup();
});

test("information about columns can be queried", async (context) => {
const database = createDatabase({
common: context.common,
schema: dogWithDefaults,
databaseConfig: context.databaseConfig,
});
await database.setup({ buildId: "abc" });

const defaults = await getTableDefaults(database, "Dog");
const dogTable = dogWithDefaults.Dog.table;
for (const [column, metadata] of Object.entries(defaults)) {
expect(dogTable[column as keyof typeof dogTable]).not.to.be.empty;
expect(metadata.default).to.be.toBeTypeOf("string");
expect(metadata.type).to.be.toBeTypeOf("string");
}
await database.kill();
});

test("default values are populated during insertion", async (context) => {
const database = createDatabase({
common: context.common,
schema: dogWithDefaults,
databaseConfig: context.databaseConfig,
});
await database.setup({ buildId: "abc" });

const { rows } = await database.qb.internal.executeQuery<Dog[]>(
(database.dialect === "sqlite"
? sql`
INSERT INTO Dog
DEFAULT VALUES
RETURNING *
`
: sql`INSERT INTO "Dog"(id, name, age, "bigAge", bowl, toys)
VALUES(DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)
RETURNING *`
).compile(database.qb.internal),
);
const sqliteRows = [
{
id: "0",
name: "firstname",
age: 5,
bigAge: "5",
bowl: `\\x${zeroAddress.slice(2)}`,
commands: JSON.stringify([
"sit",
"stay",
{
paw: {
right: true,
left: false,
},
},
]),
toys: JSON.stringify({
bone: "sofa",
ball: "bed",
}),
},
];
const pgRows = [
{
id: "0",
name: "firstname",
age: 5,
bigAge: 5n,
bowl: Buffer.from(hexToBytes(zeroAddress)),
commands: [
"sit",
"stay",
{
paw: {
right: true,
left: false,
},
},
],
toys: {
bone: "sofa",
ball: "bed",
},
},
];
expect(rows).to.deep.equal(
database.dialect === "sqlite" ? sqliteRows : pgRows,
);

await database.kill();
});

async function getUserTableNames(database: Database) {
const { rows } = await database.qb.internal.executeQuery<{ name: string }>(
database.dialect === "sqlite"
Expand Down Expand Up @@ -793,3 +919,48 @@ async function getUserIndexNames(database: Database, tableName: string) {
);
return rows.map((r) => r.name);
}

async function getTableDefaults(db: Database, tableName: string) {
if (db.dialect === "sqlite") {
const { rows } = await db.qb.internal.executeQuery<{
name: string;
type: string;
dflt_value: string | null;
}>(
sql`SELECT * from ${sql.raw(db.namespace)}.pragma_table_info('${sql.raw(tableName)}')`.compile(
db.qb.internal,
),
);
return rows.reduce(
(obj, column) => {
obj[column.name] = {
type: column.type,
default: column.dflt_value,
};
return obj;
},
{} as Record<string, { type: string; default: any }>,
);
} else {
const { rows } = await db.qb.internal.executeQuery<{
column_name: string;
data_type: string;
column_default: string;
}>(
sql`SELECT *
FROM information_schema.columns
WHERE table_schema = '${sql.raw(db.namespace)}'
AND table_name = '${sql.raw(tableName)}'`.compile(db.qb.internal),
);
return rows.reduce(
(obj, column) => {
obj[column.column_name] = {
type: column.data_type,
default: column.column_default,
};
return obj;
},
{} as Record<string, { type: string; default: any }>,
);
}
}
4 changes: 4 additions & 0 deletions packages/core/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NonRetryableError } from "@/common/errors.js";
import type { DatabaseConfig } from "@/config/database.js";
import type { Schema } from "@/schema/common.js";
import {
applyDefault,
getEnums,
getTables,
isEnumColumn,
Expand Down Expand Up @@ -698,6 +699,7 @@ export const createDatabase = (args: {
columnName,
"text",
(col) => {
col = applyDefault(col, column);
if (isOptionalColumn(column) === false)
col = col.notNull();
if (isListColumn(column) === false) {
Expand Down Expand Up @@ -729,6 +731,7 @@ export const createDatabase = (args: {
columnName,
"jsonb",
(col) => {
col = applyDefault(col, column);
if (isOptionalColumn(column) === false)
col = col.notNull();
return col;
Expand All @@ -742,6 +745,7 @@ export const createDatabase = (args: {
? scalarToSqliteType
: scalarToPostgresType)[column[" scalar"]],
(col) => {
col = applyDefault(col, column);
if (isOptionalColumn(column) === false)
col = col.notNull();
if (columnName === "id") col = col.primaryKey();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/indexing-store/utils/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ function decodeValue({
} else if (column[" scalar"] === "boolean") {
return value === 1;
} else if (column[" scalar"] === "hex") {
return bytesToHex(value as Buffer);
return bytesToHex(value as Uint8Array);
} else if (column[" scalar"] === "bigint" && dialect === "sqlite") {
return decodeToBigInt(value as string);
} else {
Expand Down
Loading
Loading