|
| 1 | +// Copyright (C) 2021 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.Orm.Configuration; |
| 8 | +using Xtensive.Orm.Services; |
| 9 | + |
| 10 | +namespace Xtensive.Orm.Tests.Upgrade |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class UpgradeSchemaWithNonDOSequences |
| 14 | + { |
| 15 | + public class DummyEntity : Entity |
| 16 | + { |
| 17 | + [Field, Key] |
| 18 | + public int Id { get; private set; } |
| 19 | + } |
| 20 | + |
| 21 | + [OneTimeSetUp] |
| 22 | + public void Setup() |
| 23 | + { |
| 24 | + Require.ProviderIs(StorageProvider.SqlServer); |
| 25 | + Require.AllFeaturesSupported(Providers.ProviderFeatures.Sequences); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + [TestCase(typeof(byte), DomainUpgradeMode.Recreate, TestName = "TinyIntRecreateTest")] |
| 30 | + [TestCase(typeof(byte), DomainUpgradeMode.Perform, TestName = "TinyIntPerformTest")] |
| 31 | + [TestCase(typeof(byte), DomainUpgradeMode.PerformSafely, TestName = "TinyIntPerformSafelyTest")] |
| 32 | + [TestCase(typeof(byte), DomainUpgradeMode.Validate, TestName = "TinyIntValidateTest")] |
| 33 | + [TestCase(typeof(byte), DomainUpgradeMode.Skip, TestName = "TinyIntSkipTest")] |
| 34 | + [TestCase(typeof(byte), DomainUpgradeMode.LegacyValidate, TestName = "TinyIntLegacyValidateTest")] |
| 35 | + [TestCase(typeof(byte), DomainUpgradeMode.LegacySkip, TestName = "TinyIntLegacySkipTest")] |
| 36 | + |
| 37 | + [TestCase(typeof(short), DomainUpgradeMode.Recreate, TestName = "SmallIntRecreateTest")] |
| 38 | + [TestCase(typeof(short), DomainUpgradeMode.Perform, TestName = "SmallIntPerformTest")] |
| 39 | + [TestCase(typeof(short), DomainUpgradeMode.PerformSafely, TestName = "SmallIntPerformSafelyTest")] |
| 40 | + [TestCase(typeof(short), DomainUpgradeMode.Validate, TestName = "SmallIntValidateTest")] |
| 41 | + [TestCase(typeof(short), DomainUpgradeMode.Skip, TestName = "SmallIntSkipTest")] |
| 42 | + [TestCase(typeof(short), DomainUpgradeMode.LegacyValidate, TestName = "SmallIntLegacyValidateTest")] |
| 43 | + [TestCase(typeof(short), DomainUpgradeMode.LegacySkip, TestName = "SmallIntLegacySkipTest")] |
| 44 | + |
| 45 | + [TestCase(typeof(int), DomainUpgradeMode.Recreate, TestName = "IntRecreateTest")] |
| 46 | + [TestCase(typeof(int), DomainUpgradeMode.Perform, TestName = "IntPerformTest")] |
| 47 | + [TestCase(typeof(int), DomainUpgradeMode.PerformSafely, TestName = "IntPerformSafelyTest")] |
| 48 | + [TestCase(typeof(int), DomainUpgradeMode.Validate, TestName = "IntValidateTest")] |
| 49 | + [TestCase(typeof(int), DomainUpgradeMode.Skip, TestName = "IntSkipTest")] |
| 50 | + [TestCase(typeof(int), DomainUpgradeMode.LegacyValidate, TestName = "IntLegacyValidateTest")] |
| 51 | + [TestCase(typeof(int), DomainUpgradeMode.LegacySkip, TestName = "IntLegacySkipTest")] |
| 52 | + |
| 53 | + [TestCase(typeof(long), DomainUpgradeMode.Recreate, TestName = "BigIntRecreateTest")] |
| 54 | + [TestCase(typeof(long), DomainUpgradeMode.Perform, TestName = "BigIntPerformTest")] |
| 55 | + [TestCase(typeof(long), DomainUpgradeMode.PerformSafely, TestName = "BigIntPerformSafelyTest")] |
| 56 | + [TestCase(typeof(long), DomainUpgradeMode.Validate, TestName = "BigIntValidateTest")] |
| 57 | + [TestCase(typeof(long), DomainUpgradeMode.Skip, TestName = "BigIntSkipTest")] |
| 58 | + [TestCase(typeof(long), DomainUpgradeMode.LegacyValidate, TestName = "BigIntLegacyValidateTest")] |
| 59 | + [TestCase(typeof(long), DomainUpgradeMode.LegacySkip, TestName = "BigIntLegacySkipTest")] |
| 60 | + public void MainTest(Type seqBaseType, DomainUpgradeMode upgradeMode) |
| 61 | + { |
| 62 | + var initConfig = GetDomainConfig(DomainUpgradeMode.Recreate); |
| 63 | + using (var initialDomain = Domain.Build(initConfig)) |
| 64 | + using (var session = initialDomain.OpenSession()) |
| 65 | + using (var tx = session.OpenTransaction()) { |
| 66 | + var service = session.Services.Get<DirectSqlAccessor>(); |
| 67 | + var command = service.CreateCommand(); |
| 68 | + command.CommandText = GetSequenceCreatorQuery(seqBaseType); |
| 69 | + using (command) { |
| 70 | + _ = command.ExecuteNonQuery(); |
| 71 | + } |
| 72 | + tx.Complete(); |
| 73 | + } |
| 74 | + |
| 75 | + var upgradeConfig = GetDomainConfig(upgradeMode); |
| 76 | + using ( var initialDomain = Domain.Build(upgradeConfig)) |
| 77 | + using (var session = initialDomain.OpenSession()) |
| 78 | + using (var tx = session.OpenTransaction()) { |
| 79 | + var service = session.Services.Get<DirectSqlAccessor>(); |
| 80 | + var command = service.CreateCommand(); |
| 81 | + command.CommandText = GetSequenceValidatorQuery(seqBaseType, upgradeMode); |
| 82 | + using (command) { |
| 83 | + Assert.IsTrue((bool)command.ExecuteScalar()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private DomainConfiguration GetDomainConfig(DomainUpgradeMode upgradeMode) |
| 89 | + { |
| 90 | + var config = DomainConfigurationFactory.Create(); |
| 91 | + config.Types.Register(typeof (DummyEntity)); |
| 92 | + config.UpgradeMode = upgradeMode; |
| 93 | + return config; |
| 94 | + } |
| 95 | + |
| 96 | + private string GetSequenceCreatorQuery(Type baseType) |
| 97 | + { |
| 98 | + var sequenceName = GetSequenceName(baseType); |
| 99 | + if (baseType == typeof(byte)) { |
| 100 | + return $"CREATE SEQUENCE dbo.{sequenceName} AS tinyint START WITH 12 INCREMENT BY 3 MINVALUE 10 MAXVALUE 200 CYCLE CACHE 3;"; |
| 101 | + } |
| 102 | + else if(baseType == typeof(short)) { |
| 103 | + return $"CREATE SEQUENCE dbo.{sequenceName} AS smallint START WITH 12 INCREMENT BY 3 MINVALUE 10 MAXVALUE 200 CYCLE CACHE 3;"; |
| 104 | + } |
| 105 | + else if (baseType == typeof(int)) { |
| 106 | + return $"CREATE SEQUENCE dbo.{sequenceName} AS int START WITH 12 INCREMENT BY 3 MINVALUE 10 MAXVALUE 200 CYCLE CACHE 3;"; |
| 107 | + } |
| 108 | + else if(baseType == typeof(long)) { |
| 109 | + return $"CREATE SEQUENCE dbo.{sequenceName} AS bigint START WITH 12 INCREMENT BY 3 MINVALUE 10 MAXVALUE 200 CYCLE CACHE 3;"; |
| 110 | + } |
| 111 | + throw new ArgumentOutOfRangeException(); |
| 112 | + } |
| 113 | + |
| 114 | + private string GetSequenceValidatorQuery(Type baseType, DomainUpgradeMode upgradeMode) |
| 115 | + { |
| 116 | + var sequenceName = GetSequenceName(baseType); |
| 117 | + if (upgradeMode == DomainUpgradeMode.Recreate) { |
| 118 | + // no sequence |
| 119 | + return $"SELECT CASE WHEN Count([name]) = 0 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 120 | + } |
| 121 | + else if (upgradeMode == DomainUpgradeMode.Perform) { |
| 122 | + // no sequence |
| 123 | + return $"SELECT CASE WHEN Count([name]) = 0 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 124 | + } |
| 125 | + else if (upgradeMode == DomainUpgradeMode.PerformSafely) { |
| 126 | + // no sequence |
| 127 | + return $"SELECT CASE WHEN Count([name]) = 0 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 128 | + } |
| 129 | + else if (upgradeMode == DomainUpgradeMode.Skip) { |
| 130 | + // sequence |
| 131 | + return $"SELECT CASE WHEN Count([name]) = 1 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 132 | + } |
| 133 | + else if (upgradeMode == DomainUpgradeMode.Validate) { |
| 134 | + // sequence |
| 135 | + return $"SELECT CASE WHEN Count([name]) = 1 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 136 | + } |
| 137 | + else if (upgradeMode == DomainUpgradeMode.LegacySkip) { |
| 138 | + // sequence |
| 139 | + return $"SELECT CASE WHEN Count([name]) = 1 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 140 | + } |
| 141 | + else if (upgradeMode == DomainUpgradeMode.LegacyValidate) { |
| 142 | + // sequence |
| 143 | + return $"SELECT CASE WHEN Count([name]) = 1 THEN Cast(1 as bit) ELSE CAST(0 as bit) END AS r FROM [DO-Tests].[sys].[sequences] WHERE[name] = N'{sequenceName}'"; |
| 144 | + } |
| 145 | + throw new ArgumentOutOfRangeException(); |
| 146 | + } |
| 147 | + |
| 148 | + private string GetSequenceName(Type baseType) |
| 149 | + { |
| 150 | + if (baseType == typeof(byte)) { |
| 151 | + return "TinyIntSeq"; |
| 152 | + } |
| 153 | + else if (baseType == typeof(short)) { |
| 154 | + return "SmallIntSeq"; |
| 155 | + } |
| 156 | + else if (baseType == typeof(int)) { |
| 157 | + return "IntSeq"; |
| 158 | + } |
| 159 | + else if (baseType==typeof(long)) { |
| 160 | + return "BigIntSeq"; |
| 161 | + } |
| 162 | + throw new ArgumentOutOfRangeException(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments