protected virtual void WriteScript(string objectName, string sql) { Throw.If(objectName).IsEmpty(); if (sql != null) { sql = sql.Trim(); } if (string.IsNullOrEmpty(sql)) { messageManager.OnScriptMessage(string.Format("{0} is empty.", objectName)); } else { string scriptPath = Path.Combine(exportDirectory, scriptName); try { if (!Directory.Exists(exportDirectory)) { Directory.CreateDirectory(exportDirectory); } if (File.Exists(scriptPath)) { File.SetAttributes(scriptPath, FileAttributes.Normal); } using (TextWriter scriptFile = new StreamWriter(scriptPath, append)) { scriptFile.WriteLine(""); scriptFile.WriteLine(sql); } append = true; } catch (Exception ex) { string msg = string.Format("Could not write the script file {0} to disk.", scriptPath); throw new SqlMigrationException(msg, ex); } messageManager.OnScriptMessage(scriptPath); } }
protected virtual void WriteScript(string objectName, string subFolderName, string sql) { Throw.If(objectName).IsEmpty(); if (sql != null) { sql = sql.Trim(); } // write sproc to file string dir = Path.Combine(exportDirectory, subFolderName); string scriptPath = Path.Combine(dir, objectName + ".sql"); if (string.IsNullOrEmpty(sql)) { messageManager.OnScriptMessage(string.Format("{0} is empty.", objectName)); } else { try { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (File.Exists(scriptPath)) { File.SetAttributes(scriptPath, FileAttributes.Normal); } using (StreamWriter scriptFile = new StreamWriter(scriptPath, false)) { scriptFile.Write(sql); } } catch (Exception ex) { string msg = string.Format("Could not write the script file {0} to disk.", scriptPath); throw new SqlMigrationException(msg, ex); } messageManager.OnScriptMessage(scriptPath); } }
protected virtual void WriteXmlFile(string objectName, string xml) { Throw.If(objectName).IsEmpty(); if (xml != null) { xml = xml.Trim(); } if (string.IsNullOrEmpty(xml)) { messageManager.OnScriptMessage(string.Format("{0} is empty.", objectName)); } else { string dir = Path.Combine(exportDirectory, "Data"); string scriptPath = Path.Combine(dir, objectName + ".xml"); try { if (!fileSystem.Exists(dir)) { fileSystem.CreateDirectory(dir); } if (fileSystem.Exists(scriptPath)) { fileSystem.SetFileAttributes(scriptPath, FileAttributes.Normal); } using (TextWriter scriptFile = fileSystem.OpenFileForWriting(scriptPath)) { scriptFile.WriteLine(xml); } } catch (Exception ex) { string msg = string.Format("Could not write the xml file {0} to disk.", scriptPath); throw new SqlMigrationException(msg, ex); } messageManager.OnScriptMessage(scriptPath); } }
/// <summary> /// Scripts a database to disk for use in creating a brand new db. This /// method scripts the entire object, i.e. no differntial or comparisons /// are done. /// </summary> /// <param name="connectionInfo">The db connection parameters.</param> /// <param name="scriptingOptions">The export scripting settings.</param> public virtual void Script(IConnectionSettings connectionInfo, IScriptingOptions scriptingOptions) { Throw.If(connectionInfo, "connectionInfo").IsNull(); Throw.If(scriptingOptions, "scriptingOptions").IsNull(); messageManager.OnScriptMessage("Starting database scripting."); IDatabase db = DatabaseConnectionFactory.CreateDbConnection(connectionInfo); List <ITable> tablesToScript = new List <ITable>(); foreach (DbObjectName tableName in scriptingOptions.TablesToScript) { ITable table = db.Tables[tableName.ShortName]; if (table == null) { throw new SqlMigrationException( string.Format("Unable to find the table {0} in database {1} on server.", tableName, connectionInfo.Database)); } tablesToScript.Add(table); } IScriptWriter writer = CreateScriptWriter(scriptingOptions, connectionInfo); int totalObjects = CalculateScriptObjectCount(scriptingOptions); int exportCount = 0; if (scriptingOptions.ScriptSchema) { foreach (ITable table in tablesToScript) { ScriptTableSchema(table, writer); OnProgressEvent(++exportCount, totalObjects); } } if (scriptingOptions.ScriptIndexes) { foreach (ITable table in tablesToScript) { ScriptTableIndexes(table, writer); OnProgressEvent(++exportCount, totalObjects); } } if (scriptingOptions.ScriptData || scriptingOptions.ScriptDataAsXml) { IScriptWriter dataWriter = writer; IDataMigrator dataMigrator = new DataMigrator(); if (scriptingOptions.ScriptDataAsXml) { dataMigrator = new XmlDataMigrator(); dataWriter = new XmlDataWriter(scriptingOptions.ExportDirectory, messageManager); } foreach (ITable table in tablesToScript) { ScriptTableData(dataMigrator, table, dataWriter); OnProgressEvent(++exportCount, totalObjects); } } if (scriptingOptions.ScriptForeignKeys) { foreach (ITable table in tablesToScript) { ScriptTableForeignKeys(table, writer); OnProgressEvent(++exportCount, totalObjects); } } foreach (DbObjectName sprocName in scriptingOptions.SprocsToScript) { IProcedure sproc = db.Procedures[sprocName.ShortName]; if (sproc == null) { throw new SqlMigrationException( string.Format("Unable to find the procedure {0} in database {1} on server.", sprocName, connectionInfo.Database)); } ScriptSproc(sproc, writer); OnProgressEvent(++exportCount, totalObjects); } foreach (DbObjectName viewName in scriptingOptions.ViewsToScript) { IView view = db.Views[viewName.ShortName]; if (view == null) { throw new SqlMigrationException( string.Format("Unable to find the view {0} in database {1} on server.", viewName, connectionInfo.Database)); } ScriptView(view, writer); OnProgressEvent(++exportCount, totalObjects); } messageManager.OnScriptMessage("Finished database scripting."); }