|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Data.Entity.Infrastructure; |
| 6 | +using System.Data.Entity.Migrations; |
| 7 | +using System.Data.Entity.Migrations.Design; |
| 8 | +using System.Data.SqlClient; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Xunit; |
| 12 | +using Xunit.Abstractions; |
| 13 | + |
| 14 | +namespace NuGet.Services.Validation.Tests |
| 15 | +{ |
| 16 | + public class PendingMigrationsFacts : IAsyncLifetime |
| 17 | + { |
| 18 | + private string _dbName; |
| 19 | + private readonly ITestOutputHelper _output; |
| 20 | + |
| 21 | + public PendingMigrationsFacts(ITestOutputHelper output) |
| 22 | + { |
| 23 | + _output = output; |
| 24 | + } |
| 25 | + |
| 26 | + public Task InitializeAsync() |
| 27 | + { |
| 28 | + return Task.CompletedTask; |
| 29 | + } |
| 30 | + |
| 31 | + public async Task DisposeAsync() |
| 32 | + { |
| 33 | + if (_dbName == null) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const string connectionString = @"Data Source=(localdb)\mssqllocaldb; Initial Catalog=master; Integrated Security=True; MultipleActiveResultSets=True"; |
| 39 | + using (var sqlConnection = new SqlConnection(connectionString)) |
| 40 | + { |
| 41 | + await sqlConnection.OpenAsync(); |
| 42 | + using (var sqlCommand = sqlConnection.CreateCommand()) |
| 43 | + { |
| 44 | + sqlCommand.CommandText = $"ALTER DATABASE {_dbName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE {_dbName};"; |
| 45 | + await sqlCommand.ExecuteNonQueryAsync(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void NoPendingMigrations() |
| 52 | + { |
| 53 | + var currentTimestamp = DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmssFFFFFFF"); |
| 54 | + _dbName = $"PendingMigrationsTest{currentTimestamp}Validation"; |
| 55 | + var connectionString = $@"Data Source=(localdb)\mssqllocaldb; Initial Catalog={_dbName}; Integrated Security=True; MultipleActiveResultSets=True"; |
| 56 | + |
| 57 | + var migrationsConfiguration = new ValidationMigrationsConfiguration |
| 58 | + { |
| 59 | + TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient"), |
| 60 | + }; |
| 61 | + |
| 62 | + var dbMigrator = new DbMigrator(migrationsConfiguration); |
| 63 | + var migrations = dbMigrator.GetLocalMigrations(); |
| 64 | + dbMigrator.Update(migrations.Last()); |
| 65 | + |
| 66 | + var migrationScaffolder = new MigrationScaffolder(dbMigrator.Configuration); |
| 67 | + |
| 68 | + var migrationName = $"TestMigration{currentTimestamp}"; |
| 69 | + var result = migrationScaffolder.Scaffold(migrationName); |
| 70 | + |
| 71 | + _output.WriteLine("Migration content:"); |
| 72 | + _output.WriteLine(new string('-', 60)); |
| 73 | + _output.WriteLine(result.UserCode); |
| 74 | + _output.WriteLine(new string('-', 60)); |
| 75 | + |
| 76 | + Assert.Equal( |
| 77 | + $@"namespace {dbMigrator.Configuration.MigrationsNamespace} |
| 78 | +{{ |
| 79 | + using System; |
| 80 | + using System.Data.Entity.Migrations; |
| 81 | + |
| 82 | + public partial class {migrationName} : DbMigration |
| 83 | + {{ |
| 84 | + public override void Up() |
| 85 | + {{ |
| 86 | + }} |
| 87 | + |
| 88 | + public override void Down() |
| 89 | + {{ |
| 90 | + }} |
| 91 | + }} |
| 92 | +}} |
| 93 | +", result.UserCode); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments