Skip to main content

Introduction

sphinx-sql is a Sphinx documentation extension for building documentation from SQL source files.

* Do you live in a bottomless pit of despair with “Living documents” in Sharepoint.
* Have you had to troubleshoot a problem and someone has reorganized the documentation tree in Sharepoint?
* Has your company let PMs loose on projects with no idea how to version documentation, so now you have copies of entire doc trees in Sharepoint?

* Do you work on a database first development project?
* Do you look at auto documentation packages and cry silently because no one cares about DB first development?
* Don’t you wish you could maintain your project documentation with your code base, so you can check out and build the documents anytime you need them?

Having found nothing in the while that could help solve the db first problem, I’ve written sphinx-sql.

The goal of sphinx-sql is to provide an autodoc type module for database first development.
With a bit of standardization of comments in the top of the sql source files, we can maintain documentation that follows the code base.

This implementation is tested against Greenplum / Postgres, those are the databases I work with on a daily basis. If you want to extend functionality, have a quick look at the contrib section of this document.

Tip: If you create your DDL with pgModeler, you may want to look into this simple script to conveniently generate single DDL files, which can be handled by sphinx-sql.

Installation

Install from PyPI:

pip install sphinx-sql

Configuration

Configuring Sphinx

In your conf.py for Sphinx enable the extension:

extensions = [
'sphinx_sql.sphinx_sql',
]

Add the option to toggle including table attributes:

sphinxsql_include_table_attributes = True

By default, Table Columns with their metadata (data type, length, precision, scale) are extracted from the DDL. You can disable this behavior by changing sphinxsql_include_table_attributes = False in your conf.py.

Configure toctree

Create a new rst file (we’ll call it autosql.rst) and include it in your toc-tree.

.. toctree::
:maxdepth: 2
:caption: Navigation:

autosql

Configure rst

Add the directive with a relative path from your document build folder to the root of your SQL source in the autosql.rst file.

SQL Documentation
^^^^^^^^^^^^^^^^^

.. autosql::
    :sqlsource: ../../SQL

Add SQL Comments

sphinx-sql recursively looks for all .sql files under the configured sqlsource path.
It will extract the first block comment out of each file as well as important
object creation lines such as CREATE TABLE / VIEW / FUNCTION / LANGUAGE etc.

Comments should adhere to the following formats, otherwise the regex searches will not find the appropriate blocks
Pipe delimiters are used in Parameters, Dependent Objects and Change Log files to create table rows in the documents, spaces don’t matter; everything else is free form text and should appear as you write it.

Key word groups:

Parameters:
Return:
Purpose:
Dependent Objects:
ChangeLog:

FUNCTIONS:

/*
 Parameters:
 Name | Type | Description

 Return: Void
 Purpose:
 Detailed explanation of the function which includes:
         - Function business logic
         - Transformation rules
         - Here is a bit more text.
 Dependent Objects:
     Type    |Name
     Table   |schema_name.source_table5
     View    |schema_name.target_table6
 ChangeLog:
     Date   |     Author      |    Ticket | Modification
         YYYY-MM-DD |        Developer name |        T-223 | Short Modification details or some really long text that will continue on.
 */

PROCEDURES:

/*
 Parameters:
 Name | Type | Description

 Purpose:
 Detailed explanation of the procedure which includes:
         - Procedure business logic
         - Transformation rules
         - Here is a bit more text.
 Dependent Objects:
     Type    |Name
     Table   |schema_name.source_table5
     View    |schema_name.target_table6
 ChangeLog:
     Date   |     Author      |    Ticket | Modification
         YYYY-MM-DD |        Developer name |        T-223 | Short Modification details or some really long text that will continue on.
 */

TABLES/VIEWS/etc:

You can comment on Table Columns (one-line text, no markups) in your DDL (COMMENT ON COLUMN). These comments will appear in the “Description” column inside the Attributes Block.

/*
Purpose:
This a new view to show how auto documentation can add new objects quickly.
Dependent Objects:
    Type    |Name
    Table   |schema1.ext_table
ChangeLog:
    Date    |    Author    |    Ticket    |    Modification
    2020-10-26    |  Developer_2  |   T-220    |    Initial Definition
*/

DML:

Files that are not a SQL object, but you’d like to include in documentation,
can be included by providing key information in the top-level comment.
Object Name, Object Type are required fields in order to categorize and sort the output.
The remainder of the keywords are valid for use in DML blocks.
/*
Object Name: <schema.name>
Object Type: DML
Purpose:
This a new view to show how auto documentation can add new objects quickly.

ChangeLog:
    Date    |    Author    |    Ticket    |    Modification
    2020-10-26    |  Developer_2  |   T-220    |    Initial Definition
*/

Metadata

Release files for sphinx-sql 1.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sphinx-sql 1.3.5
File Size Uploaded
sphinx-sql-1.3.5.tar.gz 24.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sphinx-sql 1.3.5
File Interpreter ABI Platform
sphinx_sql-1.3.5-py3-none-any.whl Python 3 none any Details

Total release size: 46.0 kB

Release files / sphinx-sql-1.3.5.tar.gz

Download URL sphinx-sql-1.3.5.tar.gz
Size 24.4 kB
Tags Source
SHA-256 checksum
How to use checksums
421136e86609a1fed8a87a14f709461cea6ea30cc542fd33ec7668ac63cf933d
BLAKE2b-256 checksum
How to use checksums
6552eaa1e5f342acd027dd04e89dffbe823427c896a1995ee79d637e3821e611
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / sphinx_sql-1.3.5-py3-none-any.whl

Download URL sphinx_sql-1.3.5-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0e62f52a8d575d951670bdbbc18a0677f107f859b4755af274d36b106a9617c
BLAKE2b-256 checksum
How to use checksums
f253ded4b52e8e6860c58443ab1f57b9602875954a3170f6a32f8020a7e74c17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

This release

1.3.5 This release

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.0

2 release files

1.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page