public LiteDatabase OpenDatabase() { if (string.IsNullOrEmpty(Path)) { throw new Exception("Database path cannot be empty."); } var dbExists = File.Exists(Path); logger.Info("Opening db " + Path); CloseDatabase(); Database = new LiteDatabase($"Filename={Path};Mode=Exclusive"); // To force litedb to try to open file, should throw exceptuion if something is wrong with db file Database.GetCollectionNames(); if (!dbExists) { Database.Engine.UserVersion = DBVersion; } else { if (Database.Engine.UserVersion > DBVersion) { throw new Exception($"Database version {Database.Engine.UserVersion} is not supported."); } if (GetMigrationRequired(Database)) { throw new Exception("Database must be migrated before opening."); } } GamesCollection = Database.GetCollection <IGame>("games"); PlatformsCollection = Database.GetCollection <Platform>("platforms"); EmulatorsCollection = Database.GetCollection <Emulator>("emulators"); if (!dbExists) { GamesCollection.EnsureIndex(a => a.Id); PlatformsCollection.EnsureIndex(a => a.Id); EmulatorsCollection.EnsureIndex(a => a.Id); // Generate default platforms if (File.Exists(EmulatorDefinition.DefinitionsPath)) { var platforms = EmulatorDefinition.GetDefinitions() .SelectMany(a => a.Profiles.SelectMany(b => b.Platforms)).Distinct() .Select(a => new Platform(a)).ToList(); AddPlatform(platforms); } } DatabaseOpened?.Invoke(this, null); IsOpen = true; return(Database); }
public LiteDatabase OpenDatabase(string path) { logger.Info("Opening db " + path); CloseDatabase(); Database = new LiteDatabase(path); MigrateDatabase(); // To force litedb to try to open file, should throw exceptuion if something is wrong with db file Database.GetCollectionNames(); GamesCollection = Database.GetCollection <IGame>("games"); PlatformsCollection = Database.GetCollection <Platform>("platforms"); EmulatorsCollection = Database.GetCollection <Emulator>("emulators"); DatabaseOpened?.Invoke(this, null); return(Database); }