Skip to content

Commit 675fc98

Browse files
authored
Merge pull request #2291 from dolthub/jennifer/orm-tests
add Drizzle ORM test in client tests
2 parents b80aedc + ac935f5 commit 675fc98

5 files changed

Lines changed: 102 additions & 6 deletions

File tree

testing/PostgresDockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=linux/amd64 ubuntu:22.04
1+
FROM --platform=${BUILDPLATFORM} ubuntu:22.04
22

33
# install python, java, bats, git ruby, perl, cpan
44
ENV DEBIAN_FRONTEND=noninteractive
@@ -7,7 +7,7 @@ RUN apt update -y && \
77
curl \
88
gnupg \
99
software-properties-common && \
10-
curl -sL https://deb.nodesource.com/setup_20.x | bash - && \
10+
curl -sL https://deb.nodesource.com/setup_22.x | bash - && \
1111
add-apt-repository ppa:deadsnakes/ppa -y && \
1212
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
1313
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
@@ -44,8 +44,8 @@ RUN apt update -y && \
4444

4545
# install go
4646
WORKDIR /root
47-
ENV GO_VERSION=1.23.3
48-
ENV GOPATH=$HOME/go
47+
ENV GO_VERSION=1.25.0
48+
ENV GOPATH=/go
4949
ENV PATH=$PATH:$GOPATH/bin
5050
ENV PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
5151
RUN curl -O "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" && \
@@ -56,7 +56,7 @@ RUN curl -O "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" && \
5656
go version
5757

5858
# Setup JAVA_HOME -- useful for docker commandline
59-
ENV JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64/
59+
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/
6060

6161
# install java postgres JDBC driver
6262
RUN mkdir -p /postgres-client-tests/java
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'dotenv/config';
2+
import { defineConfig } from 'drizzle-kit';
3+
export default defineConfig({
4+
out: './drizzle',
5+
schema: './src/db/schema.ts',
6+
dialect: 'postgresql',
7+
dbCredentials: {
8+
url: process.env.DATABASE_URL!,
9+
},
10+
tablesFilter: ["!dolt_*"], // IMPORTANT to filter out dolt system tables
11+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { jsonb, integer, pgTable, varchar } from 'drizzle-orm/pg-core';
2+
3+
export const usersTable = pgTable("users", {
4+
id: integer().primaryKey(),
5+
name: varchar({ length: 255 }).notNull(),
6+
age: integer().notNull(),
7+
email: varchar({ length: 255 }).notNull().unique(),
8+
render: jsonb('render'),
9+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'dotenv/config';
2+
import { Pool } from 'pg';
3+
import { drizzle } from 'drizzle-orm/node-postgres';
4+
import { usersTable } from "./db/schema";
5+
import { eq } from 'drizzle-orm';
6+
7+
const connectionString = process.env.DATABASE_URL!
8+
const pool = new Pool({ connectionString });
9+
10+
const db = drizzle(pool);
11+
12+
async function main() {
13+
const user: typeof usersTable.$inferInsert = {
14+
id: 1,
15+
name: 'John',
16+
age: 30,
17+
render: null,
18+
email: 'john@example.com',
19+
};
20+
await db.insert(usersTable).values(user);
21+
console.log('New user created!')
22+
const users = await db.select().from(usersTable);
23+
console.log('Getting all users from the database: ', users)
24+
/*
25+
const users: {
26+
id: number;
27+
name: string;
28+
age: number;
29+
email: string;
30+
render: jsonb;
31+
}[]
32+
*/
33+
await db
34+
.update(usersTable)
35+
.set({
36+
age: 31,
37+
})
38+
.where(eq(usersTable.email, user.email));
39+
console.log('User info updated!')
40+
41+
// Verify the update
42+
const result = await db
43+
.select()
44+
.from(usersTable)
45+
.where(eq(usersTable.age, 31));
46+
console.log('Retrieved updated record:', result);
47+
48+
// Clean up
49+
await db.delete(usersTable).where(eq(usersTable.email, user.email));
50+
console.log('User deleted!')
51+
}
52+
53+
main();

testing/postgres-client-tests/postgres-client-tests.bats

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,27 @@ teardown() {
7676
@test "python postgres: sqlalcchemy client" {
7777
cd $BATS_TEST_DIRNAME/python
7878
python3 sqlalchemy-test.py $USER $PORT
79-
}
79+
}
80+
81+
@test "Drizzle ORM smoke test" {
82+
# the schema should be empty
83+
query_server -c "DROP TABLE IF EXISTS test_table" -t
84+
85+
cd $BATS_TEST_DIRNAME/drizzle
86+
87+
# Construct the string and append it to the .env file
88+
touch .env
89+
echo "DATABASE_URL=postgres://$USER:password@localhost:$PORT/postgres" >> .env
90+
91+
npm i drizzle-orm pg dotenv
92+
npm i -D drizzle-kit tsx @types/pg
93+
npx drizzle-kit push
94+
95+
# we can check if 'components table was created'
96+
query_server -c "SELECT * FROM users" -t
97+
run query_server -c "SELECT * FROM users" -t
98+
[ "$status" -eq 0 ]
99+
100+
# this inserts and updates row and check its updated result
101+
npx tsx src/index.ts
102+
}

0 commit comments

Comments
 (0)