public void IsValidDownloadDirectory_WhenDirectoryDoesNotExist_ReturnsFalse()
        {
            directoryServiceMock.Exists("DownloadDirectory").Returns(false);

            bool actual = systemUnderTest.IsValidDownloadDirectory("DownloadDirectory");

            Assert.That(actual, Is.False);
        }
Beispiel #2
0
        private void CreateTemporaryDirectory(string absPath)
        {
            var tmpDir = absPath + "\\tmp";

            if (!_directoryService.Exists(tmpDir))
            {
                _directoryService.CreateDirectory(tmpDir);
            }
        }
Beispiel #3
0
        public string GetDirectory(string relativeDirectoryName)
        {
            var fullPath = Path.Combine(_rootDirectory, relativeDirectoryName);

            if (!_directoryService.Exists(fullPath))
            {
                _directoryService.Create(fullPath);
            }

            return(fullPath);
        }
Beispiel #4
0
        public Directory(string path, IDirectoryService directoryService, IFileService fileService)
        {
            if (directoryService == null)
            {
                throw new ArgumentNullException("directoryService");
            }
            _directoryService = directoryService;

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (!_directoryService.Exists(path))
            {
                throw new ArgumentException("path", "Directory does not exsit.");
            }

            if (fileService == null)
            {
                throw new ArgumentNullException("fileService");
            }

            Path         = path;
            _fileService = fileService;
        }
Beispiel #5
0
        private async void OnDeletemeMessageAsync(PackagingDeletemeMessage message)
        {
            if (message.Data.OperationType == PackageOperationType.Uninstall)
            {
                if (!_directoryService.Exists(message.Data.OperationPath))
                {
                    return;
                }

                _fileSystemService.CreateDeleteme(message.Data.Package.Id, message.Data.OperationPath);
            }

            if (message.Data.OperationType == PackageOperationType.Install)
            {
                _fileSystemService.RemoveDeleteme(message.Data.Package.Id, message.Data.OperationPath);

                //check is folder broken installation or not
                //this handle cases where we perform installation of version, previously not correctly removed
                if (await _nuGetPackageManager.IsPackageInstalledAsync(_defaultProject, message.Data.Package.GetIdentity(), default))
                {
                    return;
                }

                _fileSystemService.CreateDeleteme(message.Data.Package.Id, message.Data.OperationPath);
            }
        }
        public override async Task <IEnumerable <ISnapshot> > LoadSnapshotsAsync()
        {
            var directory = Directory;

            Log.Debug($"Loading snapshots from '{directory}'");

            var snapshots = new List <ISnapshot>();

            if (_directoryService.Exists(directory))
            {
                foreach (var snapshotFile in _directoryService.GetFiles(directory, $"*{SnapshotExtension}"))
                {
                    var snapshot = await LoadSnapshotAsync(snapshotFile);

                    if (snapshot != null)
                    {
                        snapshots.Add(snapshot);
                    }
                }
            }

            Log.Debug($"Loaded '{snapshots.Count}' snapshots from '{directory}'");

            return(snapshots);
        }
Beispiel #7
0
        public IDirectory CopyTo(string destBasePath)
        {
            if (string.IsNullOrWhiteSpace(destBasePath))
            {
                throw new ArgumentException("destBasePath cannot be null or white space.");
            }

            var destPath = destBasePath + _directoryService.DirectorySeparator + Name;

            if (_directoryService.Exists(destPath))
            {
                throw new InvalidOperationException("Target directory " + destPath + " already exists.");
            }

            var destDir = _directoryService.CreateDirectory(destPath);

            var files = _directoryService.GetFiles(this);

            foreach (var filePath in files)
            {
                var sourceFile = _fileService.OpenFile(filePath);
                var destFile   = sourceFile.Copy(destDir.Path + _directoryService.DirectorySeparator + sourceFile.Name);

                if (sourceFile.GetCheckSum() == destFile.GetCheckSum())
                {
                    throw new InvalidOperationException("Source and destinatin file have different check sum.");
                }
            }

            return(destDir);
        }
        public async Task IsAvailable_WhenInvoked_CheckForAccessToUploadDirectory(bool expected)
        {
            directoryServiceMock.Exists("C:\\Path").Returns(expected);

            bool actual = await systemUnderTest.IsAvailable();

            Assert.That(actual, Is.EqualTo(expected));
        }
Beispiel #9
0
        private bool DeleteDirectory(string fullName)
        {
            Argument.IsNotNullOrWhitespace(() => fullName);

            Log.Debug($"Deleting directory '{fullName}'");

            try
            {
                if (_directoryService.Exists(fullName))
                {
                    _directoryService.Delete(fullName, true);
                }
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"Failed to delete directory '{fullName}'");
            }

            return(!_directoryService.Exists(fullName));
        }
