@@ -20,8 +20,14 @@ import {
2020 DbBnsSubdomain ,
2121 DbTokenOfferingLocked ,
2222} from '../datastore/common' ;
23- import { PgDataStore , cycleMigrations , runMigrations } from '../datastore/postgres-store' ;
23+ import {
24+ PgDataStore ,
25+ cycleMigrations ,
26+ runMigrations ,
27+ getPgClientConfig ,
28+ } from '../datastore/postgres-store' ;
2429import { PoolClient } from 'pg' ;
30+ import * as pgConnectionString from 'pg-connection-string' ;
2531import { parseDbEvent } from '../api/controllers/db-controller' ;
2632import * as assert from 'assert' ;
2733import { I32_MAX } from '../helpers' ;
@@ -55,6 +61,41 @@ describe('in-memory datastore', () => {
5561 } ) ;
5662} ) ;
5763
64+ function testEnvVars ( envVars : Record < string , string | undefined > , use : ( ) => void ) : void ;
65+ function testEnvVars (
66+ envVars : Record < string , string | undefined > ,
67+ use : ( ) => Promise < void >
68+ ) : Promise < void > ;
69+ function testEnvVars (
70+ envVars : Record < string , string | undefined > ,
71+ use : ( ) => void | Promise < void >
72+ ) : void | Promise < void > {
73+ const existing = Object . fromEntries (
74+ Object . keys ( envVars )
75+ . filter ( k => k in process . env )
76+ . map ( k => [ k , process . env [ k ] ] )
77+ ) ;
78+ const added = Object . keys ( envVars ) . filter ( k => ! ( k in process . env ) ) ;
79+ Object . entries ( envVars ) . forEach ( ( [ k , v ] ) => {
80+ process . env [ k ] = v ;
81+ if ( v === undefined ) {
82+ delete process . env [ k ] ;
83+ }
84+ } ) ;
85+ const restoreEnvVars = ( ) => {
86+ added . forEach ( k => delete process . env [ k ] ) ;
87+ Object . entries ( existing ) . forEach ( ( [ k , v ] ) => ( process . env [ k ] = v ) ) ;
88+ } ;
89+ try {
90+ const runFn = use ( ) ;
91+ if ( runFn instanceof Promise ) {
92+ return runFn . finally ( ( ) => restoreEnvVars ( ) ) ;
93+ }
94+ } finally {
95+ restoreEnvVars ( ) ;
96+ }
97+ }
98+
5899describe ( 'postgres datastore' , ( ) => {
59100 let db : PgDataStore ;
60101 let client : PoolClient ;
@@ -66,6 +107,81 @@ describe('postgres datastore', () => {
66107 client = await db . pool . connect ( ) ;
67108 } ) ;
68109
110+ test ( 'postgres uri config' , ( ) => {
111+ const uri =
112+ 'postgresql://test_user:[email protected] :3211/test_db?ssl=true¤tSchema=test_schema' ; 113+ testEnvVars (
114+ {
115+ PG_CONNECTION_URI : uri ,
116+ PG_DATABASE : undefined ,
117+ PG_USER : undefined ,
118+ PG_PASSWORD : undefined ,
119+ PG_HOST : undefined ,
120+ PG_PORT : undefined ,
121+ PG_SSL : undefined ,
122+ PG_SCHEMA : undefined ,
123+ } ,
124+ ( ) => {
125+ const config = getPgClientConfig ( ) ;
126+ const parsedUrl = pgConnectionString . parse ( uri ) ;
127+ expect ( parsedUrl . database ) . toBe ( 'test_db' ) ;
128+ expect ( parsedUrl . user ) . toBe ( 'test_user' ) ;
129+ expect ( parsedUrl . password ) . toBe ( 'secret_password' ) ;
130+ expect ( parsedUrl . host ) . toBe ( 'database.server.com' ) ;
131+ expect ( parsedUrl . port ) . toBe ( '3211' ) ;
132+ expect ( parsedUrl . ssl ) . toBe ( true ) ;
133+ expect ( config . schema ) . toBe ( 'test_schema' ) ;
134+ }
135+ ) ;
136+ } ) ;
137+
138+ test ( 'postgres env var config' , ( ) => {
139+ testEnvVars (
140+ {
141+ PG_CONNECTION_URI : undefined ,
142+ PG_DATABASE : 'pg_db_db1' ,
143+ PG_USER : 'pg_user_user1' ,
144+ PG_PASSWORD : 'pg_password_password1' ,
145+ PG_HOST : 'pg_host_host1' ,
146+ PG_PORT : '9876' ,
147+ PG_SSL : 'true' ,
148+ PG_SCHEMA : 'pg_schema_schema1' ,
149+ } ,
150+ ( ) => {
151+ const config = getPgClientConfig ( ) ;
152+ expect ( config . database ) . toBe ( 'pg_db_db1' ) ;
153+ expect ( config . user ) . toBe ( 'pg_user_user1' ) ;
154+ expect ( config . password ) . toBe ( 'pg_password_password1' ) ;
155+ expect ( config . host ) . toBe ( 'pg_host_host1' ) ;
156+ expect ( config . port ) . toBe ( 9876 ) ;
157+ expect ( config . ssl ) . toBe ( true ) ;
158+ expect ( config . schema ) . toBe ( 'pg_schema_schema1' ) ;
159+ }
160+ ) ;
161+ } ) ;
162+
163+ test ( 'postgres conflicting config' , ( ) => {
164+ const uri =
165+ 'postgresql://test_user:[email protected] :3211/test_db?ssl=true¤tSchema=test_schema' ; 166+ testEnvVars (
167+ {
168+ PG_CONNECTION_URI : uri ,
169+ PG_DATABASE : 'pg_db_db1' ,
170+ PG_USER : 'pg_user_user1' ,
171+ PG_PASSWORD : 'pg_password_password1' ,
172+ PG_HOST : 'pg_host_host1' ,
173+ PG_PORT : '9876' ,
174+ PG_SSL : 'true' ,
175+ PG_SCHEMA : 'pg_schema_schema1' ,
176+ } ,
177+ ( ) => {
178+ expect ( ( ) => {
179+ const config = getPgClientConfig ( ) ;
180+ } ) . toThrowError ( ) ;
181+ }
182+ ) ;
183+ } ) ;
184+
69185 test ( 'pg address STX balances' , async ( ) => {
70186 const dbBlock : DbBlock = {
71187 block_hash : '0x9876' ,
0 commit comments