BackupDatabase -> View issues

Jan 21, 2016 at 7:41 AM
After backup of my database.
It looks almost perfectly, but can I get a "CREATE TABLE IF NOT EXISTS" before the views like:
CREATE TABLE IF NOT EXISTS `calendarviewfordata` (......
CREATE TABLE IF NOT EXISTS `calendarviewforsearch` (......
......

DROP TABLE IF EXISTS `calendarviewfordata`;
DROP VIEW IF EXISTS `calendarviewfordata`;
CREATE ALGORITHM=MERGE SQL SECURITY DEFINER VIEW `calendarviewfordata` AS.....

DROP TABLE IF EXISTS `calendarviewforsearch`;
DROP VIEW IF EXISTS `calendarviewforsearch`;
CREATE ALGORITHM=MERGE SQL SECURITY DEFINER VIEW `calendarviewforsearch` AS.....
.......
I refer to different views in the first calendarviewfordata.
Then it fails on restore.
Can't any one helps me ?
Jan 21, 2016 at 7:52 AM
Edited Jan 21, 2016 at 9:50 AM
Found in Issues

I have made a small fix there put Create table in........
public static BackupDatabase (string constring, string fileDestination)
{
        using (var conn = new MySqlConnection(constring))
                {
                    using (var cmd = new MySqlCommand())
                    {
                        using (var mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ExportToFile(fileDestination);
                            conn.Close();
                        }
                    }
                }
        SortViews(tempFileDestination);
}


public static void SortViews(string fileDestination)
        {
            var fileContents = System.IO.File.ReadAllText(fileDestination);

            var newText = new List<string>();
            fileContents = fileContents.Replace("-- Dumping views", "-- Dumping views" + Environment.NewLine + @"-- SortViews CREATE TABLE IF NOT EXISTS");
            var f = fileContents.Split(';');
            var views = f.Where(y => y.Contains("SQL SECURITY DEFINER"));
            foreach (var v in views)
            {
                var s = v.Split('`');
                var tableName = s[1];
                var count = 0;
                var createTableView = @"
                CREATE TABLE IF NOT EXISTS `" + tableName + @"` (";
                var komma = "";
                foreach (var collumName in s)
                {
                    if (collumName == " AS ")
                    {
                        var name = s[count + 1];
                        createTableView += komma + "`" + name + "` INT";
                        komma = ", ";
                    }

                    count++;
                }
                createTableView += ");";
                newText.Add(createTableView);

            }
            if (newText.Count() != 0)
            {
                fileContents = fileContents.Replace("-- SortViews CREATE TABLE IF NOT EXISTS", "-- SortViews CREATE TABLE IF NOT EXISTS" + Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine, newText));
            }
            System.IO.File.WriteAllText(fileDestination, fileContents);
            
        }
Marked as answer by stigbuhl on 1/21/2016 at 1:00 AM