Skip to content

Commit cfed15e

Browse files
committed
[drizzle-seed] removed docker container creation from pg-postgis data types tests
1 parent 92f3ae0 commit cfed15e

File tree

5 files changed

+40
-80
lines changed

5 files changed

+40
-80
lines changed

compose/dockers.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ docker run -d --name singlestoredb-dev \
2828
-e ROOT_PASSWORD="password" \
2929
--platform linux/amd64 \
3030
-p 3306:3306 -p 8080:8080 -p 9000:9000 \
31-
ghcr.io/singlestore-labs/singlestoredb-dev:latest
31+
ghcr.io/singlestore-labs/singlestoredb-dev:latest
32+
# if the command above doesn't work for you on mac m1, try using version 0.2.57 of docker image.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { geometry, pgSchema } from 'drizzle-orm/pg-core';
1+
import { geometry, pgTable } from 'drizzle-orm/pg-core';
22

3-
export const schema = pgSchema('seeder_lib_pg');
4-
5-
export const allDataTypes = schema.table('postgis_data_types', {
3+
export const allDataTypes = pgTable('postgis_data_types', {
64
geometry: geometry('geometry', { type: 'point', mode: 'tuple', srid: 0 }),
75
});
86

9-
export const allArrayDataTypes = schema.table('postgis_array_data_types', {
7+
export const allArrayDataTypes = pgTable('postgis_array_data_types', {
108
geometryArray: geometry('geometry_array', { type: 'point', mode: 'tuple', srid: 0 }).array(1),
119
});

drizzle-seed/tests/pg/allDataTypesTest/pgSchema.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
date,
88
decimal,
99
doublePrecision,
10-
// geometry,
1110
inet,
1211
integer,
1312
interval,
@@ -16,7 +15,7 @@ import {
1615
line,
1716
numeric,
1817
pgEnum,
19-
pgSchema,
18+
pgTable,
2019
point,
2120
real,
2221
serial,
@@ -30,11 +29,9 @@ import {
3029
vector,
3130
} from 'drizzle-orm/pg-core';
3231

33-
export const schema = pgSchema('seeder_lib_pg');
34-
3532
export const moodEnum = pgEnum('mood_enum', ['sad', 'ok', 'happy']);
3633

37-
export const allDataTypes = schema.table('all_data_types', {
34+
export const allDataTypes = pgTable('all_data_types', {
3835
integer: integer('integer'),
3936
smallint: smallint('smallint'),
4037
biginteger: bigint('bigint', { mode: 'bigint' }),
@@ -71,7 +68,7 @@ export const allDataTypes = schema.table('all_data_types', {
7168
vector: vector('vector', { dimensions: 3 }),
7269
});
7370

74-
export const allArrayDataTypes = schema.table('all_array_data_types', {
71+
export const allArrayDataTypes = pgTable('all_array_data_types', {
7572
integerArray: integer('integer_array').array(),
7673
smallintArray: smallint('smallint_array').array(),
7774
bigintegerArray: bigint('bigint_array', { mode: 'bigint' }).array(),
@@ -103,14 +100,14 @@ export const allArrayDataTypes = schema.table('all_array_data_types', {
103100
// geometryArray: geometry('geometry_array', { type: 'point', mode: 'tuple', srid: 0 }).array(1),
104101
});
105102

106-
export const ndArrays = schema.table('nd_arrays', {
103+
export const ndArrays = pgTable('nd_arrays', {
107104
integer1DArray: integer('integer_1d_array').array(3),
108105
integer2DArray: integer('integer_2d_array').array(3).array(4),
109106
integer3DArray: integer('integer_3d_array').array(3).array(4).array(5),
110107
integer4DArray: integer('integer_4d_array').array(3).array(4).array(5).array(6),
111108
});
112109

113-
export const intervals = schema.table('intervals', {
110+
export const intervals = pgTable('intervals', {
114111
intervalYear: interval({ fields: 'year' }),
115112
intervalYearToMonth: interval({ fields: 'year to month' }),
116113
intervalMonth: interval({ fields: 'month' }),

drizzle-seed/tests/pg/allDataTypesTest/pg_all_data_types.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ beforeAll(async () => {
1919

2020
db = drizzle({ client });
2121

22-
await db.execute(sql`CREATE SCHEMA if not exists "seeder_lib_pg";`);
23-
2422
await db.execute(
2523
sql`
2624
DO $$ BEGIN
27-
CREATE TYPE "seeder_lib_pg"."mood_enum" AS ENUM('sad', 'ok', 'happy');
25+
CREATE TYPE "mood_enum" AS ENUM('sad', 'ok', 'happy');
2826
EXCEPTION
2927
WHEN duplicate_object THEN null;
3028
END $$;
@@ -33,7 +31,7 @@ beforeAll(async () => {
3331

3432
await db.execute(
3533
sql`
36-
CREATE TABLE IF NOT EXISTS "seeder_lib_pg"."all_data_types" (
34+
CREATE TABLE IF NOT EXISTS "all_data_types" (
3735
"integer" integer,
3836
"smallint" smallint,
3937
"bigint" bigint,
@@ -63,7 +61,7 @@ beforeAll(async () => {
6361
"point_tuple" "point",
6462
"line" "line",
6563
"line_tuple" "line",
66-
"mood_enum" "seeder_lib_pg"."mood_enum",
64+
"mood_enum" "mood_enum",
6765
"uuid" "uuid",
6866
"inet" inet,
6967
"vector" vector(3)
@@ -73,7 +71,7 @@ beforeAll(async () => {
7371

7472
await db.execute(
7573
sql`
76-
CREATE TABLE IF NOT EXISTS "seeder_lib_pg"."all_array_data_types" (
74+
CREATE TABLE IF NOT EXISTS "all_array_data_types" (
7775
"integer_array" integer[],
7876
"smallint_array" smallint[],
7977
"bigint_array" bigint[],
@@ -99,7 +97,7 @@ beforeAll(async () => {
9997
"point_tuple_array" "point"[],
10098
"line_array" "line"[],
10199
"line_tuple_array" "line"[],
102-
"mood_enum_array" "seeder_lib_pg"."mood_enum"[],
100+
"mood_enum_array" "mood_enum"[],
103101
"uuid_array" uuid[],
104102
"inet_array" inet[]
105103
);
@@ -108,7 +106,7 @@ beforeAll(async () => {
108106

109107
await db.execute(
110108
sql`
111-
CREATE TABLE IF NOT EXISTS "seeder_lib_pg"."nd_arrays" (
109+
CREATE TABLE IF NOT EXISTS "nd_arrays" (
112110
"integer_1d_array" integer[3],
113111
"integer_2d_array" integer[3][4],
114112
"integer_3d_array" integer[3][4][5],
@@ -119,7 +117,7 @@ beforeAll(async () => {
119117

120118
await db.execute(
121119
sql`
122-
CREATE TABLE IF NOT EXISTS "seeder_lib_pg"."intervals" (
120+
CREATE TABLE IF NOT EXISTS "intervals" (
123121
"intervalYear" interval year,
124122
"intervalYearToMonth" interval year to month,
125123
"intervalMonth" interval month,

drizzle-seed/tests/pg/allDataTypesTest/postgis_data_types.test.ts

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,42 @@
1-
import type { Container } from 'dockerode';
21
import { sql } from 'drizzle-orm';
3-
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
4-
import { drizzle } from 'drizzle-orm/node-postgres';
5-
import type { Client as ClientT } from 'pg';
6-
import pg from 'pg';
7-
import { afterAll, beforeAll, expect, test } from 'vitest';
2+
import { expect } from 'vitest';
83
import { seed } from '../../../src/index.ts';
9-
import { createDockerPostgis } from '../utils.ts';
4+
import { pgPostgisTest as test } from '../instrumentation.ts';
105
import * as schema from './pgPostgisSchema.ts';
116

12-
const { Client } = pg;
13-
14-
let pgContainer: Container;
15-
let pgClient: ClientT;
16-
let db: NodePgDatabase;
17-
18-
beforeAll(async () => {
19-
const { url, container } = await createDockerPostgis();
20-
pgContainer = container;
21-
const sleep = 1000;
22-
let timeLeft = 40000;
23-
let connected = false;
24-
let lastError;
25-
26-
do {
27-
try {
28-
pgClient = new Client({ connectionString: url });
29-
await pgClient.connect();
30-
connected = true;
31-
break;
32-
} catch (e) {
33-
lastError = e;
34-
await new Promise((resolve) => setTimeout(resolve, sleep));
35-
timeLeft -= sleep;
36-
}
37-
} while (timeLeft > 0);
38-
if (!connected) {
39-
console.error('Cannot connect to Postgres');
40-
await pgClient!.end().catch(console.error);
41-
await pgContainer?.stop().catch(console.error);
42-
throw lastError;
43-
}
44-
45-
await pgClient.query(`CREATE EXTENSION IF NOT EXISTS postgis;`);
46-
47-
db = drizzle({ client: pgClient });
7+
let firstTime = true;
8+
let resolveFunc: (val: any) => void;
9+
const promise = new Promise((resolve) => {
10+
resolveFunc = resolve;
11+
});
12+
test.beforeEach(async ({ db }) => {
13+
if (firstTime) {
14+
firstTime = false;
4815

49-
await db.execute(sql`CREATE SCHEMA if not exists "seeder_lib_pg";`);
16+
await db.execute(sql`CREATE EXTENSION IF NOT EXISTS postgis;`);
5017

51-
await db.execute(
52-
sql`
53-
CREATE TABLE IF NOT EXISTS "seeder_lib_pg"."postgis_data_types" (
18+
await db.execute(
19+
sql`
20+
CREATE TABLE IF NOT EXISTS "postgis_data_types" (
5421
"geometry" geometry(point, 0)
5522
);
5623
`,
57-
);
24+
);
5825

59-
await db.execute(
60-
sql`
61-
CREATE TABLE IF NOT EXISTS "seeder_lib_pg"."postgis_array_data_types" (
26+
await db.execute(
27+
sql`
28+
CREATE TABLE IF NOT EXISTS "postgis_array_data_types" (
6229
"geometry_array" geometry(point, 0)[]
6330
);
6431
`,
65-
);
66-
});
32+
);
6733

68-
afterAll(async () => {
69-
await pgClient.end().catch(console.error);
70-
await pgContainer.stop().catch(console.error);
34+
resolveFunc('');
35+
}
36+
await promise;
7137
});
7238

73-
test('postgis data types test', async () => {
39+
test('postgis data types test', async ({ db }) => {
7440
await seed(db, { allDataTypes: schema.allDataTypes }, { count: 10000 });
7541

7642
const allDataTypes = await db.select().from(schema.allDataTypes);

0 commit comments

Comments
 (0)