Ejemplo n.º 1
0
        public void ExportDataToScripts(string query, string _DataDirectory, DataScriptType dataScriptType, SqlScriptType sqlScriptType, bool includeSoftDependencies, bool excludeUnwantedDependencies, bool autoMerge, string environmentName)
        {
            Dictionary <string, ExtractedTable> results = ExportDataFromDatabase(query, includeSoftDependencies, excludeUnwantedDependencies);

            string dataScriptDirectory = string.Empty;

            if (dataScriptType == DataScriptType.Static)
            {
                dataScriptDirectory = Path.Combine(_DataDirectory, "Static");
            }
            else if (dataScriptType == DataScriptType.Test)
            {
                dataScriptDirectory = Path.Combine(_DataDirectory, "Test");
            }

            if (!Directory.Exists(dataScriptDirectory))
            {
                Directory.CreateDirectory(dataScriptDirectory);
            }

            // Check for existing script files if autoMerge not set
            if (!autoMerge)
            {
                List <string> existingScripts = new List <string>();
                foreach (string tableName in results.Keys)
                {
                    string scriptName = $"{tableName}.sql";
                    if (File.Exists(Path.Combine(dataScriptDirectory, scriptName)))
                    {
                        existingScripts.Add(scriptName);
                    }
                }
                if (existingScripts.Count > 0)
                {
                    throw new System.Exception($"{sqlScriptType.ToString()} script already exists for: {string.Join(", ", existingScripts)}. Enable auto merge to merge the new data with the existing data.");
                }
            }
            // Cleanse data
            DataCleanser dataCleanser = new DataCleanser();

            dataCleanser.ReplaceImagePathFields(results.Values.Select(et => et.DataTable).ToList());

            // Create scripts for each table
            foreach (string tableName in results.Keys)
            {
                FileInfo scriptFileInfo = new FileInfo(Path.Combine(dataScriptDirectory, $"{tableName}.sql"));
                // If script already exists, merge
                if (scriptFileInfo.Exists)
                {
                    DataTable scriptDataTable = null;
                    if (sqlScriptType == SqlScriptType.Insert)
                    {
                        scriptDataTable = SqlDataScriptHelper.NewDataTableFromInsertScript(scriptFileInfo.FullName, DataParser.GetXmlSchemaFromDataTable(results[tableName].DataTable));
                    }
                    else if (sqlScriptType == SqlScriptType.Merge)
                    {
                        // todo
                        throw new System.Exception("Not implemented");
                    }
                    results[tableName].DataTable.Merge(scriptDataTable);
                }
                string script = null;
                if (sqlScriptType == SqlScriptType.Insert)
                {
                    script = SqlDataScriptHelper.NewInsertScriptFromDataTable(results[tableName].TableName.Schema, results[tableName].TableName.Name, results[tableName].DataTable, results[tableName].PrimaryKeyColumnNames);
                }
                else if (sqlScriptType == SqlScriptType.Merge)
                {
                    script = SqlDataScriptHelper.NewMergeScriptFromDataTable(results[tableName].TableName.Schema, results[tableName].TableName.Name, results[tableName].DataTable, false, true, environmentName, results[tableName].PrimaryKeyColumnNames);
                }
                SqlDataScriptHelper.WriteScriptToFile(scriptFileInfo.Name, script, dataScriptDirectory, true);
            }

            string sqlCmdScript     = SqlDataScriptHelper.NewSqlCmdScript(dataScriptDirectory, dataScriptType);
            string sqlCmdScriptName = string.Empty;

            if (dataScriptType == DataScriptType.Static)
            {
                sqlCmdScriptName = "StaticData.sql";
            }
            else if (dataScriptType == DataScriptType.Test)
            {
                sqlCmdScriptName = "TestData.sql";
            }
            SqlDataScriptHelper.WriteScriptToFile(sqlCmdScriptName, sqlCmdScript, _DataDirectory, true);
        }