Обсуждение: Scripting with subdirectories - how to specify script locations.

Поиск
Список
Период
Сортировка

Scripting with subdirectories - how to specify script locations.

От
Pól Ua Laoínecháin
Дата:
Hi all,

I like splitting scripts up into sub-units - painful at first, but I
find that it pays off in the long run - modularity &c.

I have a series of scripts for a relatively simple system as follows:

├── 01_create_database.sql
├── 02_create_schemas.sql
├── tab_create
│   ├── 01_create_tables.sql
│   ├── create.hr.employees.sql
│   └── create.production.suppliers.sql
└── tab_data
    ├── 01_populate_tables.sql
    ├── populate.hr.employees.sql
    └── populate.production.suppliers.sql


So, I have my "master" script - 01_create_database.sql - as follows:

===========================
CREATE DATABASE w_test;
\c w_test;

\i 02_create_schemas.sql;    -- the schemas are created no problem...

\i ./tab_create/01_create_tables.sql;
\i tab_data/populate_tables.sql;
================================

Now, in the tab_create directory, I have 01_create_tables.sql as follows:

[pol@fedora tab_create]$ more 01_create_tables.sql

===========================
\i ./create.hr.employees.sql;

\i ./create.production.suppliers.sql;
==============================

But, when running \i 01_create_database.sql I get:

===============================
test=# \i 01_create_database.sql
CREATE DATABASE
You are now connected to database "w_test" as user "pol".
CREATE SCHEMA
CREATE SCHEMA
CREATE SCHEMA
CREATE SCHEMA
psql:./tab_create/01_create_tables.sql:2: error:
./create.hr.employees.sql: No such file or directory
psql:./tab_create/01_create_tables.sql:4: error:
./create.production.suppliers.sql: No such file or directory
psql:01_create_database.sql:17: error: tab_data/populate_tables.sql:
No such file or directory
w_test=#
==================================

So, my schemas are created and my script goes to 01_create_tables.sql
- *_BUT_* then it can't find my create.hr.employees.sql script or the
other one.

I have tried with just putting create.hr.employees.sql instead of ./create...

I know I'm probably missing something basic - could some kind soul
point me in the right direction?

TIA and rgs,


Pól...



Re: Scripting with subdirectories - how to specify script locations.

От
"David G. Johnston"
Дата:

On Wednesday, June 2, 2021, Pól Ua Laoínecháin <linehanp@tcd.ie> wrote:

├── tab_create
│   ├── 01_create_tables.sql
│   ├── create.hr.employees.sql
│   └── create.production.suppliers.sql

===========================
\i ./create.hr.employees.sql;

\i ./create.production.suppliers.sql;
==============================

I know I'm probably missing something basic - could some kind soul
point me in the right direction?

\ir is “include relative”, makes the cwd the directory of the active script instead lf where psql was run from.

David J. 

Re: Scripting with subdirectories - how to specify script locations.

От
Pól Ua Laoínecháin
Дата:
>> I know I'm probably missing something basic - could some kind soul
>> point me in the right direction?

> \ir is “include relative”, makes the cwd the directory of the active script instead lf where psql was run from.


Thanks - that did the trick - much nicer than cluttering up the
scripts with cd commands.

Rgs,



Pól...

> David J.