Beispiel #1
0
        public async Task CreateDb_WithEncoding_Ok()
        {
            IDbProvider dbProvider = null;
            var         random     = new Random();
            var         dbName     = $"temp_{random.Next(100)}";

            try
            {
                var options = new PostgreDbProviderOptions(
                    String.Format(ConfigProvider.GetConfig().ConnectionStringMask, dbName),
                    template: "template0",
                    databaseEncoding: "SQL_ASCII");
                dbProvider = new PostgreDbProvider(options);
                await dbProvider.CreateDatabaseIfNotExistsAsync();
            }
            finally
            {
                if (dbProvider != null)
                {
                    try
                    {
                        await dbProvider.OpenConnectionAsync();

                        await dbProvider.ExecuteScriptAsync($"DROP TABLE IF EXISTS {dbName}");

                        await dbProvider.CloseConnectionAsync();
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
            }
        }
        public async Task Migrate_AllScriptOk_NoRollback()
        {
            var config           = ConfigProvider.GetConfig();
            var connectionString = String.Format(config.ConnectionStringMask, "test_ok");


            var builder = new MigratorBuilder();

            builder.UseCodeMigrations().FromAssembly(Assembly.GetExecutingAssembly());
            builder.UseScriptMigrations().FromDirectory(Path.Combine(Directory.GetCurrentDirectory(), "ScriptMigrations"));
            builder.UsePostgreSQL(connectionString);

            builder.UseUpgradeMigrationPolicy(MigrationPolicy.All);
            builder.UseDowngradeMigrationPolicy(MigrationPolicy.All);
            builder.SetUpTargetVersion(new DbVersion(3, 0));

            var migrator = builder.Build();

            await migrator.MigrateAsync();

            var migrationProvider = new PostgreDbProvider(new PostgreDbProviderOptions(connectionString));
            await migrationProvider.OpenConnectionAsync();

            var currentDbVersion = await migrationProvider.GetDbVersionSafeAsync();

            await migrationProvider.CloseConnectionAsync();

            Assert.Equal(new DbVersion(3, 0), currentDbVersion);
        }
        public PostgreSqlProviderDbTestFixture()
        {
            var random = new Random();

            DbName  = $"temp_{random.Next(100)}";
            Options = new PostgreDbProviderOptions(
                String.Format(ConfigProvider.GetConfig().ConnectionStringMask, DbName),
                lcCollate: "C",
                lcCtype: "C",
                template: "template0",
                databaseEncoding: "SQL_ASCII");
            DbProvider = new PostgreDbProvider(Options);
        }
        public async Task Migrate_AllScriptOk_Rollback()
        {
            var config           = ConfigProvider.GetConfig();
            var connectionString = String.Format(config.ConnectionStringMask, "test_rollback");


            var builder = new MigratorBuilder();

            builder.UseCodeMigrations().FromAssembly(Assembly.GetExecutingAssembly());
            builder.UseScriptMigrations().FromDirectory(Path.Combine(Directory.GetCurrentDirectory(), "ScriptMigrations"));
            builder.UsePostgreSQL(connectionString);

            builder.UseUpgradeMigrationPolicy(MigrationPolicy.Allowed);
            builder.UseDowngradeMigrationPolicy(MigrationPolicy.Allowed);
            builder.SetUpTargetVersion(new DbVersion(6, 0));

            var migrator = builder.Build();

            try
            {
                await migrator.MigrateAsync();

                // last migration is incorrect, can not go here
                Assert.False(true);
            }
            catch
            {
                // ignored
            }

            var migrationProvider = new PostgreDbProvider(new PostgreDbProviderOptions(connectionString));
            await migrationProvider.OpenConnectionAsync();

            var actualAppliedMigrations = await migrationProvider.GetAppliedMigrationVersionAsync();

            await migrationProvider.CloseConnectionAsync();

            var expectedAppliedMigrations = new HashSet <DbVersion>
            {
                new DbVersion(1, 0),
                new DbVersion(2, 0),
                new DbVersion(3, 0),
                new DbVersion(4, 0),
                new DbVersion(5, 0)
            };

            Assert.Equal(expectedAppliedMigrations, actualAppliedMigrations);
        }
        public async Task Migrate_AllScriptOk_SwitchedOffTransaction()
        {
            var config           = ConfigProvider.GetConfig();
            var connectionString = String.Format(config.ConnectionStringMask, "test_without_transactions");


            var builder = new MigratorBuilder();

            builder.UseCodeMigrations().FromAssembly(Assembly.GetExecutingAssembly());
            builder.UseScriptMigrations().FromDirectory(Path.Combine(Directory.GetCurrentDirectory(), "ScriptMigrations"));
            builder.UsePostgreSQL(connectionString);

            builder.UseUpgradeMigrationPolicy(MigrationPolicy.Allowed);
            builder.UseDowngradeMigrationPolicy(MigrationPolicy.Allowed);
            builder.SetUpTargetVersion(new DbVersion(5, 0));

            var migrator = builder.Build();

            await migrator.MigrateAsync();

            var migrationProvider = new PostgreDbProvider(new PostgreDbProviderOptions(connectionString));
            await migrationProvider.OpenConnectionAsync();

            var actualAppliedMigrations = await migrationProvider.GetAppliedMigrationVersionAsync();

            await migrationProvider.CloseConnectionAsync();

            var expectedAppliedMigrations = new HashSet <DbVersion>
            {
                new DbVersion(1, 0),
                new DbVersion(2, 0),
                new DbVersion(3, 0),
                new DbVersion(4, 0),
                new DbVersion(5, 0)
            };

            Assert.Equal(expectedAppliedMigrations, actualAppliedMigrations);
        }