Beispiel #10
0
        private void PersistUserId(string userId)
        {
            var directoryName = Path.GetDirectoryName(UserIdFilePath);

            if (!_directoryService.Exists(directoryName))
            {
                _directoryService.CreateDirectory(directoryName);
            }

            _fileService.WriteAllText(UserIdFilePath, userId);
        }
Beispiel #11
0
        /// <summary>
        /// Method to invoke when the OpenInExplorer command is executed.
        /// </summary>
        private async Task OnOpenInExplorerExecuteAsync(string parameter)
        {
            if (!_directoryService.Exists(parameter))
            {
                await _messageService.ShowWarningAsync("The directory doesn't seem to exist. Cannot open the project in explorer.");

                return;
            }

            _processService.StartProcess(parameter);
        }
 /// <summary>
 /// Method to invoke when the OpenDirectory command is executed.
 /// </summary>
 private void OnOpenDirectoryExecute()
 {
     if (_directoryService.Exists(SelectedDirectory))
     {
         var fullPath = Path.GetFullPath(SelectedDirectory);
         _processService.StartProcess(new ProcessContext
         {
             FileName        = fullPath,
             UseShellExecute = true
         });
     }
 }
        public ProjectFileService(IDirectoryService directoryService, [Inject(Key = "open")] IFileDialog openDialog, [Inject(Key = "save")] IFileDialog saveDialog, IProjectLoadSave loadSave, IDialogService dialogService)
        {
            _openFileDialog  = openDialog;
            _saveFileDialog  = saveDialog;
            _loadSave        = loadSave;
            _dialogService   = dialogService;
            ProjectDirectory = directoryService.GetCurrentDirectory() + "\\" + "Projects";

            if (!directoryService.Exists(ProjectDirectory))
            {
                directoryService.CreateDirectory(ProjectDirectory);
            }
        }
Beispiel #14
0
        /// <summary>
        /// Removes Database backups older than 30 days. If all backups are older than 30 days, the latest is kept.
        /// </summary>
        public void CleanupBackups()
        {
            const int dayThreshold = 30;

            _logger.LogInformation("Beginning cleanup of Database backups at {Time}", DateTime.Now);
            var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;

            if (!_directoryService.Exists(backupDirectory))
            {
                return;
            }
            var deltaTime      = DateTime.Today.Subtract(TimeSpan.FromDays(dayThreshold));
            var allBackups     = _directoryService.GetFiles(backupDirectory).ToList();
            var expiredBackups = allBackups.Select(filename => new FileInfo(filename))
                                 .Where(f => f.CreationTime > deltaTime)
                                 .ToList();

            if (expiredBackups.Count == allBackups.Count)
            {
                _logger.LogInformation("All expired backups are older than {Threshold} days. Removing all but last backup", dayThreshold);
                var toDelete = expiredBackups.OrderByDescending(f => f.CreationTime).ToList();
                for (var i = 1; i < toDelete.Count; i++)
                {
                    try
                    {
                        toDelete[i].Delete();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "There was an issue deleting {FileName}", toDelete[i].Name);
                    }
                }
            }
            else
            {
                foreach (var file in expiredBackups)
                {
                    try
                    {
                        file.Delete();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "There was an issue deleting {FileName}", file.Name);
                    }
                }
            }
            _logger.LogInformation("Finished cleanup of Database backups at {Time}", DateTime.Now);
        }
Beispiel #15
0
        public PluginLoader(
            Func <IProvideNodes> nodeProviderFactory,
            Func <IDirectoryService> directoryServiceFactory)
        {
            _nodeProvider     = nodeProviderFactory.Invoke();
            _directoryService = directoryServiceFactory.Invoke();
            _pluginDirectory  = _directoryService.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Plugins";
            RegisterPluginNodesFromAssembly(Assembly.Load(nameof(DiiagramrAPI)), new NodeLibrary());
            if (!_directoryService.Exists(_pluginDirectory))
            {
                _directoryService.CreateDirectory(_pluginDirectory);
            }

            LoadNonPluginDll();
            GetInstalledPlugins();
        }
Beispiel #16
0
        public async Task <RepositoryDTO> GetParsedRepositoryAsync(string repositoryPath)
        {
            if (!directoryService.Exists(repositoryPath))
            {
                return(null);
            }

            RepositoryDTO repositoryDTO = new RepositoryDTO
            {
                Name = directoryService.GetName(repositoryPath),
                Path = repositoryPath
            };

            await AddSolutionsToRepositoryAsync(repositoryDTO);

            return(repositoryDTO);
        }
