Banshee.Database.BansheeDbConnection.ValidateSchema C# (CSharp) Метод

ValidateSchema() публичный Метод

public ValidateSchema ( ) : bool
Результат bool
        public bool ValidateSchema()
        {
            bool is_valid = true;
            var new_db_path = Paths.GetTempFileName (Paths.TempDir);
            var new_db = new BansheeDbConnection (new_db_path);
            ((IInitializeService)new_db).Initialize ();

            Hyena.Log.DebugFormat ("Validating db schema for {0}", DbPath);

            var tables = new_db.QueryEnumerable<string> (
                "select name from sqlite_master where type='table' order by name"
            );

            foreach (var table in tables) {
                if (!TableExists (table)) {
                    Log.ErrorFormat ("Table {0} does not exist!", table);
                    is_valid = false;
                } else {
                    var a = new_db.SortedTableColumns (table);
                    var b = SortedTableColumns (table);

                    a.Except (b).ForEach (c => { is_valid = false; Hyena.Log.ErrorFormat ("Table {0} should contain column {1}", table, c); });
                    b.Except (a).ForEach (c => Hyena.Log.DebugFormat ("Table {0} has extra (probably obsolete) column {1}", table, c));
                }
            }

            using (var reader = new_db.Query (
                "select name,sql from sqlite_master where type='index' AND name NOT LIKE 'sqlite_autoindex%' order by name")) {
                while (reader.Read ()) {
                    string name = (string)reader[0];
                    string sql = (string)reader[1];
                    if (!IndexExists (name)) {
                        Log.ErrorFormat ("Index {0} does not exist!", name);
                        is_valid = false;
                    } else {
                        string our_sql = Query<string> ("select sql from sqlite_master where type='index' and name=?", name);
                        if (our_sql != sql) {
                            Log.ErrorFormat ("Index definition of {0} differs, should be `{1}` but is `{2}`", name, sql, our_sql);
                            is_valid = false;
                        }
                    }
                }
            }

            Hyena.Log.DebugFormat ("Done validating db schema for {0}", DbPath);
            System.IO.File.Delete (new_db_path);
            return is_valid;
        }

Usage Example

Пример #1
0
        public void Migrate ()
        {
            Paths.ApplicationName = Application.InternalName;

            int count = 0;
            foreach (string file in Directory.GetFiles (Path.Combine (TestsDir, "data"))) {
                if (file.EndsWith (".db")) {
                    var db_file = file + ".test-tmp-copy";
                    try {
                        File.Delete (db_file);
                        File.Copy (file, db_file);

                        // Call the magic methods to test the migration path
                        var db = new BansheeDbConnection (db_file);
                        SortKeyUpdater.Disable = true;
                        ((IInitializeService)db).Initialize ();
                        Assert.IsTrue (db.ValidateSchema ());
                        count++;
                    } catch (Exception e) {
                        Assert.Fail (String.Format ("Failed to migrate db: {0}", e));
                    } finally {
                        File.Delete (db_file);
                    }
                }
            }
            Assert.IsTrue (count > 0);
        }
All Usage Examples Of Banshee.Database.BansheeDbConnection::ValidateSchema