Skip to content

Commit

Permalink
fix: docker compose failing to build and spark failing to start
Browse files Browse the repository at this point in the history
- moved isTestMode from the tests.ts library to the server.ts library to circumvent any import errors.
- programmatically import in-memory database only if the server has been detected to be running in test mode.
- fix creation of citext extension on the in-memory database by including a statement before migration.
- programmatically import the redis memory server only if the server is running in the test mode.
- added build-essential to the Dockerfile to allow for the make command to work when installing dependencies.
- made the spark docker container depend on the database and keydb containers.
- created a postgres spark user for spark related tasks instead of using the root/postgres user.
  • Loading branch information
Creaous committed Dec 7, 2024
1 parent 030e886 commit ca02737
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ WORKDIR /usr/src/app
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json bun.lockb /temp/dev/
# required in order for redis-memory-store to be installed
RUN apt-get update && apt-get install -y build-essential
RUN cd /temp/dev && bun install --frozen-lockfile

# install with --production (exclude devDependencies)
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ services:
- '3005:3005'
volumes:
- ./.env:/usr/src/app/.env
depends_on:
- postgres
- keydb
restart: unless-stopped
networks:
- spark
Expand All @@ -26,6 +29,7 @@ services:
container_name: postgres
environment:
- POSTGRES_DB=spark
- POSTGRES_USER=spark
- POSTGRES_PASSWORD=postgres
volumes:
- ./docker-data/postgres_data:/var/lib/postgresql/data
Expand Down
29 changes: 23 additions & 6 deletions src/drizzle/db.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { PGlite as testClient } from '@electric-sql/pglite'; // for unit tests use
import { drizzle as prodDrizzle } from 'drizzle-orm/node-postgres'; // for production use
import { drizzle as testDrizzle } from 'drizzle-orm/pglite'; // for unit tests use
import { Client as prodClient } from 'pg'; // for production use
import * as schema from './schema'; // get all the schema

export const prodDbClient = new prodClient({
connectionString: process.env.DATABASE_URL as string
});

export const db =
process.env.NODE_ENV !== 'test'
? prodDrizzle(prodDbClient, { schema }) // we want to use a real database
: testDrizzle(new testClient(), { schema }); // we want to use a in-memory database
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import { PgliteDatabase } from 'drizzle-orm/pglite';

let db:
| (NodePgDatabase<typeof schema> & { $client: prodClient })
| (PgliteDatabase<typeof schema> & { $client: any });

if (process.env.NODE_ENV !== 'test') {
db = prodDrizzle(prodDbClient, { schema }); // we want to use a real database
} else {
import('@electric-sql/pglite').then(({ PGlite: testClient }) => {
import('drizzle-orm/pglite').then(({ drizzle: testDrizzle }) => {
// @ts-ignore - TODO: fix later
import('@electric-sql/pglite/contrib/citext').then(({ citext }) => {
db = testDrizzle(new testClient({ extensions: { citext } }), {
schema
}); // we want to use an in-memory database
});
});
});
}

export { db };
11 changes: 9 additions & 2 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { postMedia, user } from '../drizzle/schema';
import { syncClient, tokenClient } from '../redis';
import { authorize } from './authentication';
import { convertModelToUser, getHashedPk, internalUsers } from './authentik';
import { isTestMode } from './tests';

/**
* "Legacy" endpoint for uploading media.
Expand Down Expand Up @@ -368,4 +367,12 @@ async function createUsersFromRedisTokens() {
);
}

export { createUsersFromRedisTokens, mediaUploadEndpoint, webhookEndpoint };
// Just a shortcut for checking if we are in test mode.
const isTestMode = process.env.NODE_ENV === 'test';

export {
createUsersFromRedisTokens,
mediaUploadEndpoint,
webhookEndpoint,
isTestMode
};
3 changes: 0 additions & 3 deletions src/lib/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,3 @@ export async function removeUser(sub: string) {

return tokenClient.del(`tokens:${sub}`);
}

// Just a shortcut for checking if we are in test mode.
export const isTestMode = process.env.NODE_ENV === 'test';
11 changes: 7 additions & 4 deletions src/redis.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { createClient } from 'redis';
import RedisMemoryServer from 'redis-memory-server';
import { isTestMode } from './lib/tests';
import { isTestMode } from './lib/server';

if (isTestMode) {
const testRedisClient = new RedisMemoryServer({ instance: { port: 1234 } });
testRedisClient.start();
import('redis-memory-server').then(({ default: RedisMemoryServer }) => {
const testRedisClient = new RedisMemoryServer({
instance: { port: 1234 }
});
testRedisClient.start();
});
}

// Create a Redis client using the createClient function.
Expand Down
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
mediaUploadEndpoint,
webhookEndpoint
} from './lib/server';
import { isTestMode } from './lib/tests';
import { isTestMode } from './lib/server';
import { redisClient, syncClient, tokenClient } from './redis';
import { schema } from './schema';
import { enableAll } from './lib/logger';
Expand Down Expand Up @@ -94,6 +94,7 @@ export async function startServer() {
await createUsersFromRedisTokens();
} else {
// Migrate the database.
await db.execute(sql`CREATE EXTENSION IF NOT EXISTS citext;`);
await migrate(db, { migrationsFolder: './drizzle' });
}

Expand Down

0 comments on commit ca02737

Please sign in to comment.