Beispiel #17
0
        public async Task <IResult <Unit, Error> > CopyDocAsync(string source, string destination)
        {
            var destinationDir = Path.GetDirectoryName(destination);

            if (!_directoryService.Exists(destinationDir))
            {
                _directoryService.CreateDir(destinationDir);
            }

            return(await Result.Try(async() =>
            {
                await using var sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);
                await using var destinationStream = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);

                await sourceStream.CopyToAsync(destinationStream);
            },
                                    ex => _logger.LogError(ex, $"Copy Failed! from {source} path to {destination} path")));
        }
        protected override void OnOperationStarting(object sender, PackageOperationEventArgs e)
        {
            var packagesConfig = Catel.IO.Path.Combine(Catel.IO.Path.GetParentDirectory(e.InstallPath), "packages.config");

            if (e.PackageOperationType == PackageOperationType.Uninstall)
            {
                _backupFileSystemService.BackupFolder(e.InstallPath);
                _backupFileSystemService.BackupFile(packagesConfig);

                _rollbackPackageOperationService.PushRollbackAction(() =>
                {
                    _backupFileSystemService.Restore(e.InstallPath);
                    _backupFileSystemService.Restore(packagesConfig);
                }, CurrentContext);
            }

            if (e.PackageOperationType == PackageOperationType.Install)
            {
                _rollbackPackageOperationService.PushRollbackAction(() =>
                {
                    bool success = true;
                    try
                    {
                        _directoryService.Delete(e.InstallPath);
                        success = !_directoryService.Exists(e.InstallPath);
                    }
                    catch (Exception)
                    {
                        success = false;
                    }
                    finally
                    {
                        if (!success)
                        {
                            _fileSystemService.CreateDeleteme(e.PackageDetails.Id, e.InstallPath);
                            Log.Error($"Failed to delete directory {e.InstallPath} during rollback actions.");
                        }
                    }
                },
                                                                    CurrentContext
                                                                    );
            }
        }
Beispiel #19
0
        public IKernel Load(IKernel kernel)
        {
            var executableLocation    = _assemblyService.GetEntryAssembly().Location;
            var additionalPluginsPath = _pathService.Combine(_pathService.GetDirectoryName(executableLocation), "Plugins");

            if (!_directoryService.Exists(additionalPluginsPath))
            {
                _directoryService.CreateDirectory(additionalPluginsPath);
            }

            kernel.Bind(x => x
                        .FromAssembliesInPath(additionalPluginsPath)
                        .SelectAllClasses()
                        .InheritedFrom <IPlugin>()
                        .BindDefaultInterfaces()
                        .Configure(y => y.InTransientScope()));

            return(kernel);
        }
Beispiel #20
0
        public Task SerializeDataAsync(string destFolder)
        {
            return(Task.Run(() =>
            {
                using (var db = dbFactory.Create())
                {
                    foreach (var request in db.Requests)
                    {
                        var fileName = request.Date.ToString("yyyy-MM-dd") + ".xml";

                        if (!directory.Exists(destFolder))
                        {
                            directory.CreateDirectory(destFolder);
                        }

                        var fullPath = Path.Combine(destFolder, fileName);

                        if (file.Exists(fullPath))
                        {
                            file.Delete(fullPath);
                        }

                        using (var stream = file.Create(fullPath))
                        {
                            var xmlModel = new XmlRequestModel
                            {
                                Index = request.Index,
                                Content = new XmlRequestContentModel
                                {
                                    Date = request.Date,
                                    Name = request.Name,
                                    Visits = request.Visits
                                }
                            };

                            var serializer = new XmlSerializer(typeof(XmlRequestModel));
                            serializer.Serialize(stream, xmlModel);
                        }
                    }
                }
            }));
        }
