Fwd: psql include file using relative path

From: Gurjeet Singh <singh(dot)gurjeet(at)gmail(dot)com>
To: PGSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Fwd: psql include file using relative path
Date: 2011-02-24 23:21:39
Message-ID: AANLkTinVf1FwKKm61AWZLSTJ3Q8swOAxJXDHvWupBPdq@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

psql has the ability to execute commands from a file, but if one wishes
to develop and provide a modularized set of sql files, then psql is not very
helpful because the \i command can open file paths either if they are
absolute paths or if they are palced correctly relative to psql's current
working directory.

Attached patch adds a new meta-command to psql, '\ir' that allows the user
to process files relative to currently processing file.

Also attached is a sample use case ir_sample. One can extract it _anywhere_
on the filesystem and invoke psql with the path to main.sql and the rest of
the files will be automatically included from that location.

Sample session:

[/tmp]$ psql -f ~/dev/ir_sample/main.sql
processing main.sql
BEGIN
processing subdir1/1.sql
processing subdir1/2.sql
processing subdir2/1.sql
processing subdir2/subdir2.1/1.sql
processing subdir2/2.sql
processing subdir2/3.sql
COMMIT

And here's what the sample's directory structure and files look like:

[ir_sample]$ find ./ -name "*.sql" | while read f; do echo === $f ====; cat
$f; done
=== ./main.sql ====
\echo processing main.sql
BEGIN;
\ir subdir1/1.sql
\ir subdir1/2.sql
\ir subdir2/1.sql
\ir subdir2/2.sql
\ir subdir2/3.sql
COMMIT;
=== ./subdir1/1.sql ====
\echo processing subdir1/1.sql
=== ./subdir1/2.sql ====
\echo processing subdir1/2.sql
=== ./subdir2/subdir2.1/1.sql ====
\echo processing subdir2/subdir2.1/1.sql
=== ./subdir2/1.sql ====
\echo processing subdir2/1.sql
\ir subdir2.1/1.sql
=== ./subdir2/2.sql ====
\echo processing subdir2/2.sql
=== ./subdir2/3.sql ====
\echo processing subdir2/3.sql

Regards,
--
Gurjeet Singh
EnterpriseDB Corporation <http://www.enterprisedb.com/>
The Enterprise PostgreSQL <http://www.postgresql.org/> Company

Attachment Content-Type Size
psql_ir.patch text/x-patch 4.3 KB
ir_sample.tgz application/x-gzip 422 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2011-02-24 23:36:03 Re: wCTE behaviour
Previous Message Josh Berkus 2011-02-24 20:42:39 Re: WIP: cross column correlation ...