Giter VIP home page Giter VIP logo

mssql-scripter's Introduction

image

image

image

image

image

mssql-scripter

We’re excited to introduce mssql-scripter, a multi-platform command line experience for scripting SQL Server databases.

mssql-scripter is the multiplatform command line equivalent of the widely used Generate Scripts Wizard experience in SSMS. You can use mssql-scripter on Linux, macOS, and Windows to generate data definition language (DDL) and data manipulation language (DML) T-SQL scripts for database objects in SQL Server running anywhere, Azure SQL Database, and Azure SQL Data Warehouse. You can save the generated T-SQL script to a .sql file or pipe it to standard nix utilities (for example, sed, awk, grep) for further transformations. You can edit the generated script or check it into source control and subsequently execute the script in your existing SQL database deployment processes and DevOps pipelines with standard multiplatform SQL command line tools such as sqlcmd.

mssql-scripter is built using Python and incorporates the usability principles of the new Azure CLI 2.0 tools.

Installation

Please refer to the installation guide for detailed install instructions.

Usage

Please refer to the usage guide for details on options and example usage.

For general help content, pass in the -h parameter:

Contributing

If you would like to contribute to the project, please refer to the development guide.

Reporting issues and feedback

If you encounter any bugs with the tool please file an issue in the Issues section of our GitHub repo.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct.

For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

License

mssql-scripter is licensed under the MIT license.

mssql-scripter's People

Contributors

austineric avatar captainnumerica avatar charles-gagnon avatar dzsquared avatar eshultz avatar jcurbelo avatar judahr avatar microsoft-github-policy-service[bot] avatar microsoftopensource avatar mrmeemus avatar msftgits avatar pensivebrian avatar renenyffenegger avatar sitwalkstand avatar tara-raj avatar udeeshagautam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mssql-scripter's Issues

--script-drop-create switch does not work

I'm running this command line:

mssql-scripter -S localhost -d carga_inicial_9 -U scripter -P scripter -f ./objects --schema-and-data --display-progress --file-per-object --script-drop-create

But upon examination of the generated scripts, I can see no drop has been scripted.

FAQ guide

Answers for common questions:

  • consistency guarantees scripting data
  • identity columns
  • binary/lob data

dump database exclude-type user will lose the full text index

@MrMeemus Hello MrMeenus,I have tried the latest version a1.0.0a14 and I find a bug.
when I dump database not include the create database and create user statement, the table-level full-text index will be removed.If I did't remove the create database and create user statement,full-text index will be successfully dumped out。
My executive commands are

mssql-scripter -S localhost -d test -U user1 > test.sql
mssql-scripter -S localhost -d test -U user1 --exclude-types User > test.sql

The second command is missing as follows

CREATE FULLTEXT INDEX ON [dbo].[customer](
[name] LANGUAGE 'Simplified Chinese')
KEY INDEX [PK_customer]ON ([CustomerSearch], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)

Thank you for your answer.

error: Invalid directory specified by the ScriptingParams.FilePath property.

$ pip install --user -U mssql-scripter
Requirement already up-to-date: mssql-scripter in /home/chaz/.local/lib64/python3.4/site-packages
Requirement already up-to-date: mssqltoolsservice-CentOS-7==1.0.0a12 in /home/chaz/.local/lib64/python3.4/site-packages (from mssql-scripter)
Requirement already up-to-date: wheel>=0.29.0 in /home/chaz/.local/lib64/python3.4/site-packages (from mssql-scripter)
Requirement already up-to-date: future>=0.16.0 in /home/chaz/.local/lib64/python3.4/site-packages (from mssql-scripter)

$ mssql-scripter -S localhost -d AdventureWorks2014
Scripting request: c2bec063-6034-4e06-989f-36dd3628219c encountered error: Invalid directory specified by the ScriptingParams.FilePath property.
Error details: System.ArgumentException: Invalid directory specified by the ScriptingParams.FilePath property.
   at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.ValidateScriptDatabaseParams()
   at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.Execute()

