Esempio n. 1
0
        private ExplorerModel GetExplorerModel(string realPath)
        {
            var dirListModel  = directoryService.GetAllDirectories(realPath).Select(d => d.ToMvcDirectory());
            var fileListModel = fileService.GetAllFiles(realPath).Select(f => f.ToMvcFile());

            var explorerModel = new ExplorerModel
            {
                Directories = dirListModel,
                Files       = fileListModel
            };

            return(explorerModel);
        }
Esempio n. 2
0
        public ActionResult GetAllDirectory(string path = "")
        {
            List <FolderTreeViewModel> folderTreeObjects;

            if (path == "")
            {
                return(Redirect("/FolderTree/GetDrives/"));
            }

            path = PathValidation(path);

            try
            {
                var dirListModel  = directoryService.GetAllDirectories(path).Select(d => d.ToFolderTreeViewModel());
                var fileListModel = fileService.GetAllFiles(path).Select(f => f.ToFolderTreeViewModel());


                folderTreeObjects = new List <FolderTreeViewModel>();
                foreach (var obj in dirListModel)
                {
                    folderTreeObjects.Add(obj);
                }

                foreach (var obj in fileListModel)
                {
                    folderTreeObjects.Add(obj);
                }
            }
            catch (UnauthorizedAccessException e)
            {
                return(Json(new { Status = "NotAcceptable" }, JsonRequestBehavior.AllowGet));
            }


            path = path.Remove(1, 1);
            path = path.Replace("/", "\\\\");

            ViewBag.LastPath = path;
            if (Request.IsAjaxRequest())
            {
                return(PartialView("GetAllDirectoryTree", folderTreeObjects));
            }
            return(View("GetAllDirectoryTree", folderTreeObjects));
        }
