public void Run_all_MySQL_integration_tests_work() { // Open a connection to the PostgreSQL database var cnn = new MySqlConnection($"Server=127.0.0.1;Port={_fixture.MySql.HostPort};Database={_fixture.MySql.DbName};Uid={_fixture.MySql.DbUser};Pwd={_fixture.MySql.DbPwd};"); 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.MySQL_MariaDB Assert.Equal(DBMS.MySQL_MariaDB, wcnn.GetDatabaseServerType()); // Init the DatabaseHelper DatabaseHelper db = DatabaseHelperFactory.GetDatabaseHelper(DBMS.MySQL_MariaDB, wcnn); // Get default schema name Assert.True(db.GetCurrentSchemaName() == _fixture.MySql.DbName, $"The default MySQL schema should be '{_fixture.MySql.DbName}'."); // Create schema string metadataSchemaName = "My metadata schema"; Schema metadataSchema = new MySQLSchema(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 = "change log"; 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."); // Lock MetadataTable metadata.Lock(); // 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 MigrationScript(TestContext.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.Now.Date, "Installed date is not set."); // 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."); }
public void Run_all_SQLServer_integration_tests_work() { // Open a connection to the SQLServer database var cnn = new SqlConnection($"Server=127.0.0.1;Database={DbName};User Id={_sqlServerFixture.DbUser};Password={_sqlServerFixture.DbPwd};"); 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.SQLServer Assert.Equal(DBMS.SQLServer, wcnn.GetDatabaseServerType()); // Init the DatabaseHelper DatabaseHelper db = DatabaseHelperFactory.GetDatabaseHelper(DBMS.SQLServer, wcnn); // Get default schema name string metadataSchemaName = db.GetCurrentSchemaName(); Assert.True(metadataSchemaName == "dbo", "The default SQLServer schema should be 'dbo'."); // 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 EmptySchema metadata metadata.Save(MetadataType.EmptySchema, "0", "Empty schema found.", metadataSchemaName); Assert.False(metadata.CanDropSchema(metadataSchemaName), $"[{metadataSchemaName}] should not be droppable."); Assert.True(metadata.CanEraseSchema(metadataSchemaName), $"[{metadataSchemaName}] should be erasable."); // Add metadata migration var migrationScript = new FileMigrationScript(TestContext.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, "Installed date is not set."); // Update checksum metadata.UpdateChecksum(migrationMetadata.Id, "Hi !"); Assert.Equal("Hi !", metadata.GetAllMigrationMetadata().First().Checksum); // Assert metadata schema is not empty Schema metadataSchema = new SQLServerSchema(metadataSchemaName, wcnn); 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."); // 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 = new SqlConnection($"Server=127.0.0.1;Database={DbName};User Id={_sqlServerFixture.DbUser};Password={_sqlServerFixture.DbPwd};"); var wcnn2 = new WrappedConnection(cnn2); var db2 = DatabaseHelperFactory.GetDatabaseHelper(DBMS.SQLServer, 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."); }
private static void DumpPlanInternal <T>(IQueryable <T> queryable, bool dumpData, bool addNewPanel) { if (Util.CurrentDataContext != null && !(Util.CurrentDataContext.Connection is SqlConnection)) { ShowError("Query Plan Visualizer supports only Sql Server"); return; } var databaseHelper = DatabaseHelperFactory.Create(Util.CurrentDataContext, queryable); if (dumpData) { queryable.Dump(); } try { var planXml = databaseHelper.GetSqlServerQueryExecutionPlan(queryable); if (string.IsNullOrEmpty(planXml)) { ShowError("Cannot retrieve query plan"); return; } var queryPlanProcessor = new QueryPlanProcessor(planXml); var indexes = queryPlanProcessor.GetMissingIndexes(); var planHtml = queryPlanProcessor.ConvertPlanToHtml(); var files = ExtractFiles(); files.Add(planHtml); var html = string.Format(Resources.template, files.ToArray()); var control = PanelManager.GetOutputPanel(ExecutionPlanPanelTitle)?.GetControl() as QueryPlanUserControl; if (control == null || addNewPanel) { control = new QueryPlanUserControl() { DatabaseHelper = databaseHelper }; control.IndexCreated += (sender, args) => { if (MessageBox.Show("Index created. Refresh query plan?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DumpPlanInternal(queryable, false, false); } }; PanelManager.DisplayControl(control, ExecutionPlanPanelTitle); } control.DisplayExecutionPlanDetails(planXml, html, indexes); } catch (Exception exception) { ShowError(exception.ToString()); } }