dbcli / pgcli

Postgres CLI with autocompletion and syntax highlighting
http://pgcli.com
BSD 3-Clause "New" or "Revised" License
12.11k stars 557 forks source link

\i does not match psql functionality #937

Open garyhurtz opened 6 years ago

garyhurtz commented 6 years ago

Description

In psql I can use \i to load a file which contains a series of additional \i commands. This makes it simple to run commands from a single file, yet also compile groups of commands together into "parent" files that call a series of "children" files (for example, while setting up and debugging a new database).

In pgcli, running \i simply dumps a long string containing the concatenation of all other files to the screen, and stops.

Your environment

OS: Ubuntu 18.04 Server: PostgreSQL 9.6.9 Version: 1.10.3

pip freeze: argh==0.26.2 asn1crypto==0.24.0 atomicwrites==1.2.1 attrs==18.2.0 awscli==1.16.7 awsebcli==3.14.4 beautifulsoup4==4.6.3 bleach==2.1.4 blessed==1.15.0 blinker==1.4 boto3==1.8.7 botocore==1.11.7 bs4==0.0.1 cached-property==1.4.3 cement==2.8.2 certifi==2018.8.24 cffi==1.11.5 chardet==3.0.4 cli-helpers==1.0.2 click==6.7 colorama==0.3.9 configobj==5.0.6 coverage==4.5.1 cryptography==2.3.1 csscompressor==0.9.5 docker==3.5.0 docker-compose==1.21.2 docker-pycreds==0.3.0 dockerpty==0.4.1 docopt==0.6.2 docutils==0.14 Flask==1.0.2 future==0.16.0 glob2==0.6 html5lib==1.0.1 humanize==0.5.1 idna==2.6 itsdangerous==0.24 Jinja2==2.10 jmespath==0.9.3 jsonschema==2.6.0 Mako==1.0.7 MarkupSafe==1.0 more-itertools==4.3.0 parse==1.8.4 parse-type==0.4.2 pathspec==0.5.5 pathtools==0.1.2 pgcli==1.10.3 pgspecial==1.11.2 Pillow==5.2.0 pipdeptree==0.13.0 pkg-resources==0.0.0 pluggy==0.7.1 prompt-toolkit==1.0.10 psycopg2-binary==2.7.5 py==1.6.0 pyasn1==0.4.4 pycparser==2.18 Pygments==2.2.0 pyOpenSSL==18.0.0 pytest==3.8.0 pytest-bdd==2.21.0 pytest-cov==2.6.0 pytest-html==1.19.0 pytest-metadata==1.7.0 python-dateutil==2.7.3 python-dotenv==0.9.1 python-slugify==1.2.6 PyYAML==3.13 requests==2.18.4 rsa==3.4.2 s3transfer==0.1.13 semantic-version==2.5.0 setproctitle==1.1.10 six==1.11.0 sqlparse==0.2.4 tabulate==0.8.2 termcolor==1.1.0 terminaltables==3.1.0 texttable==0.9.1 Unidecode==1.0.22 urllib3==1.22 watchdog==0.9.0 wcwidth==0.1.7 webencodings==0.5.1 websocket-client==0.52.0 Werkzeug==0.14.1

j-bennet commented 5 years ago

This works with a file that contains plain sql commands in pgcli==2.1.0:

Server: PostgreSQL 10.6
Version: 2.1.0
Chat: https://gitter.im/dbcli/pgcli
Mail: https://groups.google.com/forum/#!forum/pgcli
Home: http://pgcli.com
postgres@/tmp:postgres> \i 1.sql
+-------+
| boo   |
|-------|
| 1     |
| 2     |
| 3     |
+-------+
SELECT 3
+-------+
| foo   |
|-------|
| 1     |
| 2     |
+-------+
SELECT 2
Time: 0.026s

file:

$ cat 1.sql
select 1 as boo
union select 2
union select 3;

select 1 as foo
union select 2;

What kind of commands does your file contain? Are they meta-commands (backslash-commands)? Are they SET statements (pgcli does not support all of those)? Do you get an "error in your SQL" at some point? Which point is it?

garyhurtz commented 5 years ago

It has been some time, but as described I was loading a file with \i which contained a sequence of lines formatted as \i . This allowed me easily execute sequences of commands using a single \i call. This worked in psql, but I could not get it to work in pgcli. As I recall the issue appeared to be improper handling of backslashes. I just tested again and got the following result from pgcli (which if memory serves is similar to the error last time):

gary> \i sql/test.sql                                                                                                                                                                                                            
[Errno 2] No such file or directory: 'sql/test/schema.sql\n\\i sql/test/main.sql\n\\i sql/test/other.sql'
Time: 0.001s

Note that pgcli consumed the first \i, then treated the rest of the file as a filename / path.

So, pgcli seems to assume that the file contains a single command, rather than (possibly) a sequence of slash commands, whereas psql seemed to handle this correctly.