public void Run(string[] args) { bool showHelp = false; string repoPath = string.Empty; int? version = null; optionSet = new OptionSet() .Add("?|help", "Show this message and quit", option => showHelp = option != null) .Add("repository-path=", "Required: Full path to the repository path", option => repoPath = option) .Add("dbuser="******"Required: Username for the database", option => dbUser = option) .Add("dbpass="******"Required: Password for the database", option => dbPass = option) .Add("dbname=", "Required: Database name", option => dbName = option) .Add("dbhost=", "Required: Database hostname", option => dbHostname = option) .Add("dbtype=", "Required: Database type e.g. sqlite, postgresql, mysql", option => dbType = option) .Add("version=", "For upgrade/downgrade determines what version to go to", option => version = int.Parse(option)); try { extraArgs = optionSet.Parse(args); } catch (OptionException e) { Console.Write("Sector: "); Console.WriteLine(e.Message); Console.WriteLine("Try `Sector --help' for more information."); return; } if (showHelp) { ShowHelp(optionSet); return; } if (extraArgs.Count < 1) { Console.WriteLine("Missing command"); ShowHelp(optionSet); return; } if (string.IsNullOrEmpty(repoPath)) { Console.WriteLine("Missing repository path"); return; } // Now parse sector.cfg Repository repository = new Repository(repoPath); ISectorDb sectorDb = SectorDb.FromDbInfo(dbType, dbHostname, dbUser, dbName, dbPass); sectorDb.Connect(); migrateApi = new MigrateApi(sectorDb); string command = extraArgs[0]; switch (command) { case "migrate_version_control": { migrateApi.VersionControl(repository); break; } case "migrate_db_version": { int dbVersion = migrateApi.GetDbVersion(repository); Console.WriteLine(dbVersion.ToString()); break; } case "migrate_version": { int repoVersion = repository.GetVersion(); Console.WriteLine(repoVersion.ToString()); break; } case "migrate_upgrade": { int upVersion = version.GetValueOrDefault(repository.GetVersion()); migrateApi.Upgrade(repository, upVersion); break; } case "migrate_downgrade": { if (!version.HasValue) { Console.WriteLine("Missing version for downgrade"); return; } migrateApi.Downgrade(repository, version.Value); break; } default: { Console.WriteLine("Invalid command"); return; } } sectorDb.Dispose(); }
public void Downgrade_Straight() { var sectorDb = TestUtils.MakeSectorDb(); var repository = TestUtils.MakeRepository(); MigrateApi migrateApi = new MigrateApi(sectorDb); migrateApi.VersionControl(repository); using (var dbConn = TestUtils.OpenDbconnection()) { migrateApi.Upgrade(repository, 2); Assert.AreEqual(2, migrateApi.GetDbVersion(repository)); using (var dbCommand = dbConn.CreateCommand()) { dbCommand.CommandText = "SELECT * FROM testie"; dbCommand.ExecuteNonQuery(); } using (var dbCommand = dbConn.CreateCommand()) { dbCommand.CommandText = "SELECT * FROM moon"; dbCommand.ExecuteNonQuery(); } migrateApi.Downgrade(repository, 0); Assert.AreEqual(0, migrateApi.GetDbVersion(repository)); using (var dbCommand = dbConn.CreateCommand()) { dbCommand.CommandText = "SELECT * FROM testie"; Assert.Throws<SqliteException>(delegate { dbCommand.ExecuteNonQuery(); }); } using (var dbCommand = dbConn.CreateCommand()) { dbCommand.CommandText = "SELECT * FROM moon"; Assert.Throws<SqliteException>(delegate { dbCommand.ExecuteNonQuery(); }); } } }