|
| 1 | +// Copyright (C) 2024 Xtensive LLC. |
| 2 | +// This code is distributed under MIT license terms. |
| 3 | +// See the License.txt file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using NUnit.Framework; |
| 7 | +using Xtensive.Collections; |
| 8 | +using Xtensive.Orm.Upgrade; |
| 9 | +using Xtensive.Orm.Tests.Issues.IssueJira0804_RenameFieldHintValidationBugModel; |
| 10 | + |
| 11 | + |
| 12 | +namespace Xtensive.Orm.Tests.Issues.IssueJira0804_RenameFieldHintValidationBugModel |
| 13 | +{ |
| 14 | + [Serializable] |
| 15 | + [HierarchyRoot] |
| 16 | + public class Person : Entity |
| 17 | + { |
| 18 | + [Key, Field] |
| 19 | + public long Id { get; private set; } |
| 20 | + } |
| 21 | + |
| 22 | + [Serializable] |
| 23 | + [HierarchyRoot] |
| 24 | + public class Address : Entity |
| 25 | + { |
| 26 | + [Key, Field] |
| 27 | + public long Id { get; private set; } |
| 28 | + [Field] |
| 29 | + public Person Person { get; set; } |
| 30 | + [Field] |
| 31 | + public bool NewName { get; set; } |
| 32 | + } |
| 33 | + |
| 34 | + public class Upgrader : UpgradeHandler |
| 35 | + { |
| 36 | + public override bool CanUpgradeFrom(string oldVersion) => true; |
| 37 | + |
| 38 | + protected override void AddUpgradeHints(ISet<UpgradeHint> hints) |
| 39 | + { |
| 40 | + base.AddUpgradeHints(hints); |
| 41 | + _ = hints.Add( |
| 42 | + new RenameFieldHint(typeof(Address), "FieldToRename", "NewName") |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +namespace Xtensive.Orm.Tests.Issues |
| 49 | +{ |
| 50 | + [TestFixture] |
| 51 | + public sealed class IssueJira0804_RenameFieldHintValidationBug |
| 52 | + { |
| 53 | + [Test] |
| 54 | + public void MainTest() |
| 55 | + { |
| 56 | + var initConfig = DomainConfigurationFactory.Create(); |
| 57 | + initConfig.Types.Register(typeof(Person)); |
| 58 | + // I don't include this type for it to not exist in database before upgrade |
| 59 | + // this type is delcared in the hint |
| 60 | + //initConfig.Types.Register(typeof(Address)); |
| 61 | + initConfig.UpgradeMode = DomainUpgradeMode.Recreate; |
| 62 | + |
| 63 | + using (var domain = Domain.Build(initConfig)) { } |
| 64 | + |
| 65 | + var upgradeConfig = DomainConfigurationFactory.Create(); |
| 66 | + upgradeConfig.Types.Register(typeof(Person)); |
| 67 | + upgradeConfig.Types.Register(typeof(Address)); |
| 68 | + upgradeConfig.Types.Register(typeof(Upgrader)); |
| 69 | + upgradeConfig.UpgradeMode = DomainUpgradeMode.PerformSafely; |
| 70 | + |
| 71 | + _ = Assert.Throws<InvalidOperationException>(() => { |
| 72 | + using var domain = Domain.Build(upgradeConfig); |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments