protected void DeleteTables(DiscoveredDatabase database) { var tables = new RelationshipTopologicalSort(database.DiscoverTables(true)); foreach (var t in tables.Order.Reverse()) { try { t.Drop(); } catch (Exception ex) { throw new Exception($"Failed to drop table '{t.GetFullyQualifiedName()} during cleanup", ex); } } foreach (var t in database.DiscoverTableValuedFunctions()) { try { t.Drop(); } catch (Exception ex) { throw new Exception($"Failed to drop table '{t.GetFullyQualifiedName()} during cleanup", ex); } } }
/// <summary> /// Gets an empty database on the test server of the appropriate DBMS /// </summary> /// <param name="type">The DBMS you want a server of (a valid connection string must exist in TestDatabases.txt)</param> /// <param name="dbnName">null for default test database name (recommended unless you are testing moving data from one database to another on the same test server)</param> /// <param name="server"></param> /// <param name="database"></param> /// <param name="justDropTablesIfPossible">Determines behaviour when the test database already exists. False to drop and recreate it. True to just drop tables (faster)</param> /// <returns></returns> protected DiscoveredDatabase GetCleanedServer(DatabaseType type, string dbnName, out DiscoveredServer server, out DiscoveredDatabase database, bool justDropTablesIfPossible = false) { switch (type) { case DatabaseType.MicrosoftSQLServer: server = new DiscoveredServer(DiscoveredServerICanCreateRandomDatabasesAndTablesOn.Builder); break; case DatabaseType.MySql: server = _discoveredMySqlServer == null ? null : new DiscoveredServer(_discoveredMySqlServer.Builder); break; case DatabaseType.Oracle: server = _discoveredOracleServer == null ? null : new DiscoveredServer(_discoveredOracleServer.Builder); break; default: throw new ArgumentOutOfRangeException("type"); } if (server == null) { Assert.Inconclusive(); } //the microsoft one should exist! others are optional if (!server.Exists() && type != DatabaseType.MicrosoftSQLServer) { Assert.Inconclusive(); } server.TestConnection(); database = server.ExpectDatabase(dbnName); if (justDropTablesIfPossible && database.Exists()) { foreach (var t in database.DiscoverTables(true)) { t.Drop(); } foreach (var t in database.DiscoverTableValuedFunctions()) { t.Drop(); } } else { database.Create(true); } server.ChangeDatabase(dbnName); Assert.IsTrue(database.Exists()); return(database); }
protected void DeleteTables(DiscoveredDatabase database) { var syntax = database.Server.GetQuerySyntaxHelper(); if (database.Server.DatabaseType == DatabaseType.MicrosoftSQLServer) { using (var con = database.Server.GetConnection()) { con.Open(); foreach (var t in database.DiscoverTables(false)) { //disable system versioning on any temporal tables otherwise drop fails try { t.Database.Server.GetCommand( $@"IF OBJECTPROPERTY(OBJECT_ID('{syntax.EnsureWrapped(t.GetRuntimeName())}'), 'TableTemporalType') = 2 ALTER TABLE {t.GetFullyQualifiedName()} SET (SYSTEM_VERSIONING = OFF)", con).ExecuteNonQuery(); } catch (Exception) { TestContext.Out.WriteLine( $"Failed to generate disable System Versioning check for table {t} (nevermind)"); } } } } var tables = new RelationshipTopologicalSort(database.DiscoverTables(true)); foreach (var t in tables.Order.Reverse()) { try { t.Drop(); } catch (Exception ex) { throw new Exception($"Failed to drop table '{t.GetFullyQualifiedName()} during cleanup", ex); } } foreach (var t in database.DiscoverTableValuedFunctions()) { try { t.Drop(); } catch (Exception ex) { throw new Exception($"Failed to drop table '{t.GetFullyQualifiedName()} during cleanup", ex); } } }