From f7ec170e52ff40805a452c5557157280c919a98e Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 22 Jan 2014 14:56:29 -0700 Subject: [PATCH 1/5] added trusted connection option --- Connection.cs | 23 +++++++++++++++++++---- Program.cs | 21 ++++++++++++++------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Connection.cs b/Connection.cs index 521009d..db83c34 100644 --- a/Connection.cs +++ b/Connection.cs @@ -13,13 +13,15 @@ public class Connection public string username; public string password; public bool connectionOK; + public bool trustedConnection; public string connectionError = ""; - public Connection(string svrName, string usrName, string pssWord) + public Connection(string svrName, string usrName, string pssWord, bool trstConnection) { serverName = svrName; username = usrName; password = pssWord; + trustedConnection = trstConnection; } public string connectionString() @@ -27,6 +29,10 @@ public string connectionString() string connstr = "Data Source=" + serverName + "; User Id=" + username + "; Password=" + password; + + if (trustedConnection) + connstr = String.Format("Server={0};Database=financials;Trusted_Connection=True;", serverName); + return connstr; } @@ -74,9 +80,18 @@ public Server server(string database) public ServerConnection serverConnection() { ServerConnection conn = new ServerConnection(); - conn.LoginSecure = false; - conn.Login = username; - conn.Password = password; + + if (trustedConnection) + { + conn.LoginSecure = true; + } + else + { + conn.LoginSecure = false; + conn.Login = username; + conn.Password = password; + } + conn.ServerInstance = serverName; return conn; } diff --git a/Program.cs b/Program.cs index f617b36..0460ef0 100644 --- a/Program.cs +++ b/Program.cs @@ -35,7 +35,8 @@ static void Main(string[] args) * -s * -u * -p - * -d + * -d + * -t trusted connection * * [for scripting data] * --table @@ -51,6 +52,7 @@ static void Main(string[] args) string pwd = ""; string dbs = ""; string pth = ""; + bool trustedConnection = false; // other options string tbl = ""; @@ -59,8 +61,8 @@ static void Main(string[] args) bool with_fixtures = false; string where_clause = ""; - // 10+ parameters are required - if (args.Length < 9) + // 7+ parameters are required + if (args.Length < 8) { PrintInstructions(); return; @@ -97,7 +99,10 @@ static void Main(string[] args) case "-p": pwd = args[i + 1]; break; - + case "-t": + if (pwd != "" || usr != "") Console.WriteLine(args[i] + "argument only applies if no usrname or password is set"); + trustedConnection = true; + break; case "--fixtures": if (util.ToLower() == CMD_SCRIPT_OBJECTS) Console.WriteLine(args[i] + " argument does not apply to the ScriptDbObjects utility"); with_fixtures = true; @@ -131,7 +136,7 @@ static void Main(string[] args) // // all of these are required all the time - if (svr == "" || usr == "" || pwd == "" || pth == "") + if (pwd != "" && trustedConnection || usr != "" && trustedConnection || svr == "" || pth == "") { PrintInstructions(); return; @@ -161,7 +166,7 @@ static void Main(string[] args) } // establish and test db connection - Connection connection = new Connection(svr, usr, pwd); + Connection connection = new Connection(svr, usr, pwd, trustedConnection); if (connection.testConnection() != true) { Console.WriteLine("Database connection could not be established."); @@ -213,7 +218,7 @@ static void PrintInstructions() Usage: --------------------------------- -> dbscript -f """" -s -u -p -d [--table
--alltables --limit --fixtures] +> dbscript -f """" -s -u -p -d -t [--table
--alltables --limit --fixtures] Available utilities are ScriptDbObjects, ScriptData & CreateDatabase @@ -226,6 +231,8 @@ static void PrintInstructions() When creating or scripting databases the user credentials supplied should have sufficient permissions (preferably 'sa' account). +-t option is trusted connection, can be used instead of username and password. + Options: --------------------------------- From c48301b18a8c2b46297c86188a94caaff7f80c9f Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 24 Jan 2014 11:52:11 -0700 Subject: [PATCH 2/5] modified generated paths to comform to roundhouse paths --- ScriptDB.cs | 10 +++++----- app.config | 4 ++-- dbscript.csproj | 37 ++++++++++++++++++++++++++++++++++--- dbscript.sln | 6 ++++-- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/ScriptDB.cs b/ScriptDB.cs index bddb17f..c96ddd0 100644 --- a/ScriptDB.cs +++ b/ScriptDB.cs @@ -77,7 +77,7 @@ public void scriptDB(string directory) // scripting the objects - scriptDatabase(db, scrp, urn); + //scriptDatabase(db, scrp, urn); scriptTables(db, scrp, urn); scriptViews(db, scrp, urn); scriptStoredProcedures(db, scrp, urn); @@ -139,7 +139,7 @@ void scriptDatabase(Database db, Scripter scrp, Urn[] urn) void scriptTables(Database db, Scripter scrp, Urn[] urn) { string filename; - string tblPath = m_dbPath + @"\Tables"; + string tblPath = m_dbPath + @"\Up"; Directory.CreateDirectory(tblPath); foreach (Table tbl in db.Tables) @@ -177,7 +177,7 @@ void scriptTables(Database db, Scripter scrp, Urn[] urn) string keyPath = tblPath + @"\Keys"; Directory.CreateDirectory(keyPath); - string ndxPath = tblPath + @"\Indexes"; + string ndxPath = tblPath + @"..\..\Indexes"; Directory.CreateDirectory(ndxPath); foreach (Index ndx in tbl.Indexes) @@ -315,7 +315,7 @@ void scriptViews(Database db, Scripter scrp, Urn[] urn) // Script View Indexes - string ndxPath = vwPath + @"\Indexes"; + string ndxPath = vwPath + @"..\..\Indexes"; Directory.CreateDirectory(ndxPath); foreach (Index ndx in vw.Indexes) @@ -354,7 +354,7 @@ void scriptViews(Database db, Scripter scrp, Urn[] urn) void scriptStoredProcedures(Database db, Scripter scrp, Urn[] urn) { string filename; - string procPath = m_dbPath + @"\Stored Procedures"; + string procPath = m_dbPath + @"\Sprocs"; Directory.CreateDirectory(procPath); scrp.Options.Permissions = true; diff --git a/app.config b/app.config index b7db281..73859b0 100644 --- a/app.config +++ b/app.config @@ -1,3 +1,3 @@ - + - + diff --git a/dbscript.csproj b/dbscript.csproj index 25de85b..12129e2 100644 --- a/dbscript.csproj +++ b/dbscript.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -12,7 +12,26 @@ dbscript v3.5 512 - Full + + + + + 3.5 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true true @@ -65,6 +84,18 @@ + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + - + \ No newline at end of file diff --git a/dbscript.sln b/dbscript.sln index 41d0af1..2560c40 100644 --- a/dbscript.sln +++ b/dbscript.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dbscript", "dbscript.csproj", "{AF78A639-C937-4D98-A6CA-01F860C79F97}" EndProject Global From ec3474b345e694354484252a7a54cd0ada73ae83 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 29 Jan 2014 10:52:51 -0700 Subject: [PATCH 3/5] minor formatting changes, Removed Create Database option, Modified scripts to create if not exists --- Program.cs | 20 ++++++++++---------- ScriptData.cs | 50 ++++++++++++++++++++++++------------------------- app.config | 2 +- dbscript.csproj | 7 +++++-- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Program.cs b/Program.cs index 0460ef0..217591c 100644 --- a/Program.cs +++ b/Program.cs @@ -47,7 +47,7 @@ static void Main(string[] args) */ // basic options - string svr = ""; + string svr = ""; string usr = ""; string pwd = ""; string dbs = ""; @@ -143,19 +143,23 @@ static void Main(string[] args) } // if creating a database - it should only be a local instance?? + /* if (util.ToLower() == CMD_CREATE_DATABASE && !svr.Contains(@"\SQLEXPRESS")) { Console.WriteLine("The " + util + " function should only be used on workstation installations of SQLEXPRESS."); return; } + */ // if creating a database or insert script, need the db name + /* if ((util.ToLower() == CMD_CREATE_DATABASE || util.ToLower() == CMD_SCRIPT_DATA) && dbs == "") { Console.WriteLine("The " + util + " function requires the database name to be specified."); PrintInstructions(); return; } + */ // if scripting table data, tablename must be provided if (util.ToLower() == CMD_SCRIPT_DATA && tbl == "" && all_tables == false) @@ -181,11 +185,12 @@ static void Main(string[] args) } // Create Database + /* if (util.ToLower() == CMD_CREATE_DATABASE) { CreateDatabase db = new CreateDatabase(connection, pth, dbs, with_fixtures); } - + */ // Script Objects if (util.ToLower() == CMD_SCRIPT_OBJECTS) { @@ -220,18 +225,15 @@ static void PrintInstructions() > dbscript -f """" -s -u -p -d -t [--table
--alltables --limit --fixtures] -Available utilities are ScriptDbObjects, ScriptData & CreateDatabase +Available utilities are ScriptDbObjects & ScriptData The is the starting point for reading/writing all your database creation scripts (probably within a subversion branch). The is the server instance you are connectiong to. For a local installation of SQLEXPRESS it will be PCNAME\SQLEXPRESS is the database you want to script or build. This parameter is optional when using ScriptDbObjects. If it is not supplied the utility will script all databases. -When creating a database, the Root Path directory must contain a ""databasename.database.sql"" file as created by the ScriptDbObjects utility. -If the database already exists, it will be dropped and then recreated. - -When creating or scripting databases the user credentials supplied should have sufficient permissions (preferably 'sa' account). +When scripting databases the user credentials supplied should have sufficient permissions (preferably 'sa' account). --t option is trusted connection, can be used instead of username and password. +-t option is trusted connection, which can be used instead of username and password. Options: @@ -252,8 +254,6 @@ When creating or scripting databases the user credentials supplied should have s > dbscript ScriptDbObjects -f ""\dev\files\branches\myFeatureBranch\Databases"" -s MYPC\SQLEXPRESS -u sa -p adminpass -d textme -> dbscript CreateDatabase -d textme -f ""\dev\files\branches\myFeatureBranch\Databases"" -s MYPC\SQLEXPRESS -u sa -p adminpass --fixtures - > dbscript ScriptData -d textme --table GlobalOptions -f ""\dev\files\branches\myFeatureBranch\Databases"" -s MYPC\SQLEXPRESS -u sa -p adminpass diff --git a/ScriptData.cs b/ScriptData.cs index b1c7f33..6a765b1 100644 --- a/ScriptData.cs +++ b/ScriptData.cs @@ -11,12 +11,12 @@ namespace dbscript { class ScriptData { - private string m_table; - private string m_database; - private int m_limit_results; - private bool m_tbl_has_identity; - private bool m_for_fixtures; - private string m_where_clause; + private string _table; + private string _database; + private int _limit_results; + private bool _tbl_has_identity; + private bool _for_fixtures; + private string _where_clause; private const int ROW_LIMIT = 10000; @@ -32,15 +32,15 @@ public ScriptData(Connection conn, string dbFilesPath, string dbName, string tbl string dbDataScriptsPath = getDataScriptsPath(dbFilesPath, dbName, fixtures); string filename = String.Format(@"{0}\{1}.insert.sql", dbDataScriptsPath, tblName); - m_table = tblName; - m_database = dbName; - m_limit_results = limit; - m_for_fixtures = fixtures; - m_where_clause = where; + _table = tblName; + _database = dbName; + _limit_results = limit; + _for_fixtures = fixtures; + _where_clause = where; - m_tbl_has_identity = hasIdentity(conn, dbName, tblName); + _tbl_has_identity = hasIdentity(conn, dbName, tblName); - if (m_tbl_has_identity == false) Console.WriteLine("table has no identity column !!"); + if (_tbl_has_identity == false) Console.WriteLine("table has no identity column !!"); ArrayList cols = columns(conn); Console.WriteLine("got " + cols.Count + " columns "); @@ -102,7 +102,7 @@ private void generateDataScript(string filename, ArrayList cols, List private TextWriter getScriptFileForWriting(string filename, ArrayList cols) { bool trunc = true; - if (m_for_fixtures == true) trunc = false; + if (_for_fixtures == true) trunc = false; return getScriptFileForWriting(filename, cols, trunc); } @@ -111,10 +111,10 @@ private TextWriter getScriptFileForWriting(string filename, ArrayList cols, bool Console.WriteLine("generating script file: " + filename); // write file TextWriter tw = new StreamWriter(filename); - if (withTruncate == true) tw.WriteLine("TRUNCATE TABLE [{0}]", m_table); // fixtures don't truncate tables - if (m_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] ON", m_table); + if (withTruncate == true) tw.WriteLine("TRUNCATE TABLE [{0}]", _table); // fixtures don't truncate tables + if (_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] ON", _table); - tw.WriteLine("INSERT INTO [{0}] (", m_table); + tw.WriteLine("INSERT INTO [{0}] (", _table); tw.WriteLine("[" + string.Join("],[", cols.ToArray(typeof(string)) as string[]) + "]"); tw.WriteLine(")\n"); @@ -122,7 +122,7 @@ private TextWriter getScriptFileForWriting(string filename, ArrayList cols, bool } private void closeScriptFile(TextWriter tw) { - if (m_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] OFF", m_table); + if (_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] OFF", _table); tw.Close(); } @@ -133,16 +133,16 @@ private List tableData(ArrayList cols, Connection conn) string limit = ""; string order_by = ""; // if negative number for LIMIT then order descending (by first column) - if (m_limit_results != 0) + if (_limit_results != 0) { // get top n - limit = String.Format(@"TOP {0} ", Math.Abs(m_limit_results).ToString()); + limit = String.Format(@"TOP {0} ", Math.Abs(_limit_results).ToString()); // ordering order_by = "ORDER BY " + cols[0]; - if (m_limit_results < 0) order_by += " DESC"; + if (_limit_results < 0) order_by += " DESC"; } - string command = String.Format(@"SELECT {0}* FROM [{1}].[dbo].[{2}] WITH(NOLOCK) {3} {4}", limit, m_database, m_table, m_where_clause, order_by); + string command = String.Format(@"SELECT {0}* FROM [{1}].[dbo].[{2}] WITH(NOLOCK) {3} {4}", limit, _database, _table, _where_clause, order_by); SqlConnection sqlconn = new SqlConnection(conn.connectionString()); sqlconn.Open(); @@ -173,9 +173,9 @@ private ArrayList columns(Connection conn) SqlConnection sqlconn = new SqlConnection(conn.connectionString()); sqlconn.Open(); - SqlCommand cmd = new SqlCommand(m_database+".dbo.sp_columns", sqlconn); + SqlCommand cmd = new SqlCommand(_database+".dbo.sp_columns", sqlconn); cmd.CommandType = CommandType.StoredProcedure; - cmd.Parameters.Add(new SqlParameter("@table_name", m_table)); + cmd.Parameters.Add(new SqlParameter("@table_name", _table)); // execute the command SqlDataReader rdr = cmd.ExecuteReader(); @@ -190,7 +190,7 @@ private ArrayList columns(Connection conn) } private string dataScript(ArrayList cols, List data) { - string script = @"INSERT INTO [" + m_table + "] ("; + string script = @"INSERT INTO [" + _table + "] ("; script += string.Join(",", cols.ToArray(typeof(string)) as string[]); script += ")\n"; diff --git a/app.config b/app.config index 73859b0..fca2a8e 100644 --- a/app.config +++ b/app.config @@ -1,3 +1,3 @@ - + diff --git a/dbscript.csproj b/dbscript.csproj index 12129e2..ef7d11c 100644 --- a/dbscript.csproj +++ b/dbscript.csproj @@ -10,13 +10,15 @@ Properties dbscript dbscript - v3.5 + v4.5.1 512 3.5 + false + publish\ true Disk @@ -29,7 +31,6 @@ true 0 1.0.0.%2a - false false true @@ -41,6 +42,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -49,6 +51,7 @@ TRACE prompt 4 + false From d3e29deef99de88eec19bbc24211d38bae030899 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 29 Jan 2014 10:55:11 -0700 Subject: [PATCH 4/5] missed this from prev commit --- ScriptDB.cs | 77 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/ScriptDB.cs b/ScriptDB.cs index c96ddd0..b3a81c1 100644 --- a/ScriptDB.cs +++ b/ScriptDB.cs @@ -7,6 +7,7 @@ using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Sdk.Sfc; +using System.Collections.Specialized; namespace dbscript { @@ -14,14 +15,14 @@ class ScriptDB { public int objectCount = 0; - private string m_dbPath; - private string m_database; - private Connection m_connection; + private string _dbPath; + private string _database; + private Connection _connection; public ScriptDB(string dbName, Connection conn) { - m_database = dbName; - m_connection = conn; + _database = dbName; + _connection = conn; } public void scriptDB(string directory) @@ -29,13 +30,13 @@ public void scriptDB(string directory) DateTime began = DateTime.Now; - m_dbPath = generateScriptPath(directory); + _dbPath = generateScriptPath(directory); Console.WriteLine("\n**********************************************"); - Console.WriteLine("Scripting [" + m_database + "] to " + m_dbPath); + Console.WriteLine("Scripting [" + _database + "] to " + _dbPath); Console.WriteLine("**********************************************\n"); - Server srvr = m_connection.server(m_database); + Server srvr = _connection.server(_database); try { srvr.Initialize(); @@ -43,15 +44,15 @@ public void scriptDB(string directory) } catch (Exception e) { - Console.WriteLine("\nERROR: Connection to Server " + m_connection.serverName + ", Database " + m_database + " failed\n"); + Console.WriteLine("\nERROR: Connection to Server " + _connection.serverName + ", Database " + _database + " failed\n"); Console.WriteLine(e); return; } - Database db = srvr.Databases[m_database]; + Database db = srvr.Databases[_database]; if (db == null) { - Console.WriteLine("\nERROR: Database " + m_database + " does not exist\n"); + Console.WriteLine("\nERROR: Database " + _database + " does not exist\n"); return; } @@ -86,9 +87,9 @@ public void scriptDB(string directory) // done! DateTime ended = DateTime.Now; - Console.WriteLine("\n[" + m_database + "] began: " + began.ToLongTimeString() + ", ended: " + ended.ToLongTimeString()); + Console.WriteLine("\n[" + _database + "] began: " + began.ToLongTimeString() + ", ended: " + ended.ToLongTimeString()); Console.WriteLine("Number of objects scripted: " + objectCount.ToString()); - Console.WriteLine("\n[" + m_database + "] done...."); + Console.WriteLine("\n[" + _database + "] done...."); } @@ -102,10 +103,33 @@ void ScriptIt(Urn[] urn, Scripter scrp, string filename, bool withDrop) void ScriptIt(Urn[] urn, Scripter scrp, string filename) { scrp.Options.FileName = filename; + //override previous settings. + scrp.Options.IncludeIfNotExists = true; + //var createPattern = new Regex(@"(N'CREATE)(.*)$", RegexOptions.Multiline | RegexOptions.IgnoreCase); + + var createPattern = new Regex(@"CREATE PROCEDURE \[(.*)\]\.\[(.*)\](.*)", RegexOptions.Multiline | RegexOptions.IgnoreCase); + //createPattern = new Regex(@"CREATE PROCEDURE (dbo\..*)(\(.*)"); try { scrp.Script(urn); + + var reader = new StreamReader(filename); + var createScript = reader.ReadToEnd(); + reader.Close(); +//Did the initial research on this. For our needs it's not that helpful. To many regular expressions required. +/* + var alterScript = createPattern.Replace(createScript, @"CREATE PROCEDURE [$1].[$2] AS RAISERROR(''Empty Stored Proc'', 16, 1) WITH SETERROR' +END +GO +ALTER PROCEDURE [$1].[$2] +$3"); + int index = alterScript.LastIndexOf("'"); + alterScript = alterScript.Substring(0, alterScript.LastIndexOf("'")) + "GO"; + var writer = new StreamWriter(filename); + writer.Write(alterScript); + writer.Close(); +*/ } catch (Exception e) { @@ -126,7 +150,7 @@ void scriptDatabase(Database db, Scripter scrp, Urn[] urn) string filename; urn[0] = db.Urn; - filename = m_dbPath + @"\" + scrub(db.Name) + ".database.sql"; + filename = _dbPath + @"\" + scrub(db.Name) + ".database.sql"; Console.WriteLine("Database: " + db.Name); // script the database @@ -139,7 +163,7 @@ void scriptDatabase(Database db, Scripter scrp, Urn[] urn) void scriptTables(Database db, Scripter scrp, Urn[] urn) { string filename; - string tblPath = m_dbPath + @"\Up"; + string tblPath = _dbPath + @"\Up"; Directory.CreateDirectory(tblPath); foreach (Table tbl in db.Tables) @@ -170,7 +194,7 @@ void scriptTables(Database db, Scripter scrp, Urn[] urn) string command = "EXEC sp_helprotect" + " @name = '" + tbl.Name + "'" + ", @grantorname = '" + tbl.Schema + "'"; - ScriptPermissions(m_connection.connectionString(), command, filename); + ScriptPermissions(_connection.connectionString(), command, filename); // Script Table Indexes @@ -284,7 +308,7 @@ void scriptTables(Database db, Scripter scrp, Urn[] urn) void scriptViews(Database db, Scripter scrp, Urn[] urn) { string filename; - string vwPath = m_dbPath + @"\Views"; + string vwPath = _dbPath + @"\Views"; Directory.CreateDirectory(vwPath); foreach (View vw in db.Views) @@ -306,14 +330,9 @@ void scriptViews(Database db, Scripter scrp, Urn[] urn) Console.WriteLine(" View: " + vw.Schema + "." + vw.Name); // script the view - ScriptIt(urn, scrp, filename, true); + //ScriptIt(urn, scrp, filename, true); ScriptIt(urn, scrp, filename, false); - string command = "EXEC sp_helprotect" - + " @name = '" + vw.Name + "'" - + ", @grantorname = '" + vw.Schema + "'"; - - // Script View Indexes string ndxPath = vwPath + @"..\..\Indexes"; Directory.CreateDirectory(ndxPath); @@ -354,7 +373,7 @@ void scriptViews(Database db, Scripter scrp, Urn[] urn) void scriptStoredProcedures(Database db, Scripter scrp, Urn[] urn) { string filename; - string procPath = m_dbPath + @"\Sprocs"; + string procPath = _dbPath + @"\Sprocs"; Directory.CreateDirectory(procPath); scrp.Options.Permissions = true; @@ -373,7 +392,7 @@ void scriptStoredProcedures(Database db, Scripter scrp, Urn[] urn) Console.WriteLine(" Stored Procedure: " + proc.Schema + "." + proc.Name); // script the procedure with drop statement - ScriptIt(urn, scrp, filename, true); + //ScriptIt(urn, scrp, filename, true); ScriptIt(urn, scrp, filename, false); } } @@ -385,7 +404,7 @@ void scriptStoredProcedures(Database db, Scripter scrp, Urn[] urn) void scriptUserDefinedFunctions(Database db, Scripter scrp, Urn[] urn) { string filename; - string funcPath = m_dbPath + @"\Functions"; + string funcPath = _dbPath + @"\Functions"; Directory.CreateDirectory(funcPath); scrp.Options.ScriptSchema = true; @@ -404,7 +423,7 @@ void scriptUserDefinedFunctions(Database db, Scripter scrp, Urn[] urn) Console.WriteLine(" User Defined Function: " + func.Schema + "." + func.Name); // script the function with drop statement - ScriptIt(urn, scrp, filename, true); + //ScriptIt(urn, scrp, filename, true); ScriptIt(urn, scrp, filename, false); } } @@ -416,7 +435,7 @@ void scriptUserDefinedFunctions(Database db, Scripter scrp, Urn[] urn) void scriptXmlSchemaCollections(Database db, Scripter scrp, Urn[] urn) { string filename; - string xmlPath = m_dbPath + @"\XML Schema Collections"; + string xmlPath = _dbPath + @"\XML Schema Collections"; Directory.CreateDirectory(xmlPath); foreach (XmlSchemaCollection xml in db.XmlSchemaCollections) @@ -552,7 +571,7 @@ string generateScriptPath(string directory) } } - var dbPath = String.Format(@"{0}\{1}\SchemaObjects\", srvrPath, m_database); + var dbPath = String.Format(@"{0}\{1}\SchemaObjects\", srvrPath, _database); if (Directory.Exists(dbPath)) { From 5307ef874b47fc1f42904fbce759851e70a180ee Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 11 Feb 2014 08:57:52 -0700 Subject: [PATCH 5/5] added users, other modifications for roundhouse --- ScriptDB.cs | 31 ++++++++++++++++++++++++++++++- ScriptData.cs | 4 ++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/ScriptDB.cs b/ScriptDB.cs index b3a81c1..4f839b9 100644 --- a/ScriptDB.cs +++ b/ScriptDB.cs @@ -79,6 +79,7 @@ public void scriptDB(string directory) // scripting the objects //scriptDatabase(db, scrp, urn); + scriptUsers(db, scrp, urn); scriptTables(db, scrp, urn); scriptViews(db, scrp, urn); scriptStoredProcedures(db, scrp, urn); @@ -157,6 +158,34 @@ void scriptDatabase(Database db, Scripter scrp, Urn[] urn) ScriptIt(urn, scrp, filename); } + /******************************************************************************* + * Script Users + *******************************************************************************/ + void scriptUsers(Database db, Scripter scrp, Urn[] urn) + { + string filename; + string userPath = _dbPath + @"\runAfterCreateDatabase"; + Directory.CreateDirectory(userPath); + + scrp.Options.Permissions = true; + + foreach (User user in db.Users) + { + // skip system procedures + if (user.IsSystemObject) + { + continue; + } + + urn[0] = user.Urn; + + filename = userPath + @"\" + scrub(user.Name) + ".user.sql"; + Console.WriteLine(" User: " + user.Name); + + ScriptIt(urn, scrp, filename, false); + } + } + /******************************************************************************* * Script Tables *******************************************************************************/ @@ -216,7 +245,7 @@ void scriptTables(Database db, Scripter scrp, Urn[] urn) } else if (ndx.IndexKeyType.ToString() == "DriPrimaryKey") { - filename = keyPath + @"\" + scrub(tbl.Schema) + "." + scrub(tbl.Name) + filename = keyPath + @"\" + scrub(tbl.Schema) + ".1." + scrub(tbl.Name) + "." + scrub(ndx.Name) + ".pkey.sql"; } else diff --git a/ScriptData.cs b/ScriptData.cs index 6a765b1..25d3f37 100644 --- a/ScriptData.cs +++ b/ScriptData.cs @@ -18,7 +18,7 @@ class ScriptData private bool _for_fixtures; private string _where_clause; - private const int ROW_LIMIT = 10000; + private const int ROW_LIMIT = 2000; public ScriptData(Connection conn, string dbFilesPath, string dbName, string tblName) :this(conn, dbFilesPath, dbName, tblName, -1, false, "") @@ -111,7 +111,7 @@ private TextWriter getScriptFileForWriting(string filename, ArrayList cols, bool Console.WriteLine("generating script file: " + filename); // write file TextWriter tw = new StreamWriter(filename); - if (withTruncate == true) tw.WriteLine("TRUNCATE TABLE [{0}]", _table); // fixtures don't truncate tables + if (withTruncate == true) tw.WriteLine("DELETE FROM [{0}]", _table); // fixtures don't truncate tables if (_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] ON", _table); tw.WriteLine("INSERT INTO [{0}] (", _table);