How to generate Microsoft SQL Server DDL in a cross-platform way (preferably in Python)?
I'd like to have a cross-platform way to dump an SQL Server database's DDL to files, so I can keep them in version control. I currently use SQLDMO in VBScript on Windows to do this, but I am looking to be able to do this from a Linux system too, preferably in Python.
I currently have somewhat of a solution using SQLAlchemy table reflection, but it's a little slow and doesn't currently give me all the detail I want. For example, a foreign key is missing "ON DELETE CASCADE.开发者_高级运维"
Are there any other projects out there reconstructing DDL from just what you can get through T-SQL? If so, I can probably port that to Python without too much trouble. Other suggestions also welcome.
Here is my stab at some code that does this:
https://bitbucket.org/rsyring/mssqlddlwriter/
It writes basic details for tables, constraints, indexes, and triggers out to files.
This is not a complete solution (it's a script, originally by Tim Chapman, that I grabbed off the internet at some point, and has been patched up a few times), but I use a T-SQL stored proc that can script tables:
https://github.com/TaoK/PoorMansTSqlFormatter/blob/master/PoorMansTSqlFormatterTest/Data/InputSql/5_ComplexDDL.txt
It's not much use if you're looking to script other objects as well (procs, views, etc), but if you're looking just for table structure, it's worked pretty well for me! Like @Randy's suggestion above it handles tables, indexes, and constraints, although this one does not handle Triggers.
精彩评论