Example #1
0
        protected override void OnOK(object sender, EventArgs args)
        {
            if (this.ItemList.Items.Length == 0)
            {
                Context.ClientPage.ClientResponse.Alert(MessageNoItemsSelected);
                return;
            }

            List <MigrationWorker.ItemReference> itemsToProcess = new List <MigrationWorker.ItemReference>();

            foreach (ListviewItem item in this.ItemList.Items)
            {
                string[] textArray = item.Value.Split(new char[] { ':' }, 2);
                ItemUri  uri       = ItemUri.Parse(textArray[1]);
                if (uri != null)
                {
                    itemsToProcess.Add(new MigrationWorker.ItemReference(uri, textArray[0] == "recursive"));
                }
            }

            bool convertToBlob = this.TargetGroup.Value.Equals(ConvertToBlob, StringComparison.InvariantCultureIgnoreCase);
            Job  job           = MigrationWorker.CreateJob(itemsToProcess.ToArray(), convertToBlob);

            JobManager.Start(job);
            string url = "/sitecore/shell/default.aspx?xmlcontrol=MediaConversionToolWorkingForm&handle=" + job.Handle;

            SheerResponse.SetLocation(url);
        }
        /// <summary>
        /// Execute all scripts in 'scriptsFolder' if not have a record with same timestamp and filename inside the migrations table
        /// </summary>
        /// <param name="scriptsFolder"></param>
        /// <param name="formatDatabaseName"></param>
        public async void ExecuteMigrations(string scriptsFolder, bool formatDatabaseName = false)
        {
            //Try to create database if doesn't exist
            bool isDatabaseCreated = worker.CreateDatabaseIfNotExist();
            //Try to create migration table if doesn't exist
            bool isMigrationTableCreated = worker.CreateMigrationTableIfNotExist();

            if (isDatabaseCreated && isMigrationTableCreated)
            {
                //Return all current records inside the Migration Table
                List <DatabaseMigrationEntity> lstMigrations = await worker.GetCurrentMigrations();

                //Get all information inside DirectoryInfo
                DirectoryInfo d = new DirectoryInfo(scriptsFolder);
                //For each file with .sql extension
                foreach (var sqlScriptFile in d.GetFiles("*.sql"))
                {
                    //Get the Name of Sql Script
                    string fileName = sqlScriptFile.Name;
                    //Create a array removing the extension .sql from the file splitting with the separator _
                    string[] fileNameArray = fileName.Replace(".sql", "").Split('_');
                    //Get the TimeStamp based on the last index inside the array
                    string stringFileTimeStamp = fileNameArray[fileNameArray.Length - 1];

                    //Try parse the string from file to a timestamp
                    Int64 fileTimeStamp = 0;
                    Int64.TryParse(stringFileTimeStamp, out fileTimeStamp);

                    //Verify if inside the list of records exists one with the same TimeStamp and File Name
                    if (!lstMigrations.Exists(migr => migr.TimestampMigration == fileTimeStamp &&
                                              migr.FileName == fileName))
                    {
                        //Read the Sql scripts
                        string scriptQuery = File.ReadAllText(sqlScriptFile.FullName);
                        //Try to execute the script
                        bool isScriptExecuted = worker.ExecuteScriptQuery(scriptQuery, formatDatabaseName);
                        //If is successful insert the TimeStamp and File Name
                        if (isScriptExecuted)
                        {
                            worker.AddMigration(fileTimeStamp, fileName);
                        }
                    }
                }
            }

            worker = null;
        }
 /// <summary>
 /// Constructor for Migration to pass connection string and Database Name to the Worker
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="databaseName"></param>
 public Migration(string connectionString, string databaseName)
 {
     worker = new MigrationWorker(connectionString, databaseName);
 }