oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.95k stars 94 forks source link

Question about migrations using the node-postgres adapter #325

Open philiplee13 opened 1 year ago

philiplee13 commented 1 year ago

This is more of a question than anything - but was there support for using migrations using one of the adapters?

I was trying something like the below

Folder structure

The contents of init.sql would be something like...

create schema my_schema;
create table (
   columns go here
);
insert record in my_schema.table;
import { newDb } from 'pg-mem';
const { Pool } = newDb().adapters.createPg();

let pool = new Pool();

// migrate goes here?

Got a little confused on how I would apply the migrate - from the docs here

I wasn't sure if I could do db.public.migrate({}) because wouldn't that apply to the default?

Please let me know if I need to add additional info

aminalplanet commented 1 year ago

I am using the node-postgres adapter as well, and running the migrations on the database before creating an adapter worked for me:

import { newDb } from 'pg-mem';

const db = newDb();
await db.public.migrate(/* config, etc. */);

const { Client } = db.adaptors.createPg();
const client = new Client();

// run queries, etc.

Hope this helps.

stathismor commented 10 months ago

@philiplee13 have you managed to sort this out? I have a similar issue (https://github.com/oguimbal/pg-mem/issues/364) where I cannot access the schema from the adapter query.