Exemplo n.º 1
0
Arquivo: Main.cs Projeto: ahall/Sector
        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();
        }
Exemplo n.º 2
0
        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();
                    });
                }
            }
        }
Exemplo n.º 3
0
        public void VersionControl()
        {
            var sectorDb = TestUtils.MakeSectorDb();
            var repository = TestUtils.MakeRepository();

            MigrateApi migrateApi = new MigrateApi(sectorDb);
            Assert.IsFalse(migrateApi.IsVersionControlled(repository));

            migrateApi.VersionControl(repository);
            Assert.IsTrue(migrateApi.IsVersionControlled(repository));
            Assert.AreEqual(0, migrateApi.GetDbVersion(repository));

            using (var dbConn = TestUtils.OpenDbconnection())
            using (var dbCommand = dbConn.CreateCommand())
            {
                string templ = "SELECT * FROM {0} WHERE repository_id = '{1}'";
                dbCommand.CommandText = string.Format(templ, SectorDb.TableName, repository.RepositoryId);
                var reader = dbCommand.ExecuteReader();

                Assert.AreEqual(repository.RepositoryId, reader["repository_id"]);
                Assert.AreEqual(repository.RepositoryPath, reader["repository_path"]);
                Assert.AreEqual(0, reader["version"]);
            }

            // Trying again results in SectorException.
            Assert.Throws<SectorException>(delegate {
                migrateApi.VersionControl(repository);
            });
        }
Exemplo n.º 4
0
        public void Upgrade_Incremental()
        {
            var sectorDb = TestUtils.MakeSectorDb();
            var repository = TestUtils.MakeRepository();

            MigrateApi migrateApi = new MigrateApi(sectorDb);
            migrateApi.VersionControl(repository);

            using (var dbConn = TestUtils.OpenDbconnection())
            {
                Assert.AreEqual(0, migrateApi.GetDbVersion(repository));

                using (var dbCommand = dbConn.CreateCommand())
                {
                    dbCommand.CommandText = "SELECT * FROM testie";

                    Assert.Throws<SqliteException>(delegate {
                        dbCommand.ExecuteNonQuery();
                    });
                }

                // Upgrading to 1, will add testie.
                migrateApi.Upgrade(repository, 1);
                Assert.AreEqual(1, migrateApi.GetDbVersion(repository));

                using (var dbCommand = dbConn.CreateCommand())
                {
                    dbCommand.CommandText = "SELECT * FROM testie";
                    dbCommand.ExecuteNonQuery();
                }

                // moon comes in in version 2, so shouldnt be there now.
                using (var dbCommand = dbConn.CreateCommand())
                {
                    dbCommand.CommandText = "SELECT * FROM moon";

                    Assert.Throws<SqliteException>(delegate {
                        dbCommand.ExecuteNonQuery();
                    });
                }

                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();
                }
            }
        }
Exemplo n.º 5
0
        public void GetDbVersion_EmptyDb()
        {
            var sectorDb = TestUtils.MakeSectorDb();
            var repository = TestUtils.MakeRepository();

            Assert.Throws<SqliteException>(delegate {
                MigrateApi migrateApi = new MigrateApi(sectorDb);
                migrateApi.GetDbVersion(repository);
            });
        }
Exemplo n.º 6
0
        public void GetDbVersion()
        {
            var sectorDb = TestUtils.MakeSectorDb();
            var repository = TestUtils.MakeRepository();

            MigrateApi migrateApi = new MigrateApi(sectorDb);

            // Need to start with the schema created.
            TestUtils.CreateMigrationTable();

            using (var dbConn = TestUtils.OpenDbconnection())
            {
                using (var dbCommand = dbConn.CreateCommand())
                {
                    string templ = "INSERT INTO migrate_version (repository_id, repository_path, version) VALUES('{0}', '{1}', '{2}')";
                    dbCommand.CommandText = string.Format(templ, repository.RepositoryId, repository.RepositoryPath, 0);
                    dbCommand.ExecuteNonQuery();

                    // Verify correct behaviour.
                    Assert.AreEqual(0, migrateApi.GetDbVersion(repository));
                }

                using (var dbCommand = dbConn.CreateCommand())
                {
                    string templ = "UPDATE migrate_version SET version = 10 WHERE repository_id = '{0}'";
                    dbCommand.CommandText = string.Format(templ, repository.RepositoryId);
                    dbCommand.ExecuteNonQuery();

                    // Verify correct behaviour.
                    Assert.AreEqual(10, migrateApi.GetDbVersion(repository));
                }
            }
        }