public void Run_all_PostgreSQL_integration_tests_work() { // Arrange var cnn = _dbContainer.CreateDbConnection(); var wcnn = new WrappedConnection(cnn).AssertDatabaseServerType(DBMS.PostgreSQL); var db = DatabaseHelperFactory.GetDatabaseHelper(DBMS.PostgreSQL, wcnn); string schemaName = "My metadata schema"; var schema = new PostgreSQLSchema(schemaName, wcnn); // Assert schema.AssertIsNotExists(); schema.AssertCreation(); schema.AssertExists(); schema.AssertIsEmpty(); db.AssertDefaultSchemaName("public") .AssertApplicationLock(_dbContainer.CreateDbConnection()) .AssertMetadataTableCreation(schemaName, "changelog") .AssertMetadataTableLock() .AssertSchemaIsDroppableWhenNewSchemaFound(schemaName) // id:1 .AssertVersionedMigrationSave() // id:2 .AssertVersionedMigrationChecksumUpdate() .AssertRepeatableMigrationSave(); // id:3 schema.AssertIsNotEmpty(); schema.Erase(); schema.AssertIsEmpty(); schema.Drop(); schema.AssertIsNotExists(); db.AssertCloseConnection(); }
public void When_disposed_inner_dbconnection_is_closed() { var cnn = _pgContainer.CreateDbConnection(); using (var wrappedConnection = new WrappedConnection(cnn)) { wrappedConnection.Open(); } Assert.True(cnn.State == ConnectionState.Closed); }
public void Run_all_PostgreSQL_migrations_work() { // Arrange string[] locations = AppVeyor ? new[] { PostgreSQL.MigrationFolder } : new[] { PostgreSQL.MigrationFolder, PostgreSQL.Migration11Folder }; // Add specific PostgreSQL 11 scripts int expectedNbMigration = AppVeyor ? Directory.GetFiles(PostgreSQL.MigrationFolder).Length : Directory.GetFiles(PostgreSQL.MigrationFolder).Length + 1; var cnn = _dbContainer.CreateDbConnection(); var evolve = new Evolve(cnn, msg => _output.WriteLine(msg)) { Schemas = new[] { "public", "unittest" }, MetadataTableSchema = "unittest", Placeholders = new Dictionary <string, string> { ["${schema1}"] = "unittest" } }; // Assert evolve.AssertInfoIsSuccessful(cnn, expectedNbRows: 0) .AssertMigrateIsSuccessful(cnn, expectedNbMigration, null, locations) .AssertMigrateThrows <EvolveValidationException>(cnn, locations: PostgreSQL.ChecksumMismatchFolder) .AssertRepairIsSuccessful(cnn, expectedNbReparation: 1) .AssertMigrateIsSuccessful(cnn, expectedNbMigration: 0) .AssertMigrateThrows <EvolveValidationException>(cnn, locations: PostgreSQL.OutOfOrderFolder) .AssertMigrateIsSuccessful(cnn, expectedNbMigration: 1, e => e.OutOfOrder = true) .AssertEraseThrows <EvolveConfigurationException>(cnn, e => e.IsEraseDisabled = true) .AssertEraseIsSuccessful(cnn, e => e.IsEraseDisabled = false) .AssertMigrateIsSuccessful(cnn, expectedNbMigration, null, locations) .AssertMigrateIsSuccessful(cnn, expectedNbMigration: 1, e => e.MustEraseOnValidationError = true, locations: PostgreSQL.ChecksumMismatchFolder) .AssertEraseIsSuccessful(cnn, e => e.IsEraseDisabled = false) .AssertMigrateIsSuccessful(cnn, expectedNbMigration, null, locations) .AssertRepairIsSuccessful(cnn, expectedNbReparation: 0, locations: PostgreSQL.RepeatableFolder) .AssertMigrateIsSuccessful(cnn, expectedNbMigration: 1) .AssertMigrateIsSuccessful(cnn, expectedNbMigration: 0) .AssertInfoIsSuccessful(cnn, expectedNbRows: expectedNbMigration + 3); }
public void Run_all_PostgreSQL_migrations_work() { var cnn = _pgContainer.CreateDbConnection(); var evolve = new Evolve(cnn, msg => _output.WriteLine(msg)) { Locations = new List <string> { TestContext.PostgreSQL.MigrationFolder }, Schemas = new List <string> { "public", "unittest" }, MetadataTableSchema = "unittest", Placeholders = new Dictionary <string, string> { ["${schema1}"] = "unittest" } }; int nbMigration = Directory.GetFiles(TestContext.PostgreSQL.MigrationFolder).Length; if (!TestContext.AppVeyor) { // Add specific PostgreSQL 11 scripts evolve.Locations = new List <string> { TestContext.PostgreSQL.MigrationFolder, TestContext.PostgreSQL.Migration11Folder }; nbMigration = nbMigration + 1; } // Migrate Sql_Scripts\Migration evolve.Migrate(); Assert.True(evolve.NbMigration == nbMigration, $"{nbMigration} migrations should have been applied, not {evolve.NbMigration}."); Assert.True(cnn.State == ConnectionState.Closed); // Migrate: nothing to do. Database is already up to date. evolve.Migrate(); Assert.True(evolve.NbMigration == 0, $"There should be no more migration after a successful one, not {evolve.NbMigration}."); Assert.True(cnn.State == ConnectionState.Closed); // Migrate Sql_Scripts\Checksum_mismatch: validation should fail due to a checksum mismatch. evolve.Locations = new List <string> { TestContext.PostgreSQL.ChecksumMismatchFolder }; Assert.Throws <EvolveValidationException>(() => evolve.Migrate()); Assert.True(cnn.State == ConnectionState.Closed); // Repair sucessfull evolve.Repair(); Assert.True(evolve.NbReparation == 1, $"There should be 1 migration repaired, not {evolve.NbReparation}."); Assert.True(cnn.State == ConnectionState.Closed); // Migrate: nothing to do. Database is already up to date. evolve.Migrate(); Assert.True(evolve.NbMigration == 0, $"There should be no more migration after a successful one, not {evolve.NbMigration}."); Assert.True(cnn.State == ConnectionState.Closed); // Migrate Sql_Scripts\OutOfOrder: validation should fail due to an unordered migration (OutOfOrder = false). evolve.Locations = new List <string> { TestContext.PostgreSQL.OutOfOrderFolder }; Assert.Throws <EvolveValidationException>(() => evolve.Migrate()); Assert.True(cnn.State == ConnectionState.Closed); // Migrate sucessfull: (OutOfOrder = true). evolve.OutOfOrder = true; evolve.Migrate(); Assert.True(evolve.NbMigration == 1, $"1 migration should have been applied, not {evolve.NbMigration}."); Assert.True(cnn.State == ConnectionState.Closed); // Erase cancelled (EraseDisabled = true) evolve.IsEraseDisabled = true; Assert.Throws <EvolveConfigurationException>(() => evolve.Erase()); Assert.True(cnn.State == ConnectionState.Closed); // Erase sucessfull evolve.IsEraseDisabled = false; evolve.Erase(); Assert.True(evolve.NbSchemaErased == evolve.Schemas.Count(), $"{evolve.Schemas.Count()} schemas should have been erased, not {evolve.NbSchemaErased}."); Assert.True(cnn.State == ConnectionState.Closed); // Migrate sucessfull after a validation error (MustEraseOnValidationError = true) evolve.Locations = new List <string> { TestContext.PostgreSQL.MigrationFolder }; // Migrate Sql_Scripts\Migration nbMigration = Directory.GetFiles(TestContext.PostgreSQL.MigrationFolder).Length; evolve.Migrate(); Assert.True(evolve.NbMigration == nbMigration, $"{nbMigration} migrations should have been applied, not {evolve.NbMigration}."); evolve.Locations = new List <string> { TestContext.PostgreSQL.ChecksumMismatchFolder }; // Migrate Sql_Scripts\Checksum_mismatch evolve.MustEraseOnValidationError = true; evolve.Migrate(); Assert.True(evolve.NbSchemaErased == evolve.Schemas.Count(), $"{evolve.Schemas.Count()} schemas should have been erased, not {evolve.NbSchemaErased}."); Assert.True(evolve.NbMigration == 1, $"1 migration should have been applied, not {evolve.NbMigration}."); Assert.True(cnn.State == ConnectionState.Closed); }
public void Run_all_PostgreSQL_integration_tests_work() { // Open a connection to the PostgreSQL database var cnn = _pgContainer.CreateDbConnection(); cnn.Open(); Assert.True(cnn.State == ConnectionState.Open, "Cannot open a connection to the database."); // Initiate a connection to the database var wcnn = new WrappedConnection(cnn); // Validate DBMS.PostgreSQL Assert.Equal(DBMS.PostgreSQL, wcnn.GetDatabaseServerType()); // Init the DatabaseHelper DatabaseHelper db = DatabaseHelperFactory.GetDatabaseHelper(DBMS.PostgreSQL, wcnn); // Test default schema name Assert.True(db.GetCurrentSchemaName() == "public", "The default PostgreSQL schema should be 'public'."); // Create schema string metadataSchemaName = "My metadata schema"; Schema metadataSchema = new PostgreSQLSchema(metadataSchemaName, wcnn); Assert.False(metadataSchema.IsExists(), $"The schema [{metadataSchemaName}] should not already exist."); Assert.True(metadataSchema.Create(), $"Creation of the schema [{metadataSchemaName}] failed."); Assert.True(metadataSchema.IsExists(), $"The schema [{metadataSchemaName}] should be created."); Assert.True(metadataSchema.IsEmpty(), $"The schema [{metadataSchemaName}] should be empty."); // Get MetadataTable string metadataTableName = "changelog"; var metadata = db.GetMetadataTable(metadataSchemaName, metadataTableName); // Create MetadataTable Assert.False(metadata.IsExists(), "MetadataTable sould not already exist."); Assert.True(metadata.CreateIfNotExists(), "MetadataTable creation failed."); Assert.True(metadata.IsExists(), "MetadataTable sould exist."); Assert.False(metadata.CreateIfNotExists(), "MetadataTable already exists. Creation should return false."); Assert.True(metadata.GetAllMigrationMetadata().Count() == 0, "No migration metadata should be found."); // TryLock/ReleaseLock MetadataTable Assert.True(metadata.TryLock()); Assert.True(metadata.ReleaseLock()); // Save NewSchema metadata metadata.Save(MetadataType.NewSchema, "0", "New schema created.", metadataSchemaName); Assert.True(metadata.CanDropSchema(metadataSchemaName), $"[{metadataSchemaName}] should be droppable."); Assert.False(metadata.CanEraseSchema(metadataSchemaName), $"[{metadataSchemaName}] should not be erasable."); // Add metadata migration var migrationScript = new FileMigrationScript(TestContext.PostgreSQL.EmptyMigrationScriptPath, "1_3_2", "Migration_description"); metadata.SaveMigration(migrationScript, true); var migrationMetadata = metadata.GetAllMigrationMetadata().FirstOrDefault(); Assert.True(migrationMetadata != null, "One migration metadata should be found."); Assert.True(migrationMetadata.Version == migrationScript.Version, "Metadata version is not the same."); Assert.True(migrationMetadata.Checksum == migrationScript.CalculateChecksum(), "Metadata checksum is not the same."); Assert.True(migrationMetadata.Description == migrationScript.Description, "Metadata descritpion is not the same."); Assert.True(migrationMetadata.Name == migrationScript.Name, "Metadata name is not the same."); Assert.True(migrationMetadata.Success == true, "Metadata success is not true."); Assert.True(migrationMetadata.Id > 0, "Metadata id is not set."); Assert.True(migrationMetadata.InstalledOn.Date == DateTime.UtcNow.Date, $"Metadata InstalledOn date {migrationMetadata.InstalledOn} must be equals to {DateTime.UtcNow.Date}."); // Update checksum metadata.UpdateChecksum(migrationMetadata.Id, "Hi !"); Assert.Equal("Hi !", metadata.GetAllMigrationMetadata().First().Checksum); // Assert metadata schema is not empty Assert.False(metadataSchema.IsEmpty(), $"[{metadataSchemaName}] should not be empty."); // Erase schema metadataSchema.Erase(); Assert.True(metadataSchema.IsEmpty(), $"The schema [{metadataSchemaName}] should be empty."); Assert.True(metadataSchema.IsExists(), $"The schema [{metadataSchemaName}] should exist."); // Drop schema metadataSchema.Drop(); Assert.False(metadataSchema.IsExists(), $"The schema [{metadataSchemaName}] should not exist."); // Acquisition du lock applicatif while (true) { if (db.TryAcquireApplicationLock()) { break; } Thread.Sleep(TimeSpan.FromSeconds(1)); } Assert.True(db.TryAcquireApplicationLock(), "Cannot acquire application lock."); // Can not acquire lock while it is taken by another connection var cnn2 = _pgContainer.CreateDbConnection(); var wcnn2 = new WrappedConnection(cnn2); var db2 = DatabaseHelperFactory.GetDatabaseHelper(DBMS.PostgreSQL, wcnn2); Assert.False(db2.TryAcquireApplicationLock(), "Application lock could not have been acquired."); // Release the lock db.ReleaseApplicationLock(); db.CloseConnection(); Assert.True(db.WrappedConnection.DbConnection.State == ConnectionState.Closed, "SQL connection should be closed."); }