{"id":15111472,"url":"https://github.com/catterall/dbscripts","last_synced_at":"2025-09-15T03:35:15.898Z","repository":{"id":256281220,"uuid":"854800691","full_name":"Catterall/dbscripts","owner":"Catterall","description":"A small Python package to quickly run database object scripts against a database without dependency issues.","archived":false,"fork":false,"pushed_at":"2024-10-17T10:18:48.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T17:27:38.735Z","etag":null,"topics":["database","database-management","database-migrations","databases","script","scripting","scripts","sql","sql-query","sql-server"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/dbscripts/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Catterall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-09T19:53:37.000Z","updated_at":"2024-10-17T10:18:52.000Z","dependencies_parsed_at":"2024-09-16T19:35:34.766Z","dependency_job_id":null,"html_url":"https://github.com/Catterall/dbscripts","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"0c378954e9b49d4e4c23674050d74ebd3cf9b965"},"previous_names":["catterall/dbscripts"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Catterall%2Fdbscripts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Catterall%2Fdbscripts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Catterall%2Fdbscripts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Catterall%2Fdbscripts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Catterall","download_url":"https://codeload.github.com/Catterall/dbscripts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237780162,"owners_count":19365142,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-management","database-migrations","databases","script","scripting","scripts","sql","sql-query","sql-server"],"created_at":"2024-09-26T00:20:24.048Z","updated_at":"2025-02-08T08:30:51.539Z","avatar_url":"https://github.com/Catterall.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"*Written by Kyle Catterall of Baltic Apprenticeships L4.\n\nThis message will remain here as evidence of identity until appr. end.\n\n---\n\n# dbscripts\n\ndbscripts 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.\n\n### Supported Features\n\n- **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.\n\n- **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.\n\n- **Script Execution** - of course, this package can be used to execute database scripts against a database.\n\n\n### Limitations\n\n- 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.\n\n---\n\n## Basic Usage\n\n### Connecting to a SQL Server database.\nTo connect to a SQL server database, create a `pyodbc.Connection` using your connection string as usual. \n\nFor those who are unfamiliar with SQL Server connection strings, I have created a connection string builder that will hopefully simplify its creation.\n\n```py\nimport pyodbc\n\nfrom dbscripts.dbwriter import ConnectionStringBuilderFactory, DBTypes\n\nbuilder = ConnectionStringBuilderFactory.get_builder(DBTypes.MSSQL)\n\nconnection_string = (\n    builder.set_driver(\"{DRIVER}\")\n            .set_server(\"SERVER\")\n            .set_database(\"DATABASE_NAME\")\n            .set_windows_authentication(True)  # Include this line if using Windows Auth.\n            .set_options({\"Encrypt\": \"yes\", \"TrustServerCertificate\": \"yes\"})  # Adjust as needed.\n            .build()\n)\n\nconn = pyodbc.connect(connection_string)\n```\n\n### Creating `DBScript` instances.\nA `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.\n\n```py\nfrom dbscripts.dbscripts import DBScript, DBFlavor_MSSQL\n\nscript = DBScript('./your_database_script.sql', DBFlavor_MSSQL())\n\n# DBScript attributes. . .\nprint(script.contents)\nprint(script.metadata.obj_name, script.metadata.obj_type, script.metadata.obj_schema, sep=\" - \")\n```\n\n### Using `DBScripts`.\nThe `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.\n\n```py\nfrom dbscripts.dbscripts import DBScripts, DBScript, DBFlavor_MSSQL, DBScriptsAppendRegular\n\nscript_a = DBScript('./dependent_on_b.sql', DBFlavor_MSSQL())\nscript_b = DBScript('./b.sql', DBFlavor_MSSQL())\nscripts = DBScripts(DBFlavor_MSSQL(), DBScriptsAppendRegular())\nscripts.append(script_a)\nscripts.append(script_b)\n\n# Example: handling dependencies. . .\nfor script in scripts.scripts:\n    print(script.metadata.obj_name)\n\nfor script in scripts.safe_execution_order():\n    print(script.metadata.obj_name)\n```\n\nAs 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:\n- `DBScriptsAppendRegular` - does nothing fancy; it just appends the scripts.\n- `DBScriptsAppendIgnoreDuplicates` - if you append a `DBScript` instance already present, it will be ignored.\n- `DBScriptsAppendErrorOnDuplicates` - if you append a `DBScript` instance already present, a `DBScriptAlreadyPresentError` exception is raised.\n\nThese 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.\n\n### Executing scripts with `DBWriter`.\n\nA `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.\n\n##### Running a directory of scripts irregardless of dependencies.\n\n```py\nfrom dbscripts.dbscripts import DBScripts, DBFlavor_MSSQL, DBScriptsAppendRegular\nfrom dbscripts.dbwriter import DBWriter\n\nconn = pyodbc.connect('your_connection_string')\nwriter = DBWriter(conn)\nscripts = DBScripts(DBFlavor_MSSQL(), DBScriptsAppendRegular())\nscripts.populate_from_dir('./your_database_scripts_directory')\nwriter.execute_scripts(scripts.scripts, raise_exceptions=True)\n```\n\n##### Running a directory of scripts with attention to dependencies.\n\n```py\nfrom dbscripts.dbscripts import DBScripts, DBFlavor_MSSQL, DBScriptsAppendRegular\nfrom dbscripts.dbwriter import DBWriter\n\nconn = pyodbc.connect('your_connection_string')\nwriter = DBWriter(conn)\nscripts = DBScripts(DBFlavor_MSSQL(), DBScriptsAppendRegular())\nscripts.populate_from_dir('./your_database_scripts_directory')\nwriter.execute_scripts(scripts.safe_execution_order(), raise_exceptions=True)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcatterall%2Fdbscripts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcatterall%2Fdbscripts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcatterall%2Fdbscripts/lists"}