Esempio n. 3
0
        /// <inheritdoc />
        public void RunVersionDirectories(
            IDbConnection connection,
            IDbTransaction transaction,
            List <string> appliedVersions,
            string workspace,
            string targetVersion,
            TransactionContext transactionContext,
            List <KeyValuePair <string, string> > tokens = null,
            string bulkSeparator        = null,
            string metaSchemaName       = null,
            string metaTableName        = null,
            int?commandTimeout          = null,
            int?bulkBatchSize           = null,
            string appliedByTool        = null,
            string appliedByToolVersion = null,
            string environment          = null,
            string transactionMode      = null
            )
        {
            //excludes all versions already executed
            var versionDirectories = _directoryService.GetDirectories(workspace, "v*.*")
                                     .Where(v => !appliedVersions.Contains(new DirectoryInfo(v).Name))
                                     .ToList();

            //exclude all versions greater than the target version
            if (!string.IsNullOrEmpty(targetVersion))
            {
                versionDirectories.RemoveAll(v =>
                {
                    var cv = new LocalVersion(new DirectoryInfo(v).Name);
                    var tv = new LocalVersion(targetVersion);
                    return(string.Compare(cv.SemVersion, tv.SemVersion) == 1);
                });
            }

            //execute all sql scripts in the version folders
            if (versionDirectories.Any())
            {
                versionDirectories.Sort();
                versionDirectories.ForEach(versionDirectory =>
                {
                    //initialize stop watch to measure duration of execution per version
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();

                    if (!string.IsNullOrEmpty(transactionMode) && transactionMode.Equals(TRANSACTION_MODE.VERSION))
                    {
                        using (var internalConnection = _dataService.CreateConnection())
                        {
                            internalConnection.Open();
                            using (var internalTransaction = internalConnection.BeginTransaction())
                            {
                                try
                                {
                                    if (null != internalTransaction)
                                    {
                                        _traceService.Info("Transaction created for current version. This version migration run will be executed in this dedicated connection and transaction context.");
                                    }

                                    //run scripts in all sub-directories in the version
                                    var scriptSubDirectories = _directoryService.GetAllDirectories(versionDirectory, "*").ToList();;
                                    RunVersionDirectoriesInternal(internalConnection, internalTransaction, scriptSubDirectories, versionDirectory, versionDirectory, stopwatch);

                                    internalTransaction.Commit();
                                }
                                catch (Exception)
                                {
                                    internalTransaction.Rollback();
                                    throw;
                                }
                            }
                        }
                    }
                    else
                    {
                        //collect all child directions in current version vxx.xx
                        var scriptSubDirectories = _directoryService.GetAllDirectories(versionDirectory, "*").ToList();;

                        //check for special _transaction directory in the version vxx.xx directory
                        var transactionDirectory = Path.Combine(versionDirectory, RESERVED_DIRECTORY_NAME.TRANSACTION);
                        var transactionExplicit  = _directoryService.Exists(transactionDirectory);
                        if (transactionExplicit)
                        {
                            //check version directory with _transaction directory only applies to platforms NOT supporting transactional ddl
                            if (_dataService.IsTransactionalDdlSupported)
                            {
                                throw new YuniqlMigrationException(@$ "The version directory " "{versionDirectory}" " can't contain " "{RESERVED_DIRECTORY_NAME.TRANSACTION}" " subdirectory for selected target platform, because the whole migration is already running in single transaction.");
                            }

                            //check version directory must only contain _transaction directory and nothing else
                            if (_directoryService.GetDirectories(versionDirectory, "*").Count() > 1)
                            {
                                throw new YuniqlMigrationException(@$ "The version directory " "{versionDirectory}" " containing " "{RESERVED_DIRECTORY_NAME.TRANSACTION}" " subdirectory can't contain other subdirectories.");
                            }

                            //check version directory must only contain _transaction directory, files are also not allowed
                            //check users need to place the script files and subdirectories inside _transaction directory
                            if (_directoryService.GetFiles(versionDirectory, "*.*").Count() > 0)
                            {
                                throw new YuniqlMigrationException(@$ "The version directory " "{versionDirectory}" " containing " "{RESERVED_DIRECTORY_NAME.TRANSACTION}" " subdirectory can't contain files.");
                            }

                            //override the list of subdirectories to process by the directory list container in _transaction directory
                            scriptSubDirectories = _directoryService.GetAllDirectories(transactionDirectory, "*").ToList();
                        }


                        if (transactionExplicit)
                        {
                            //run scripts within a single transaction for all scripts inside _transaction directory and scripts in the child directories
                            string versionName = new DirectoryInfo(versionDirectory).Name;
                            _traceService.Warn(@$ "The " "{RESERVED_DIRECTORY_NAME.TRANSACTION}" " directory has been detected and therefore " "{versionName}" " version scripts will run in single transaction. The rollback will not be reliable in case the version scripts contain commands causing implicit commit (e.g. DDL)!");

                            using (var transaction = connection.BeginTransaction())
                            {
                                try
                                {
                                    //scriptSubDirectories is the child directories under _transaction directory c:\temp\vxx.xx\_transaction\list_of_directories
                                    //transactionDirectory the path of _transaction directory c:\temp\vxx.xx\_transaction
                                    //versionDirectory path of version c:\temp\vxx.xx
                                    RunVersionDirectoriesInternal(connection, transaction, scriptSubDirectories, transactionDirectory, versionDirectory, stopwatch);
                                    transaction.Commit();

                                    _traceService.Info(@$ "Target database has been commited after running " "{versionName}" " version scripts.");
                                }
                                catch (Exception)
                                {
                                    _traceService.Error(@$ "Target database will be rolled back to the state before running " "{versionName}" " version scripts.");
                                    transaction.Rollback();
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            if (null == transaction)
                            {
                                _traceService.Warn("Transaction is disabled for current session. This version migration run will be executed without explicit transaction context.");
                            }

                            //run scripts without transaction
                            //scriptSubDirectories is the child directories under _transaction directory c:\temp\vxx.xx\list_of_directories
                            //versionDirectory path of version c:\temp\vxx.xx
                            RunVersionDirectoriesInternal(connection, transaction, scriptSubDirectories, versionDirectory, versionDirectory, stopwatch);
                        }
                    }

                    //reset duration timer
                    stopwatch.Restart();
                });
            }
            else
            {
                var connectionInfo = _dataService.GetConnectionInfo();
                _traceService.Info($"Target database is updated. No migration step executed at {connectionInfo.Database} on {connectionInfo.DataSource}.");
            }

            void RunVersionDirectoriesInternal(IDbConnection connection, IDbTransaction transaction, List <string> scriptSubDirectories, string scriptDirectory, string versionDirectory, Stopwatch stopwatch)
            {
                try
                {
                    var versionName = new DirectoryInfo(versionDirectory).Name;
                    scriptSubDirectories.Sort();
                    scriptSubDirectories.ForEach(scriptSubDirectory =>
                    {
                        //run all scripts in the current version folder
                        RunVersionSqlScripts(connection, transaction, transactionContext, stopwatch, versionName, workspace, scriptSubDirectory, metaSchemaName, metaTableName, tokens, commandTimeout, environment, appliedByTool, appliedByToolVersion);

                        //import csv files into tables of the the same filename as the csv
                        RunBulkImportScripts(connection, transaction, workspace, scriptSubDirectory, bulkSeparator, bulkBatchSize, commandTimeout, environment);
                    });

                    //run all scripts in the current version folder
                    RunVersionSqlScripts(connection, transaction, transactionContext, stopwatch, versionName, workspace, scriptDirectory, metaSchemaName, metaTableName, tokens, commandTimeout, environment);

                    //import csv files into tables of the the same filename as the csv
                    RunBulkImportScripts(connection, transaction, workspace, scriptDirectory, bulkSeparator, bulkBatchSize, commandTimeout, environment);

                    //update db version
                    stopwatch.Stop();
                    _metadataService.InsertVersion(connection, transaction, versionName, transactionContext,
                                                   metaSchemaName: metaSchemaName,
                                                   metaTableName: metaTableName,
                                                   commandTimeout: commandTimeout,
                                                   appliedByTool: appliedByTool,
                                                   appliedByToolVersion: appliedByToolVersion,
                                                   durationMs: Convert.ToInt32(stopwatch.ElapsedMilliseconds));

                    _traceService.Info($"Completed migration to version {versionDirectory} in {Convert.ToInt32(stopwatch.ElapsedMilliseconds)} ms");
                }
                finally
                {
                    //clear nontransactional context to ensure it is not applied on next version
                    transactionContext = null;
                }
            }
        }
Esempio n. 4
0
        private void RunVersionScripts(
            IDbConnection connection,
            IDbTransaction transaction,
            List <string> dbVersions,
            string workingPath,
            string targetVersion,
            NonTransactionalContext nonTransactionalContext,
            List <KeyValuePair <string, string> > tokenKeyPairs = null,
            string delimiter            = null,
            string schemaName           = null,
            string tableName            = null,
            int?commandTimeout          = null,
            int?batchSize               = null,
            string appliedByTool        = null,
            string appliedByToolVersion = null,
            string environmentCode      = null
            )
        {
            //excludes all versions already executed
            var versionDirectories = _directoryService.GetDirectories(workingPath, "v*.*")
                                     .Where(v => !dbVersions.Contains(new DirectoryInfo(v).Name))
                                     .ToList();

            //exclude all versions greater than the target version
            if (!string.IsNullOrEmpty(targetVersion))
            {
                versionDirectories.RemoveAll(v =>
                {
                    var cv = new LocalVersion(new DirectoryInfo(v).Name);
                    var tv = new LocalVersion(targetVersion);

                    return(string.Compare(cv.SemVersion, tv.SemVersion) == 1);
                });
            }

            //execute all sql scripts in the version folders
            if (versionDirectories.Any())
            {
                versionDirectories.Sort();
                versionDirectories.ForEach(versionDirectory =>
                {
                    try
                    {
                        //run scripts in all sub-directories
                        List <string> scriptSubDirectories = null;

                        //check for transaction directory
                        bool isExplicitTransactionDefined = false;
                        string transactionDirectory       = Path.Combine(versionDirectory, "_transaction");
                        if (_directoryService.Exists(transactionDirectory))
                        {
                            if (_dataService.IsAtomicDDLSupported)
                            {
                                throw new YuniqlMigrationException(@$ "The version directory " "{versionDirectory}" " can't contain " "_transaction" " subdirectory for selected target platform, because the whole migration is already running in single transaction.");
                            }
                            if (_directoryService.GetDirectories(versionDirectory, "*").Count() > 1)
                            {
                                throw new YuniqlMigrationException(@$ "The version directory " "{versionDirectory}" " containing " "_transaction" " subdirectory can't contain other subdirectories");
                            }
                            else if (_directoryService.GetFiles(versionDirectory, "*.*").Count() > 0)
                            {
                                throw new YuniqlMigrationException(@$ "The version directory " "{versionDirectory}" " containing " "_transaction" " subdirectory can't contain files");
                            }

                            isExplicitTransactionDefined = true;
                            scriptSubDirectories         = _directoryService.GetAllDirectories(transactionDirectory, "*").ToList();
                        }
                        else
                        {
                            scriptSubDirectories = _directoryService.GetAllDirectories(versionDirectory, "*").ToList();
                        }

                        if (isExplicitTransactionDefined)
                        {
                            string versionName = new DirectoryInfo(versionDirectory).Name;

                            //run scripts within a single transaction
                            _traceService.Info(@$ "The " "_transaction" " directory has been detected and therefore " "{versionName}" " version scripts will run in single transaction. The rollback will not be reliable in case the version scripts contain commands causing implicit commit (e.g. DDL)!");

                            using (var transaction = connection.BeginTransaction())
                            {
                                try
                                {
                                    RunVersionScriptsInternal(transaction, scriptSubDirectories, transactionDirectory, versionDirectory, schemaName, tableName);

                                    transaction.Commit();

                                    _traceService.Info(@$ "Target database has been commited after running " "{versionName}" " version scripts.");
                                }
                                catch (Exception)
                                {
                                    _traceService.Error(@$ "Target database will be rolled back to the state before running " "{versionName}" " version scripts.");
                                    transaction.Rollback();
                                    throw;
                                }
                            }
                        }
                        else //run scripts without transaction
                        {
                            RunVersionScriptsInternal(transaction, scriptSubDirectories, versionDirectory, versionDirectory, schemaName, tableName);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                });
            }
            else
            {
                var connectionInfo = _dataService.GetConnectionInfo();
                _traceService.Info($"Target database is updated. No migration step executed at {connectionInfo.Database} on {connectionInfo.DataSource}.");
            }

            void RunVersionScriptsInternal(IDbTransaction transaction, List <string> scriptSubDirectories, string scriptDirectory, string versionDirectory, string schemaName, string tableName)
            {
                try
                {
                    string versionName = new DirectoryInfo(versionDirectory).Name;

                    scriptSubDirectories.Sort();
                    scriptSubDirectories.ForEach(scriptSubDirectory =>
                    {
                        //run all scripts in the current version folder
                        RunSqlScripts(connection, transaction, nonTransactionalContext, versionName, workingPath, scriptSubDirectory, schemaName, tableName, tokenKeyPairs, commandTimeout, environmentCode, appliedByTool, appliedByToolVersion);

                        //import csv files into tables of the the same filename as the csv
                        RunBulkImport(connection, transaction, workingPath, scriptSubDirectory, delimiter, batchSize, commandTimeout, environmentCode);
                    });

                    //run all scripts in the current version folder
                    RunSqlScripts(connection, transaction, nonTransactionalContext, versionName, workingPath, scriptDirectory, schemaName, tableName, tokenKeyPairs, commandTimeout, environmentCode, appliedByTool, appliedByToolVersion);

                    //import csv files into tables of the the same filename as the csv
                    RunBulkImport(connection, transaction, workingPath, scriptDirectory, delimiter, batchSize, commandTimeout, environmentCode);

                    //update db version
                    _configurationDataService.InsertVersion(connection, transaction, versionName,
                                                            schemaName: schemaName,
                                                            tableName: tableName,
                                                            commandTimeout: commandTimeout,
                                                            appliedByTool: appliedByTool,
                                                            appliedByToolVersion: appliedByToolVersion);

                    _traceService.Info($"Completed migration to version {versionDirectory}");
                }
                finally
                {
                    //Clear nontransactional context to ensure it is not applied on next version
                    nonTransactionalContext = null;
                }
            }
        }
Esempio n. 5
0
        ///<inheritdoc/>
        public override void RunVersionScripts(
            IDbConnection connection,
            IDbTransaction transaction,
            List <string> dbVersions,
            string workingPath,
            string targetVersion,
            NonTransactionalContext nonTransactionalContext,
            List <KeyValuePair <string, string> > tokenKeyPairs = null,
            string bulkSeparator        = null,
            string metaSchemaName       = null,
            string metaTableName        = null,
            int?commandTimeout          = null,
            int?bulkBatchSize           = null,
            string appliedByTool        = null,
            string appliedByToolVersion = null,
            string environmentCode      = null
            )
        {
            //excludes all versions already executed
            var versionDirectories = _directoryService.GetDirectories(workingPath, "v*.*")
                                     .Where(v => !dbVersions.Contains(new DirectoryInfo(v).Name))
                                     .ToList();

            //exclude all versions greater than the target version
            if (!string.IsNullOrEmpty(targetVersion))
            {
                versionDirectories.RemoveAll(v =>
                {
                    var cv = new LocalVersion(new DirectoryInfo(v).Name);
                    var tv = new LocalVersion(targetVersion);

                    return(string.Compare(cv.SemVersion, tv.SemVersion) == 1);
                });
            }

            //execute all sql scripts in the version folders
            if (versionDirectories.Any())
            {
                versionDirectories.Sort();
                versionDirectories.ForEach(versionDirectory =>
                {
                    var versionName = new DirectoryInfo(versionDirectory).Name;

                    //run scripts in all sub-directories
                    var scriptSubDirectories = _directoryService.GetAllDirectories(versionDirectory, "*").ToList();
                    scriptSubDirectories.Sort();
                    scriptSubDirectories.ForEach(scriptSubDirectory =>
                    {
                        //run all scripts in the current version folder
                        RunSqlScripts(connection, transaction, nonTransactionalContext, versionName, workingPath, scriptSubDirectory, metaSchemaName, metaTableName, tokenKeyPairs, commandTimeout, environmentCode);

                        //import csv files into tables of the the same filename as the csv
                        RunBulkImport(connection, transaction, workingPath, scriptSubDirectory, bulkSeparator, bulkBatchSize, commandTimeout, environmentCode);
                    });

                    //run all scripts in the current version folder
                    RunSqlScripts(connection, transaction, nonTransactionalContext, versionName, workingPath, versionDirectory, metaSchemaName, metaTableName, tokenKeyPairs, commandTimeout, environmentCode);

                    //import csv files into tables of the the same filename as the csv
                    RunBulkImport(connection, transaction, workingPath, versionDirectory, bulkSeparator, bulkBatchSize, commandTimeout, environmentCode);

                    //update db version
                    _configurationDataService.InsertVersion(connection, transaction, versionName,
                                                            metaSchemaName: metaSchemaName,
                                                            metaTableName: metaTableName,
                                                            commandTimeout: commandTimeout,
                                                            appliedByTool: appliedByTool,
                                                            appliedByToolVersion: appliedByToolVersion);

                    _traceService.Info($"Completed migration to version {versionDirectory}");
                });
            }
            else
            {
                var connectionInfo = _dataService.GetConnectionInfo();
                _traceService.Info($"Target database is updated. No migration step executed at {connectionInfo.Database} on {connectionInfo.DataSource}.");
            }
        }