Created
December 13, 2017 11:07
-
-
Save MatteoPiovanelli-Laser/2bf66dcf313a8b912492e63956dfb1d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Nwazet.Commerce.Models; | |
using Orchard.ContentManagement.MetaData; | |
using Orchard.Data; | |
using Orchard.Data.Migration; | |
using Orchard.Environment.Extensions; | |
using System; | |
using System.Data; | |
namespace Nwazet.Commerce.Migrations { | |
[OrchardFeature("Nwazet.BaseTaxImplementations")] | |
public class BaseTaxImplementationsMigrations : DataMigrationImpl { | |
private readonly ITransactionManager _transactionManager; | |
private readonly IRepository<StateOrCountryTaxPartRecord> _repository; | |
public BaseTaxImplementationsMigrations( | |
ITransactionManager transactionManager, | |
IRepository<StateOrCountryTaxPartRecord> repository) { | |
_transactionManager = transactionManager; | |
_repository = repository; | |
} | |
public int Create() { | |
// we could be calling this because we enabled the feature in the migrations for | |
// Nwazet.Taxes. In that case, we do not have to actually create the tables, because | |
// those were handled there in the past. | |
try { | |
SchemaBuilder.CreateTable("StateOrCountryTaxPartRecord", table => table | |
.ContentPartRecord() | |
.Column<string>("State") | |
.Column<string>("Country") | |
.Column<decimal>("Rate") | |
.Column<int>("Priority") | |
); | |
ContentDefinitionManager.AlterTypeDefinition("StateOrCountryTax", cfg => cfg | |
.WithPart("StateOrCountryTaxPart")); | |
ContentDefinitionManager.AlterTypeDefinition("ZipCodeTax", cfg => cfg | |
.WithPart("ZipCodeTaxPart")); | |
} catch (Exception) { | |
// The CreateTable throws an SqlException if the table already exists, so I thought | |
// this could work. However, in its own handling it attempts to do _transaction.RollBack() | |
// and that fails saying "Transaction not connected, or was disconnected". | |
} | |
return 1; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Orchard.ContentManagement.MetaData; | |
using Orchard.Data; | |
using Orchard.Data.Migration; | |
using Orchard.Data.Migration.Records; | |
using Orchard.Environment.Extensions; | |
using Orchard.Modules.Services; | |
using System.Data; | |
namespace Nwazet.Commerce.Migrations { | |
[OrchardFeature("Nwazet.Taxes")] | |
public class TaxMigrations : DataMigrationImpl { | |
private readonly IRepository<DataMigrationRecord> _dataMigrationRepository; | |
private readonly IModuleService _moduleService; | |
private readonly ITransactionManager _transactionManager; | |
public TaxMigrations( | |
IRepository<DataMigrationRecord> dataMigrationRepository, | |
IModuleService moduleService, | |
ITransactionManager transactionManager) { | |
_dataMigrationRepository = dataMigrationRepository; | |
_moduleService = moduleService; | |
_transactionManager = transactionManager; | |
} | |
public int Create() { | |
// Update: we moved the StateCountryTax and ZipCodeTax implementations to a separate feature. | |
// Hence we need to skip the migrations steps related to them. | |
return 4; | |
} | |
// UpdateFrom1() and UpdateFrom2() still here, for retrocompatibility | |
public int UpdateFrom1() { | |
ContentDefinitionManager.AlterTypeDefinition("ZipCodeTax", cfg => cfg | |
.WithPart("ZipCodeTaxPart")); | |
return 2; | |
} | |
public int UpdateFrom2() { | |
SchemaBuilder.AlterTable("StateOrCountryTaxPartRecord", table => | |
table.AlterColumn("Rate", column => | |
column.WithType(DbType.Decimal))); | |
return 3; | |
} | |
public int UpdateFrom3() { | |
// Everything that was done above now belongs to the Nwazet.BaseTaxImplementations feature. | |
// If we are running this method, it means we are updating an old pre-territories system. | |
// We need to activate the Nwazet.BaseTaxImplementations feature, and not run its migrations, | |
// because those would break the db by trying to recreate tables. | |
// The issue is that I don't know how to enable the feature and not run its migration. | |
_moduleService.EnableFeatures(new string[] { "Nwazet.BaseTaxImplementations" }, true); | |
return 4; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment