From 552a4166cbd2169331eba5d6e3c2ab225ab1f196 Mon Sep 17 00:00:00 2001 From: Brian Hines Date: Wed, 6 Jan 2016 10:16:57 -0600 Subject: [PATCH 1/2] Remove JSON functions for older versions of Postgres --- pg_table_markdown/__init__.py | 4 ++-- pg_table_markdown/database.py | 9 ++++++++- pg_table_markdown/queries.py | 24 ++++++++++-------------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pg_table_markdown/__init__.py b/pg_table_markdown/__init__.py index f0baa7a..b886c80 100644 --- a/pg_table_markdown/__init__.py +++ b/pg_table_markdown/__init__.py @@ -27,10 +27,10 @@ def cli(database_url, table_schema, output_file): cursor = db.cursor() cursor.execute(build_schema_query(table_schema=table_schema)) - result = cursor.fetchone() + results = cursor.fetchall() cursor.close() - parsed = parse_schema_data(schema_data=result[0]) + parsed = parse_schema_data(schema_data=results) with open(output_file, 'w') as f: for table_name in sorted(parsed.keys()): f.write(SECTION_HEADING.format(table_name)) diff --git a/pg_table_markdown/database.py b/pg_table_markdown/database.py index 181901b..6406e09 100644 --- a/pg_table_markdown/database.py +++ b/pg_table_markdown/database.py @@ -1,4 +1,5 @@ import psycopg2 +from psycopg2.extras import RealDictCursor try: from urllib.parse import urlparse except ImportError: @@ -19,7 +20,13 @@ def database_connection(database_url): database = parsed.path.strip('/') try: - connection = psycopg2.connect(host=host, port=port, user=user, password=password, database=database) + connection = psycopg2.connect( + host=host, + port=port, + user=user, + password=password, + database=database, + cursor_factory=RealDictCursor) except psycopg2.OperationalError: raise UnableToConnectToDatabase connection.set_session(autocommit=True) diff --git a/pg_table_markdown/queries.py b/pg_table_markdown/queries.py index 6fbcb8c..a358cb8 100644 --- a/pg_table_markdown/queries.py +++ b/pg_table_markdown/queries.py @@ -1,18 +1,14 @@ def build_schema_query(table_schema): schema_query = """ - WITH table_schema_info AS ( - SELECT table_name, - column_name, - column_default, - is_nullable, - data_type, - character_maximum_length - FROM information_schema.columns - WHERE table_schema = '{0}' - ORDER BY table_name, - ordinal_position - ) - SELECT JSON_AGG(table_schema_info.*) - FROM table_schema_info; + SELECT table_name, + column_name, + column_default, + is_nullable, + data_type, + character_maximum_length + FROM information_schema.columns + WHERE table_schema = '{0}' + ORDER BY table_name, + ordinal_position """.format(table_schema) return schema_query From e1dceda63265be1970a9fa2e41aa550edb6089ec Mon Sep 17 00:00:00 2001 From: Brian Hines Date: Wed, 6 Jan 2016 10:21:34 -0600 Subject: [PATCH 2/2] Bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f9f6443..58ce9ff 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pg-table-markdown', - version='1.0.3', + version='1.0.4', author='Vokal', author_email='pypi@vokal.io', description='A command line tool that generates markdown documentation for Postgres tables in a given schema',