Пример #1
0
 public EphemeralManager(string databaseConnectionString)
 {
     DatabaseConnectionString = databaseConnectionString;
     using (var context = new EphemeralDbContext(DatabaseConnectionString))
     {
         context.Database.Migrate();
     }
 }
Пример #2
0
        public void DeleteAllDatabases()
        {
            using (var context = new EphemeralDbContext(DatabaseConnectionString))
            {
                var databases = context.EphemeralDatabases.ToList();
                context.EphemeralDatabases.RemoveRange(context.EphemeralDatabases);
                context.SaveChanges();

                foreach (var database in databases)
                {
                    new TestDatabase(GetConnectionString(database.Id), database.Id, DatabaseConnectionString, null).Delete();
                }
            }
        }
Пример #3
0
        public TestDatabase GetTestDatabase(string schemaScript, Variant variant = Variant.Default, string dataScript = null)
        {
            var sha512 = SHA512Managed.Create();
            var bytes  = Encoding.UTF8.GetBytes(schemaScript);
            var hash   = sha512.ComputeHash(bytes);

            bool   createDatabase = false;
            string connectionString;
            int?   id = null;

            using (var context = new EphemeralDbContext(DatabaseConnectionString))
            {
                context.Database.Migrate();
                using (var transaction = context.Database.BeginTransaction())
                {
                    var database = context.EphemeralDatabases.Where(db => db.VersionHash == hash && db.Variant == variant && db.CheckedOut == null).FirstOrDefault();
                    if (database != null)
                    {
                        database.CheckedOut = DateTimeOffset.UtcNow;
                        context.SaveChanges();
                        id = database.Id;
                    }
                    transaction.Commit();
                }
                if (id == null)
                {
                    var database = context.EphemeralDatabases.Add(new EphemeralDatabase
                    {
                        VersionHash = hash,
                        Variant     = variant,
                        CheckedOut  = DateTimeOffset.UtcNow
                    });
                    context.SaveChanges();
                    createDatabase = true;
                    id             = database.Entity.Id;
                }
                connectionString = GetConnectionString(id.Value);
            }

            var testDatabase = new TestDatabase(connectionString, id.Value, DatabaseConnectionString, dataScript);

            if (createDatabase)
            {
                testDatabase.Create(schemaScript, variant);
            }
            testDatabase.ResetData();

            return(testDatabase);
        }
Пример #4
0
        public void Dispose()
        {
            if (!disposedValue)
            {
                using (var context = new EphemeralDbContext(EphemeralConnectionString))
                {
                    var database = context.EphemeralDatabases.Where(db => db.Id == EphemeralDatabaseId).FirstOrDefault();
                    if (database != null)
                    {
                        database.CheckedOut = null;
                        context.SaveChanges();
                    }
                }

                disposedValue = true;
            }
        }