Skip to content
Merged
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
10 changes: 2 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ jobs:
name: Lint and build
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- uses: jdx/mise-action@v3
- run: npm ci
- run: npm run build
- run: npm run format:check
Expand All @@ -38,10 +35,7 @@ jobs:
postgres: [13, 15, 17, 18]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- uses: jdx/mise-action@v3
- uses: ankane/setup-postgres@v1
with:
postgres-version: ${{ matrix.postgres }}
Expand Down
2 changes: 2 additions & 0 deletions .mise.toml
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are starting to adopt mise elsewhere so just sneaking this in (since otherwise I no longer had a global node installed when in this repo).

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
node = "24.12.0"
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions src/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,85 @@ describe("Table.minId", () => {

expect(minId).toBe(2n);
});

test("respects time filter for timestamptz column", async ({
transaction,
}) => {
await transaction.query(sql.unsafe`
CREATE TABLE test_table (
id BIGSERIAL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL,
name TEXT
)
`);
await transaction.query(sql.unsafe`
INSERT INTO test_table (created_at, name) VALUES
('2024-01-01 00:00:00 UTC', 'old'),
('2024-06-01 00:00:00 UTC', 'mid'),
('2024-12-01 00:00:00 UTC', 'new')
`);

const table = Table.parse("test_table");
const minId = await table.minId(transaction, {
column: "created_at",
cast: "timestamptz",
startingTime: new Date("2024-06-01T00:00:00Z"),
});

expect(minId).toBe(2n);
});

test("returns null when time filter excludes all rows", async ({
transaction,
}) => {
await transaction.query(sql.unsafe`
CREATE TABLE test_table (
id BIGSERIAL PRIMARY KEY,
created_at DATE NOT NULL,
name TEXT
)
`);
await transaction.query(sql.unsafe`
INSERT INTO test_table (created_at, name) VALUES
('2024-01-01', 'old'),
('2024-03-01', 'mid'),
('2024-06-01', 'new')
`);

const table = Table.parse("test_table");
const minId = await table.minId(transaction, {
column: "created_at",
cast: "date",
startingTime: new Date("2025-01-01"),
});

expect(minId).toBeNull();
});

test("respects time filter with string IDs", async ({ transaction }) => {
await transaction.query(sql.unsafe`
CREATE TABLE test_table (
id TEXT PRIMARY KEY,
created_at DATE NOT NULL,
name TEXT
)
`);
await transaction.query(sql.unsafe`
INSERT INTO test_table (id, created_at, name) VALUES
('01ARZ3NDEKTSV4RRFFQ69G5FAA', '2024-01-01', 'old'),
('01ARZ3NDEKTSV4RRFFQ69G5FAM', '2024-06-01', 'mid'),
('01ARZ3NDEKTSV4RRFFQ69G5FAV', '2024-12-01', 'new')
`);

const table = Table.parse("test_table");
const minId = await table.minId(transaction, {
column: "created_at",
cast: "date",
startingTime: new Date("2024-06-01"),
});

expect(minId).toBe("01ARZ3NDEKTSV4RRFFQ69G5FAM");
});
});

describe("Table.columns", () => {
Expand Down
8 changes: 7 additions & 1 deletion src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,23 @@ export class Table {
const col = sql.identifier([await this.primaryKey(tx)]);

let whereClause = sql.fragment`1 = 1`;
const orderByClauses = [sql.fragment`${col} ASC`];

if (options?.column && options.cast && options.startingTime) {
const timeCol = sql.identifier([options.column]);
const startDate = formatDateForSql(options.startingTime, options.cast);

whereClause = sql.fragment`${timeCol} >= ${startDate}`;
orderByClauses.unshift(sql.fragment`${timeCol} ASC`);
}

const result = await tx.maybeOne(
sql.type(z.object({ min_id: idValueSchema }))`
SELECT MIN(${col}) AS min_id
SELECT ${col} AS min_id
FROM ${this.sqlIdentifier}
WHERE ${whereClause}
ORDER BY ${sql.join(orderByClauses, sql.fragment`, `)}
LIMIT 1
`,
);

Expand Down