{"id":20186935,"url":"https://github.com/finndersen/sqlserver-schema-manager","last_synced_at":"2026-06-08T19:31:08.080Z","repository":{"id":260275833,"uuid":"875010192","full_name":"Finndersen/sqlserver-schema-manager","owner":"Finndersen","description":"A library for programmatically managing the schema of a SQLServer Database","archived":false,"fork":false,"pushed_at":"2024-10-30T13:13:01.000Z","size":45,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-30T22:12:31.202Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Finndersen.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-10-18T22:25:01.000Z","updated_at":"2024-10-30T13:13:14.000Z","dependencies_parsed_at":"2024-10-30T14:37:44.133Z","dependency_job_id":null,"html_url":"https://github.com/Finndersen/sqlserver-schema-manager","commit_stats":null,"previous_names":["finndersen/sqlserver-schema-manager"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Finndersen/sqlserver-schema-manager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finndersen%2Fsqlserver-schema-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finndersen%2Fsqlserver-schema-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finndersen%2Fsqlserver-schema-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finndersen%2Fsqlserver-schema-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Finndersen","download_url":"https://codeload.github.com/Finndersen/sqlserver-schema-manager/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finndersen%2Fsqlserver-schema-manager/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34078019,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-14T03:19:14.260Z","updated_at":"2026-06-08T19:31:08.062Z","avatar_url":"https://github.com/Finndersen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQLServer Schema Manager\n\n\n## Introduction\n\nA library for programmatically managing the schema of a SQLServer Database, with two modes of operation:\n* Declare the entire structure of a SQLServer instance in code, and apply it to a target database (automatically determines and executes all diff changes required).\n* Programmatically/interactively inspect the structure of an existing SQLServer instance, and make changes using code instead of SQL.\n\nIt can be used in tandem with other tools that manage a DB schema (e.g. an ORM), to manage the features that they do not support (e.g. the database itself, users, partitioning and compression).\n\nImportant note: This is a tool I developed for my own purposes many years ago, it has a lot of functionality and works quite well but is not recommended to be used in a production context without some further work. \n\n## Installation\n\nThis library is not packed or included in any package manager, so you will need to clone the repository and manually include it in your project. \n\n## Usage\n\n### Declarative Mode\n\n1. Define your desired server schema:\n\n```python\nfrom sssm.db_entities.declared import Server, Database, Table, Index, ForeignKey, Login, IntegerColumn, VarcharColumn, DateColumn, IdentityColumn, FloatColumn, User\n\n# Currently does not support creating Logins, so these will need to correspond to ones already existing on the server\nadmin_login = Login('admin')\nwriter_login = Login('writer', server_roles=['bulkadmin'])\nreader_login = Login('reader')\n\nserver = Server(\n    databases=[\n        Database(\n            name='MyDatabase',\n            owner=admin_login.name,\n            data_file_dir=r'D:\\MSSQL\\DATA',\n            log_file_dir=r'D:\\MSSQL\\LOG',\n            data_size=100_000,\n            # Can also specify schema name if desired, otherwise default \"db\" is used\n            tables=[\n                Table(\n                    name='Person',\n                    columns=[\n                        IdentityColumn(name='ID', primary_key=True),\n                        VarcharColumn(name='Name', char_max_len=255),\n                        DateColumn(name='DateOfBirth'),\n                        IntegerColumn(name='HeightCM'),\n                        FloatColumn(name='WeightKG'),\n                        IntegerColumn(name='AddressID'),\n                    ],\n                    indexes=[\n                        Index(name='IX_MyTable_Name', columns=['Name'], compression='PAGE'),\n                    ],\n                    foreign_keys=[\n                        ForeignKey(column='AddressID', foreign_table='Address', foreign_column='ID'),\n                    ],\n                ),\n                Table(\n                    name='Address',\n                    columns=[\n                        IdentityColumn(name='ID', primary_key=True),\n                        IntegerColumn(name='StreetNumber'),\n                        VarcharColumn(name='StreetName', char_max_len=255),\n                        VarcharColumn(name='PostCode', char_max_len=6),\n                        VarcharColumn(name='State', char_max_len=100),\n                        VarcharColumn(name='Country', char_max_len=255),\n                    ],\n                ),\n            ],\n            users=[\n                User.for_login(admin_login),                    \n                User.for_login(writer_login, db_roles=['db_datawriter']),   \n                User.for_login(reader_login, db_roles=['db_datareader']),\n            ]\n        )\n    ],\n    logins=[\n        admin_login,\n        writer_login,\n        reader_login,\n    ],\n)\n```\n\nApply the schema to a target database:\n\n```python\nimport pyodbc\n\nfrom sssm.align import align_server\nfrom .schema import server\n\n# Create connection using an admin login\nconnection = pyodbc.connect(...)\n\nalign_server(connection.cursor(), server)\n```\n\nThis should be run from an interactive Python session or script, as it will prompt for confirmation before applying destructive changes.\n\n### Inspection Mode\n\nInspecting an existing server schema can be done either programmatically or in an interactive shell:\n\n```python\n\u003e\u003e\u003e import pyodbc\n\u003e\u003e\u003e from sssm.db_entities.reflected import ReflectedServer\n# Create connection using an admin login\n\u003e\u003e\u003e connection = pyodbc.connect(...)\n\u003e\u003e\u003e server = ReflectedServer.from_cursor(connection.cursor())\n\u003e\u003e\u003e database = server.get_current_database()    # Get database specified in connection\n\u003e\u003e\u003e person_table = database.get_table('Person') # Get table by name\n\u003e\u003e\u003e person_table.get_compression()\n'NONE'\n\u003e\u003e\u003e person_table.set_compression('PAGE')        # Rebuild table with PAGE compression\n\u003e\u003e\u003e person_table.get_compression()\n'PAGE'\n\u003e\u003e\u003e person_table.clear_data()\n\n```\n\n## Features\n\n### Supported Database Entities\n\nSupports managing the following database entities and attributes:\n* Server Logins (not creating)\n  * Server roles \n* Databases\n  * Owner \n  * Data and log file directories\n  * Data size\n  * Recovery model\n* Schemas\n* Tables\n* Users\n  * Login name\n  * DB roles\n* Columns\n  * Data types\n  * Max length\n  * Nullable\n  * Datetime/Numeric precision\n* Indexes / Primary keys\n  * Columns\n  * Clustered\n  * Unique\n  * Included columns\n  * Compression\n* Foreign keys\n  * Source and target columns \n* Partitions\n  * Partition column \n\n### Other Features\n\n* Use `ignore_extra_children` on a declared object if you want to ignore (not delete) any extra existing children when aligning it to a reflected object.\n* Set `old_name` on a declared object to identify and rename the existing object when aligning it to a database.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffinndersen%2Fsqlserver-schema-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffinndersen%2Fsqlserver-schema-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffinndersen%2Fsqlserver-schema-manager/lists"}