Beispiel #21
0
        public bool Copy(string source, string target)
        {
            if (_fileService.Exists(source))
            {
                var sourceFile = _fileService.OpenFile(source);
                var targetFile = _fileService.Copy(sourceFile, target);

                return(targetFile != null);
            }

            if (_directoryService.Exists(source))
            {
                var sourceDir = _directoryService.OpenDirectory(source);
                var targetDir = sourceDir.CopyTo(target);

                return(targetDir != null);
            }

            throw new ArgumentException("Unknown source.");
        }
        private void bindAdditionalPlugins()
        {
            _assemblyService  = _kernel.Get <IAssemblyService>();
            _directoryService = _kernel.Get <IDirectoryService>();
            _pathService      = _kernel.Get <IPathService>();

            var executableLocation    = _assemblyService.GetEntryAssembly().Location;
            var additionalPluginsPath = _pathService.Combine(_pathService.GetDirectoryName(executableLocation), "Plugins");

            if (!_directoryService.Exists(additionalPluginsPath))
            {
                _directoryService.CreateDirectory(additionalPluginsPath);
            }

            _kernel.Bind(x => x
                         .FromAssembliesInPath(additionalPluginsPath)
                         .SelectAllClasses()
                         .InheritedFrom <IPlugin>()
                         .BindDefaultInterfaces()
                         .Configure(y => y.InTransientScope()));
        }
Beispiel #23
0
        internal virtual string ResolveAndValidateCacheDirectory(bool cache, string cacheDirectory)
        {
            if (cache)
            {
                if (string.IsNullOrWhiteSpace(cacheDirectory))
                {
                    return(Path.Combine(Directory.GetCurrentDirectory(), "ContentCache")); // Always valid
                }

                if (!_directoryService.Exists(cacheDirectory))
                {
                    throw new OptionsException(nameof(IFlexiIncludeBlockOptions.CacheDirectory), string.Format(Strings.OptionsException_Shared_DirectoryDoesNotExist, cacheDirectory));
                }

                return(cacheDirectory);
            }
            else
            {
                return(null);
            }
        }
        public Task <bool> IsAvailable()
        {
            string uploadDirectory = settings.UploadDirectory;

            return(Task.FromResult(directoryService.Exists(uploadDirectory)));
        }