Refactor pip install for the SqlToolsService dependency

Our current approach to manage the SqlToolsService is getting complicated due to all of the ways pip can install: user install, machine install, virtual environments, etc.

To simplify things, we're going to try a different approach to managing the SqlToolsService dependency:

  • Package up each SqlToolsService distro archive into it's own pip wheel
  • Using a pip sdist install and a custom install script, detect the distro at pip install time, and include the appropriate SqlToolsService wheel as a dependency

This way, we let pip manage everything.

Scripting fails due to SMO post processing feature not existing on .NET Core

Scripting a database can result in the following exception:

Error details: Microsoft.SqlServer.Management.SqlScriptPublish.SqlScriptPublishException: An error occurred while scripting the objects. ---> System.MissingMethodException: Constructor on type 'Microsoft.SqlServer.Management.Smo.PostProcessUser' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at Microsoft.SqlServer.Management.Smo.Util.CreateObjectInstance(Assembly assembly, String objectType)
   at Microsoft.SqlServer.Management.Smo.SqlPostProcess.GetPostProcessInstance()
   at Microsoft.SqlServer.Management.Smo.SqlPostProcess.Register(SqlObjectBase obj)
   at Microsoft.SqlServer.Management.Smo.SqlObjectBase.RegisterPostProcessHits()
   at Microsoft.SqlServer.Management.Smo.SqlObjectBase.PrepareGetData(EnumResult erParent)
   at Microsoft.SqlServer.Management.Smo.SqlObjectBase.GetData(EnumResult erParent)
   at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData()
   at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request req, Object ci)
   at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo, Request request)
   at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req)
   at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitChildLevel(Urn levelFilter, ScriptingPreferences sp, Boolean forScripting)
   at Microsoft.SqlServer.Management.Smo.Database.PrefetchUsers(ScriptingPreferences options)
   at Microsoft.SqlServer.Management.Smo.DatabasePrefetchBase.PrefetchAllObjects(String urnType)
   at Microsoft.SqlServer.Management.Smo.DatabasePrefetchBase.<PrefetchObjects>d__14.MoveNext()
   at Microsoft.SqlServer.Management.Smo.SmoDependencyDiscoverer.SfcChildrenDiscovery(HashSet`1 discoveredUrns)
   at Microsoft.SqlServer.Management.Smo.SmoDependencyDiscoverer.Discover(IEnumerable`1 urns)
   at Microsoft.SqlServer.Management.Smo.ScriptMaker.Discover(IEnumerable`1 urns)
   at Microsoft.SqlServer.Management.Smo.ScriptMaker.DiscoverOrderScript(IEnumerable`1 urns)
   at Microsoft.SqlServer.Management.Smo.ScriptMaker.ScriptWorker(List`1 urns, ISmoScriptWriter writer)
   at Microsoft.SqlServer.Management.Smo.ScriptMaker.Script(Urn[] urns, ISmoScriptWriter writer)
   at Microsoft.SqlServer.Management.SqlScriptPublish.SqlScriptGenerator.DoScript(ScriptOutputOptions outputOptions)

Encountering an encoding error scripting AdventureWorks data

On Windows running Python 3.5, I'm seeing the below behavior:

Run the following command:
$ python main.py -S localhost -d AdventureWorks2014 --schema-and-data > AdventureWorks.sql

Result:
Traceback (most recent call last):
File "main.py", line 123, in
main(sys.argv[1:])
File "main.py", line 102, in main
sys.stdout.write(line)
File "C:\Python35\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 364-379: character maps to

Please Provide Node.Js methods

I would like to be able to perform an npm install, pull down a package, and be able to query and script out objects. With this request, I would the ability to log into the SQL instance using Windows Auth. Having the ability to use Node.Js would be amazing. I am currently using Edge.js to perform MSSQL calls that marshal out to .Net and then use ADO to query SQL, I would prefer something native and support by Microsoft.

Support for virtual environment

When in production we use the site.getsitepackages() method to find the location of the site-packages of the current python interpreter. This will fail in virutal environments. We need to manually search for the sqltoolsservice folder.

Using the --file option still writes to stdout

Repro
Run with the --file option:

mssql-scripter -S localhost -d AdventureWorks2014 --file AdventureWorks.sql

Actual
The output is going to stdout and the output file.

Expected
The output to go to the output file only.

Scripting data is broken

We're encounter a strange issue in SMO which is causing the SqlScriptPublishModel.AdvancedOptions to get lost, so default values are used. Since default values are used, data is never scripted.

I have a fix, just adding more tests and I'll have a PR out soon.

error: System.ArgumentException,Please check out

Hello,
When I use the software in windows7,there was a bug. The error message is as follows.

$ mssql-scripter -S localhost -d db_01 -U user1 --enable-toolsservice-logging
Password:
Scripting request: 1da2a284-987d-4669-8bf2-63be6bebabba encountered error: Inval
id directory specified by the ScriptingParams.FilePath property.
Error details: System.ArgumentException: Invalid directory specified by the Scri
ptingParams.FilePath property.
at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.Validat
eScriptDatabaseParams() in D:\repos\sqltoolsservice\src\Microsoft.SqlTools.Servi
ceLayer\Scripting\ScriptingScriptOperation.cs:line 298
at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.Execute
() in D:\repos\sqltoolsservice\src\Microsoft.SqlTools.ServiceLayer\Scripting\Scr
iptingScriptOperation.cs:line 70

And the log files found in my user directory under “.mssqlscipyer” is as follows.

2017/6/26 19:10:17 [ERROR] - Method "Execute" at line 115 of D:\repos\sqltoolsservice\src\Microsoft.SqlTools.ServiceLayer\Scripting\ScriptingScriptOperation.cs

Scripting operation 1da2a284-987d-4669-8bf2-63be6bebabba failed with exception System.ArgumentException: Invalid directory specified by the ScriptingParams.FilePath property.
   at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.ValidateScriptDatabaseParams() in D:\repos\sqltoolsservice\src\Microsoft.SqlTools.ServiceLayer\Scripting\ScriptingScriptOperation.cs:line 298
   at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.Execute() in D:\repos\sqltoolsservice\src\Microsoft.SqlTools.ServiceLayer\Scripting\ScriptingScriptOperation.cs:line 70

Please check out, thank you!

exception when connecting to Azure DW

When connecting to azure sql data warehouse, the below exception occurs:

Scripting request: 1c89a309-2656-453d-92e5-9ed451d94f60 encountered error: An error occurred while scripting the objects.
Error details: Microsoft.SqlServer.Management.SqlScriptPublish.SqlScriptPublishException: An error occurred while scripting the objects. ---> System.MissingMethodException: Constructor on type 'Microsoft.SqlServer.Management.Smo.PostProcessUser' not found.

command used:

mssql-scripter -S a-sql-server.database.windows.net -d dwh-name -U username

Improve usage guide

Add recipes for common usage scenarios:

  • change database name
  • windows->linux paths
  • change schema name
  • etc

Add command line options to include/exclude based on type and schema

Currently we have no way to filter object by type, and filtering by schema is not intuitive with the --include-object schema.* argument.

I'm proposing we add the following command line options:

--include-schema [name]
--exclude-schema [name]
--include-type [name]
--exclude-type [name]

I think this is little more clear and usable. Thoughts?

Add integration tests

Currently there are only unit tests. We will need to add support to simulate end to end scenarios via session play backs. i.e simulate the input and output stream with baseline files to verify expected responses match with requests.

EOF in JSON RPC reader can cause unhandled TypeError

https://github.com/Microsoft/sql-xplat-cli/blob/dev/mssqlscripter/jsonrpc/jsonrpcclient.py#L318 adds the number of bytes read to the buffer end offset regardless of the outcome of the call to read from the stream. This could cause issues if EOF is reached when reading b/c adding "" (returned when EOF is reached) to a number will raise a TypeError.

Moving the referenced line after the if not length_read: block would solve the issue easily.

In my server implementation, I've also added a unit test for checking that this condition doesn't occur:

    def test_read_next_chunk_eof(self):
        with io.BytesIO() as stream:
            # If:
            # ... I create a reader with a stream that has no bytes
            reader = JsonRpcReader(stream)

            # ... and I read a chunk from the stream
            # Then: I should get an exception
            with self.assertRaises(EOFError):
                reader._read_next_chunk()

Gracefully handle scripting errors

When we encounter a scripting error, we need to define:

  • How the scripting api indicates and error
  • How to surface errors to the end user

Threat model data flow diagrams

Key things to track:

  • Connection strings which may hold credentials - these cannot be logged
  • Temp .sql files which can contain data from the database being scripting

Error scripting out a SQL DW database

I am using Python 3.6.1 on Windows, running the command below. I receive the error message (below) when trying to script out my database.

mssql-scripter -S -d airlift -U -P

mssql_error

Thanks!
Kevin

Credential handling

Update command line parameter to handle:

  • Prompt for password
  • Password from environment variable
  • Connection string from environment variable

Remove OS specific file paths from generated scripts

Scripts can have reference to OS specific file paths. For example, file groups on Windows will have path "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA". We need to figure out how to make the script use OS independent paths.

Script to multiple sql files

This request contains following function requirements.

  1. Produce one sql file per t-sql object. e.g. table, view, sp, function etc.
  2. Option to specify the output directory.
  3. Option to specify type of t-sql object in addition to the multiple sql file capability. e.g. script all tables + create a sql file for each table.
  4. Option not to include DROP or ALTER statement in the scripted file. e.g. CREATE statement only.

Failed to login to AzureDB

Using correct credential but cannot connect to AzureDB, with error Login failed.

C:\Users\UserName>mssql-scripter --version
1.0.0a12

C:\Users\UserName>mssql-scripter -S ae-az-dw-prod.database.windows.net -d bidw_prod -U etluser
Password:
Scripting request: 484b9114-e793-4760-8743-2a9b8509e96a encountered error: Failed to connect to server ae-az-dw-prod.database.windows.net.
Error details: Microsoft.SqlServer.Management.Common.ConnectionFailureException: Failed to connect to server ae-az-dw-prod.database.windows.net. --->
System.Data.SqlClient.SqlException: Login failed for user 'etluser'.
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerIn
fo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderIn
fo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions option
s, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConne
ction)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal old
Connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate,
Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOpti ons, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions u
serOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, Tas
kCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletio nSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.SqlServer.Management.Common.ConnectionManager.InternalConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.GetServerInformation()
at Microsoft.SqlServer.Management.Smo.Server.IsAzureDbScopedConnection(ServerConnection sc)
at Microsoft.SqlServer.Management.Smo.Server.GetExecutionManager()
at Microsoft.SqlServer.Management.Smo.Server.get_ExecutionManager()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.Server.get_Version()
at Microsoft.SqlServer.Management.SqlScriptPublish.SqlScriptPublishModel.GetAdvancedScriptingOptions()
at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.BuildPublishModel() in D:\repos\sqltoolsservice\src\Microsoft.SqlTools.Servic
eLayer\Scripting\ScriptingScriptOperation.cs:line 162
at Microsoft.SqlTools.ServiceLayer.Scripting.ScriptingScriptOperation.Execute() in D:\repos\sqltoolsservice\src\Microsoft.SqlTools.ServiceLayer\Scr
ipting\ScriptingScriptOperation.cs:line 70

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.