public static List <SqlServerScript> ExecuteScriptsInFolders(IEnumerable <string> folderPaths, SqlServerDatabase targetDatabase, int?maxRetries)
        {
            List <SqlServerScript> scripts = new List <SqlServerScript>();

            foreach (string folderPath in folderPaths)
            {
                foreach (string fileName in Directory.EnumerateFiles(folderPath, "*.sql"))
                {
                    SqlServerScript script = new SqlServerScript(Path.Combine(folderPath, fileName));

                    foreach (ISqlBatch batch in script.Batches)
                    {
                        targetDatabase.ExecuteSqlBatch(batch);
                    }

                    scripts.Add(script);
                }
            }

            int countFailedBatches;
            int previousCountFailedBatches = 0;

            while (maxRetries > 0 || maxRetries == null)
            {
                countFailedBatches = 0;

                foreach (SqlServerScript script in scripts)
                {
                    foreach (SqlServerBatch batch in script.Batches)
                    {
                        if (batch.Executions.Last().Exception != null)
                        {
                            countFailedBatches++;
                            targetDatabase.ExecuteSqlBatch(batch);
                        }
                    }
                }

                if (countFailedBatches == previousCountFailedBatches)
                {
                    break;
                }

                previousCountFailedBatches = countFailedBatches;
                maxRetries--;
            }

            return(scripts);
        }
        public static List<SqlServerScript> ExecuteScriptsInFolders(IEnumerable<string> folderPaths, SqlServerDatabase targetDatabase, int? maxRetries)
        {
            List<SqlServerScript> scripts = new List<SqlServerScript>();

            foreach (string folderPath in folderPaths)
            {
                foreach (string fileName in Directory.EnumerateFiles(folderPath, "*.sql"))
                {
                    SqlServerScript script = new SqlServerScript(Path.Combine(folderPath, fileName));

                    foreach (ISqlBatch batch in script.Batches)
                    {
                        targetDatabase.ExecuteSqlBatch(batch);
                    }

                    scripts.Add(script);
                }
            }

            int countFailedBatches;
            int previousCountFailedBatches = 0;

            while (maxRetries > 0 || maxRetries == null)
            {
                countFailedBatches = 0;

                foreach (SqlServerScript script in scripts)
                {
                    foreach (SqlServerBatch batch in script.Batches)
                    {
                        if (batch.Executions.Last().Exception != null)
                        {
                            countFailedBatches++;
                            targetDatabase.ExecuteSqlBatch(batch);
                        }
                    }
                }

                if (countFailedBatches == previousCountFailedBatches)
                    break;

                previousCountFailedBatches = countFailedBatches;
                maxRetries--;
            }

            return scripts;
        }
Esempio n. 3
0
        public IDatabase CreateDatabase(string databaseName)
        {
            SqlServerBatch batch =
                new SqlServerBatch(
                    String.Format(
                        @"CREATE DATABASE {0};",
                        TransactSqlHelpers.Identifiers.ValidIdentifier(databaseName)));

            _masterDatabase.ExecuteSqlBatch(batch);

            Exception lastBatchExecutionException = batch.Executions.Last().Exception;

            if (lastBatchExecutionException != null)
            {
                throw new ApplicationException("Database creation failed.", lastBatchExecutionException);
            }

            return(new SqlServerDatabase(this.ConnectionString(databaseName)));
        }