Beispiel #25
0
 public bool IsValidDownloadDirectory(string unsafeDownloadDirectory)
 {
     return(directoryService.Exists(unsafeDownloadDirectory));
 }
        ///<inheritdoc/>
        public void Init(string workspace)
        {
            string initDirectoryPath = Path.Combine(workspace, RESERVED_DIRECTORY_NAME.INIT);

            if (!_directoryService.Exists(initDirectoryPath))
            {
                _directoryService.CreateDirectory(initDirectoryPath);
                _fileService.AppendAllText(Path.Combine(initDirectoryPath, RESERVED_FILE_NAME.README), @$ "# The `{RESERVED_DIRECTORY_NAME.INIT}` directory
Initialization scripts. Executed once. This is called the first time you do `yuniql run`.");
                _traceService.Info($"Created script directory {initDirectoryPath}");
            }

            string preDirectoryPath = Path.Combine(workspace, RESERVED_DIRECTORY_NAME.PRE);

            if (!_directoryService.Exists(preDirectoryPath))
            {
                _directoryService.CreateDirectory(preDirectoryPath);
                _fileService.AppendAllText(Path.Combine(preDirectoryPath, RESERVED_FILE_NAME.README), @$ "# The `{RESERVED_DIRECTORY_NAME.PRE}` directory
Pre migration scripts. Executed every time before any version. 
");
                _traceService.Info($"Created script directory {preDirectoryPath}");
            }

            string defaultVersionDirectoryPath = Path.Combine(workspace, RESERVED_DIRECTORY_NAME.BASELINE);

            if (!_directoryService.Exists(defaultVersionDirectoryPath))
            {
                _directoryService.CreateDirectory(defaultVersionDirectoryPath);
                _fileService.AppendAllText(Path.Combine(defaultVersionDirectoryPath, RESERVED_FILE_NAME.README), @"# The `v0.00` directory
Baseline scripts. Executed once. This is called when you do `yuniql run`.");
                _traceService.Info($"Created script directory {defaultVersionDirectoryPath}");
            }

            string draftDirectoryPath = Path.Combine(workspace, RESERVED_DIRECTORY_NAME.DRAFT);

            if (!_directoryService.Exists(draftDirectoryPath))
            {
                _directoryService.CreateDirectory(draftDirectoryPath);
                _fileService.AppendAllText(Path.Combine(draftDirectoryPath, RESERVED_FILE_NAME.README), $@"# The `{RESERVED_DIRECTORY_NAME.DRAFT}` directory
Scripts in progress. Scripts that you are currently working and have not moved to specific version directory yet. Executed every time after the latest version.");
                _traceService.Info($"Created script directory {draftDirectoryPath}");
            }

            string postDirectoryPath = Path.Combine(workspace, RESERVED_DIRECTORY_NAME.POST);

            if (!_directoryService.Exists(postDirectoryPath))
            {
                _directoryService.CreateDirectory(postDirectoryPath);
                _fileService.AppendAllText(Path.Combine(postDirectoryPath, RESERVED_FILE_NAME.README), $@"# The `{RESERVED_DIRECTORY_NAME.POST}` directory
Post migration scripts. Executed every time and always the last batch to run.");
                _traceService.Info($"Created script directory {postDirectoryPath}");
            }

            string eraseDirectoryPath = Path.Combine(workspace, RESERVED_DIRECTORY_NAME.ERASE);

            if (!_directoryService.Exists(eraseDirectoryPath))
            {
                _directoryService.CreateDirectory(eraseDirectoryPath);
                _fileService.AppendAllText(Path.Combine(eraseDirectoryPath, RESERVED_FILE_NAME.README), $@"# The `{RESERVED_DIRECTORY_NAME.ERASE}` directory
Database cleanup scripts. Executed once only when you do `yuniql erase`.");
                _traceService.Info($"Created script directory {eraseDirectoryPath}");
            }

            var readMeFile = Path.Combine(workspace, RESERVED_FILE_NAME.README);

            if (!_fileService.Exists(readMeFile))
            {
                var assembly          = typeof(WorkspaceService).Assembly;
                var embededReadMeFile = $"{assembly.GetName().Name}.TemplateReadMe.md";
                _fileService.AppendAllText(readMeFile, _fileService.ReadAllEmbeddedText(embededReadMeFile));
                _traceService.Info($"Created file {readMeFile}");
            }

            var dockerFile = Path.Combine(workspace, RESERVED_FILE_NAME.DOCKER_FILE);

            if (!_fileService.Exists(dockerFile))
            {
                _fileService.AppendAllText(dockerFile, @"FROM yuniql/yuniql:latest
COPY . ./db                
");
                _traceService.Info($"Created file {dockerFile}");
            }

            var gitIgnoreFile = Path.Combine(workspace, RESERVED_FILE_NAME.GIT_IGNORE_FILE);

            if (!_fileService.Exists(gitIgnoreFile))
            {
                _fileService.AppendAllText(gitIgnoreFile, @"
.plugins
yuniql.exe
yuniql.pdb
yuniqlx.exe
yuniql-log-*.txt
");
                _traceService.Info($"Created file {gitIgnoreFile}");
            }
        }
Beispiel #27
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;
                }
            }
        }
        public async Task <bool> RunAsync()
        {
            var folderProject = new FolderNuGetProject(_defaultProject.ContentPath);

            if (!_directoryService.Exists(_defaultProject.ContentPath))
            {
                Log.Info($"Plugins folder does not exist");
                return(false);
            }

            var subFolders = folderProject.GetPackageDirectories();

            var failedIdentities = new List <PackageIdentity>();
            var parsedPackages   = new List <IPackageDetails>();

            bool anyUpgraded = false;

            using (var context = AcquireSourceContextForActions())
            {
                if (context == SourceContext.EmptyContext)
                {
                    Log.Warning($"Source context is empty");

                    return(false);
                }

                foreach (var folder in subFolders)
                {
                    var packageFolderName = Path.GetFileName(folder);
                    var package           = PackageIdentityParser.Parse(packageFolderName);

                    var packageDetails = PackageDetailsFactory.Create(package);

                    parsedPackages.Add(packageDetails);
                }

                //no valid package folders
                if (!parsedPackages.Any())
                {
                    return(false);
                }

                _packageOperationNotificationService.NotifyAutomaticOperationBatchStarting(PackageOperationType.Install, parsedPackages.ToArray());

                foreach (var packageDetails in parsedPackages)
                {
                    var package = packageDetails.GetIdentity();
                    if (package is null)
                    {
                        continue;
                    }

                    var installationPath = _defaultProject.GetInstallPath(package);

                    var isV2packageInstalled = folderProject.PackageExists(package, NuGet.Packaging.PackageSaveMode.Defaultv2);
                    if (!isV2packageInstalled)
                    {
                        Log.Warning($"Package '{package}' is recognized in project folder as v2 NuGet installed package");
                        continue;
                    }

                    if (await _nuGetPackageManager.IsPackageInstalledAsync(_defaultProject, package, default))
                    {
                        Log.Info($"Skipping package '{package}', package is valid");
                        continue;
                    }

                    _packageOperationNotificationService.NotifyAutomaticOperationStarting(installationPath, PackageOperationType.Install, packageDetails);

                    //reinstall
                    try
                    {
                        var isInstalled = await _nuGetPackageManager.InstallPackageForProjectAsync(_defaultProject, package, default, false);
Beispiel #29
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;
                }
            }
        }