eradman / ephemeralpg

Quickly spin up a temporary PostgreSQL test databases
http://eradman.com/ephemeralpg/
Other
188 stars 3 forks source link

Purpose of `NEW` file #7

Closed aleclarson closed 2 hours ago

aleclarson commented 3 hours ago

Hey! I'm porting your awesome utility to Node.js and I have a question about the NEW file.

Correct me if I'm wrong, but it appears that its absence will prevent reuse of an existing “datadir” (a temporary data directory). Does that mean once a datadir is “started” once, it can't be automatically reused ever again? (You know, because there won't be a NEW file present anymore.)

If that's true, that would mean calling pg_tmp start 3 times in a row would create 3 separate temporary databases. Is that correct (and intentional)?

Sorry if this all seems basic, but I'm making sure I understand it clearly. 😄

Related lines

Inside initdb: https://github.com/eradman/ephemeralpg/blob/ade77fce1594484039406b6b04ef1907540587c2/pg_tmp.sh#L65

Inside start: https://github.com/eradman/ephemeralpg/blob/ade77fce1594484039406b6b04ef1907540587c2/pg_tmp.sh#L72-L78

aleclarson commented 3 hours ago

I'm also not clear on why $0 initdb is executed twice here:

https://github.com/eradman/ephemeralpg/blob/ade77fce1594484039406b6b04ef1907540587c2/pg_tmp.sh#L77-L78

EDIT: Nevermind, the git blame helped explain. It's initializing another database in advance for future calls to pg_tmp start. Very cool! 😄

eradman commented 2 hours ago

Hey! I'm porting your awesome utility to Node.js

Cool!

and I have a question about the NEW file.

Correct me if I'm wrong, but it appears that its absence will prevent reuse of an existing “datadir” (a temporary data directory). Does that mean once a datadir is “started” once, it can't be automatically reused ever again? (You know, because there won't be a NEW file present anymore.)

Yes, the NEW file is used to indicate that pg_tmp can grab this directory and start PostgreSQL, and other invocations of the script will ignore this directory.

aleclarson commented 2 hours ago

Appreciate the quick reply!

To summarize what I've learned, the NEW file helps facilitate the optimistic pg_tmp initdb call that occurs in the background whenever pg_tmp start runs without an explicit data directory provided.

Thanks for making ephemeralpg! :)