Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Set precedence to DATABASE_URL over POSTHOG_DB_* fields (#441)
Browse files Browse the repository at this point in the history
* set precedence to DATABASE_URL

* make DATABASE_URL non mandatory, check env var configuration

* fix tests

* address comments

* check what went wrong

* Remove extra cat

馃樉

Co-authored-by: Michael Matloka <dev@twixes.com>
  • Loading branch information
neilkakkar and Twixes committed Jun 4, 2021
1 parent 13ed91b commit 14038ae
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ There's a multitude of settings you can use to control the plugin server. Use th
| KAFKA_PRODUCER_MAX_QUEUE_SIZE | Kafka producer batch max size before flushing | `20` |
| KAFKA_FLUSH_FREQUENCY_MS | Kafka producer batch max duration before flushing | `500` |
| KAFKA_MAX_MESSAGE_BATCH_SIZE | Kafka producer batch max size in bytes before flushing | `900000` |
| LOG_LEVEL | minimum log level | `LogLevel.Info` |
| LOG_LEVEL | minimum log level | `'info'` |
| SENTRY_DSN | Sentry ingestion URL | `null` |
| STATSD_HOST | StatsD host - integration disabled if this is not provided | `null` |
| STATSD_PORT | StatsD port | `8125` |
Expand Down
11 changes: 8 additions & 3 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import os from 'os'

import { LogLevel, PluginsServerConfig } from '../types'
import { stringToBoolean } from '../utils/utils'
import { determineNodeEnv, NodeEnv, stringToBoolean } from '../utils/utils'
import { KAFKA_EVENTS_PLUGIN_INGESTION } from './kafka-topics'

export const defaultConfig = overrideWithEnv(getDefaultConfig())
export const configHelp = getConfigHelp()

export function getDefaultConfig(): PluginsServerConfig {
const isTestEnv = process.env.NODE_ENV === 'test'
const isTestEnv = determineNodeEnv() === NodeEnv.Test
const isDevEnv = determineNodeEnv() === NodeEnv.Development
const coreCount = os.cpus().length

return {
CELERY_DEFAULT_QUEUE: 'celery',
DATABASE_URL: isTestEnv ? 'postgres://localhost:5432/test_posthog' : 'postgres://localhost:5432/posthog',
DATABASE_URL: isTestEnv
? 'postgres://localhost:5432/test_posthog'
: isDevEnv
? 'postgres://localhost:5432/posthog'
: null,
POSTHOG_DB_NAME: null,
POSTHOG_DB_USER: 'postgres',
POSTHOG_DB_PASSWORD: '',
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface PluginsServerConfig extends Record<string, any> {
TASKS_PER_WORKER: number
TASK_TIMEOUT: number
CELERY_DEFAULT_QUEUE: string
DATABASE_URL: string
DATABASE_URL: string | null
POSTHOG_DB_NAME: string | null
POSTHOG_DB_USER: string
POSTHOG_DB_PASSWORD: string
Expand Down
15 changes: 10 additions & 5 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,22 +425,27 @@ export function createPostgresPool(
configOrDatabaseUrl: PluginsServerConfig | string,
onError?: (error: Error) => any
): Pool {
if (typeof configOrDatabaseUrl !== 'string') {
if (!configOrDatabaseUrl.DATABASE_URL && !configOrDatabaseUrl.POSTHOG_DB_NAME) {
throw new Error('Invalid configuration for Postgres: either DATABASE_URL or POSTHOG_DB_NAME required')
}
}
const credentials: Partial<PoolConfig> =
typeof configOrDatabaseUrl === 'string'
? {
connectionString: configOrDatabaseUrl,
}
: configOrDatabaseUrl.POSTHOG_DB_NAME
: configOrDatabaseUrl.DATABASE_URL
? {
database: configOrDatabaseUrl.POSTHOG_DB_NAME,
connectionString: configOrDatabaseUrl.DATABASE_URL,
}
: {
database: configOrDatabaseUrl.POSTHOG_DB_NAME ?? undefined,
user: configOrDatabaseUrl.POSTHOG_DB_USER,
password: configOrDatabaseUrl.POSTHOG_DB_PASSWORD,
host: configOrDatabaseUrl.POSTHOG_POSTGRES_HOST,
port: configOrDatabaseUrl.POSTHOG_POSTGRES_PORT,
}
: {
connectionString: configOrDatabaseUrl.DATABASE_URL,
}

const pgPool = new Pool({
...credentials,
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/graphile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Pool } from 'pg'
import { PluginsServerConfig } from '../../src/types'

export async function resetGraphileSchema(serverConfig: PluginsServerConfig): Promise<void> {
const graphileUrl = serverConfig.JOB_QUEUE_GRAPHILE_URL || serverConfig.DATABASE_URL
const graphileUrl = serverConfig.JOB_QUEUE_GRAPHILE_URL || serverConfig.DATABASE_URL!
const db = new Pool({ connectionString: graphileUrl })

try {
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function resetTestDatabase(
extraRows: ExtraDatabaseRows = {}
): Promise<void> {
const config = { ...defaultConfig, ...extraServerConfig }
const db = new Pool({ connectionString: config.DATABASE_URL })
const db = new Pool({ connectionString: config.DATABASE_URL! })
try {
await db.query('DELETE FROM ee_hook')
} catch {}
Expand Down Expand Up @@ -224,7 +224,7 @@ export function onQuery(hub: Hub, onQueryCallback: (queryText: string) => any):
}

export async function getErrorForPluginConfig(id: number): Promise<any> {
const db = new Pool({ connectionString: defaultConfig.DATABASE_URL })
const db = new Pool({ connectionString: defaultConfig.DATABASE_URL! })
let error
try {
const response = await db.query('SELECT * FROM posthog_pluginconfig WHERE id = $1', [id])
Expand Down

0 comments on commit 14038ae

Please sign in to comment.