Skip to main content

dbscripts

dbscripts is a small Python package for handling what my collegues and I like to refer to as "database scripts" - SQL scripts, often generated by a DBMS, that allow for the quick creation and altering of database objects.

Supported Features

  • Minor Analysis - this package can be used to analyse database scripts produced by MS-SSMS. For any given database script, you can get the name of the database object, the type of the database object, and the schema of the database object.

  • Dependency Management - this package can be used to reorder a collection of database scripts into an order safe to execute without dependency issues. It does this through a combination of regular-expressions and Khan's topological sorting algorithm.

  • Script Execution - of course, this package can be used to execute database scripts against a database.

Limitations

  • As implied, I have created this package as an aid to my job. As such, only Microsoft SQL Server is currently supported, since that's what I use. However, I have attempted to make the package as easily extendable as possible, so feel free to fork, alter, and make pull requests if you would like another flavor to be supported.

Basic Usage

Connecting to a SQL Server database.

To connect to a SQL server database, create a pyodbc.Connection using your connection string as usual.

For those who are unfamiliar with SQL Server connection strings, I have created a connection string builder that will hopefully simplify its creation.

import pyodbc

from dbscripts.dbwriter import ConnectionStringBuilderFactory, DBTypes

builder = ConnectionStringBuilderFactory.get_builder(DBTypes.MSSQL)

connection_string = (
    builder.set_driver("{DRIVER}")
            .set_server("SERVER")
            .set_database("DATABASE_NAME")
            .set_windows_authentication(True)  # Include this line if using Windows Auth.
            .set_options({"Encrypt": "yes", "TrustServerCertificate": "yes"})  # Adjust as needed.
            .build()
)

conn = pyodbc.connect(connection_string)

Creating DBScript instances.

A DBScript instance can be created with a path to a database script and the flavor of your database. Given that, as mentioned, only MSSQL is currently supported, this process is very simple.

from dbscripts.dbscripts import DBScript, DBFlavor_MSSQL

script = DBScript('./your_database_script.sql', DBFlavor_MSSQL())

# DBScript attributes. . .
print(script.contents)
print(script.metadata.obj_name, script.metadata.obj_type, script.metadata.obj_schema, sep=" - ")

Using DBScripts.

The DBScripts class allows you to have a collection of DBScript instances, and perform operations that require or affect multiple scripts at once. The most noteable use-case is handling dependencies.

from dbscripts.dbscripts import DBScripts, DBScript, DBFlavor_MSSQL, DBScriptsAppendRegular

script_a = DBScript('./dependent_on_b.sql', DBFlavor_MSSQL())
script_b = DBScript('./b.sql', DBFlavor_MSSQL())
scripts = DBScripts(DBFlavor_MSSQL(), DBScriptsAppendRegular())
scripts.append(script_a)
scripts.append(script_b)

# Example: handling dependencies. . .
for script in scripts.scripts:
    print(script.metadata.obj_name)

for script in scripts.safe_execution_order():
    print(script.metadata.obj_name)

As you can see, as well as taking a flavor, the DBScripts class also takes one of several IDBScriptsAppender implementations, which determine how the append method will behave. The following implementations exist:

  • DBScriptsAppendRegular - does nothing fancy; it just appends the scripts.
  • DBScriptsAppendIgnoreDuplicates - if you append a DBScript instance already present, it will be ignored.
  • DBScriptsAppendErrorOnDuplicates - if you append a DBScript instance already present, a DBScriptAlreadyPresentError exception is raised.

These implementations are a lot more handy when using populating methods, such as populate_from_dir, which just attempts to append instances of DBScript for every SQL file in a given directory.

Executing scripts with DBWriter.

A DBWriter class can be given a pyodbc.Connection instance and used to execute either a DBScript instance or a list of DBScript instances. Two common use cases are shown below as examples.

Running a directory of scripts irregardless of dependencies.
from dbscripts.dbscripts import DBScripts, DBFlavor_MSSQL, DBScriptsAppendRegular
from dbscripts.dbwriter import DBWriter

conn = pyodbc.connect('your_connection_string')
writer = DBWriter(conn)
scripts = DBScripts(DBFlavor_MSSQL(), DBScriptsAppendRegular())
scripts.populate_from_dir('./your_database_scripts_directory')
writer.execute_scripts(scripts.scripts, raise_exceptions=True)
Running a directory of scripts with attention to dependencies.
from dbscripts.dbscripts import DBScripts, DBFlavor_MSSQL, DBScriptsAppendRegular
from dbscripts.dbwriter import DBWriter

conn = pyodbc.connect('your_connection_string')
writer = DBWriter(conn)
scripts = DBScripts(DBFlavor_MSSQL(), DBScriptsAppendRegular())
scripts.populate_from_dir('./your_database_scripts_directory')
writer.execute_scripts(scripts.safe_execution_order(), raise_exceptions=True)

Metadata

Release files for dbscripts 0.1.42

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

Source distribution (sdist)

Source distribution for dbscripts 0.1.42
File Size Uploaded
dbscripts-0.1.42.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dbscripts 0.1.42
File Interpreter ABI Platform
dbscripts-0.1.42-py3-none-any.whl Python 3 none any Details

Total release size: 21.1 kB

Release files / dbscripts-0.1.42.tar.gz

Download URL dbscripts-0.1.42.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3e045c3c001d2c78ea25a90179538eb60c95efb7b327095f1b485cb708ae6588
BLAKE2b-256 checksum
How to use checksums
ffa196d28b17dec3cc259dc16049802062288a5083f4d8a9573f6c418a3d649e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / dbscripts-0.1.42-py3-none-any.whl

Download URL dbscripts-0.1.42-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38f4f8e4c88f0a1411289697c53b0a4053036740731480a1326902bd519db563
BLAKE2b-256 checksum
How to use checksums
5aca767c5617b719e49445bb4c9a10a6b1863004c7d30ed2d379e3c6f7859c5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.42 This release

2 release files

0.1.41

2 release files

0.1.38

2 release files

0.1.37

2 release files

0.1.36

2 release files

0.1.35

2 release files

0.1.34

2 release files

0.1.33

2 release files

0.1.32

2 release files

0.1.31

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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