public SqlServerFixture() : base(new SqlConnection(CreateConnectionString()), new SqlServerDialect()) { var runner = new SqlServerMigrationRunner(Connection.ConnectionString); runner.CreateDatabaseIfNotExists(); runner.MigrateUp(typeof(TMigrationInfo).Assembly, typeof(TMigrationInfo).Namespace); }
private static void MigrateToLatest(string connectionString, RuntimeOptions options) { var runner = new SqlServerMigrationRunner(connectionString); if (options.CreateIfNotExists) { runner.CreateDatabaseIfNotExists(); } if (options.MigrateOnStartup) { runner.MigrateUp(typeof(CreateIdentitySchema).Assembly, typeof(CreateIdentitySchema).Namespace); } }
protected override void Dispose(bool disposing) { var connectionString = ConnectionString; var database = Database; base.Dispose(disposing); var builder = new SqlConnectionStringBuilder(connectionString) { InitialCatalog = "master" }; using (var db = new SqlConnection(builder.ConnectionString)) { db.Open(); try { db.Execute($"ALTER DATABASE [{database}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"); db.Execute($"DROP DATABASE [{database}]"); } catch (SqlException e) { Trace.TraceError(e.ToString()); } } switch (Connection) { case SqlConnection connection when connection.DataSource != null: try { File.Delete(SqlServerMigrationRunner.DatabaseFileName(database)); } catch (Exception e) { Trace.TraceError(e.ToString()); } break; } }
private static string CreateConnectionString() { var database = $"{Guid.NewGuid().ToString("N").ToUpperInvariant()}"; var builder = new SqlConnectionStringBuilder( "Data Source=(localdb)\\MSSQLLocalDB;Integrated Security=true;MultipleActiveResultSets=true") { InitialCatalog = "master" }; using (var connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); var fileName = SqlServerMigrationRunner.DatabaseFileName(database); connection.Execute($"CREATE DATABASE [{database}] ON (NAME = N'{database}', FILENAME = '{fileName}')"); connection.Execute($"ALTER DATABASE [{database}] SET READ_COMMITTED_SNAPSHOT ON;"); connection.Execute($"ALTER DATABASE [{database}] SET ALLOW_SNAPSHOT_ISOLATION ON;"); } builder.InitialCatalog = database; return(builder.ConnectionString); }