Skip to content

Commit

Permalink
Merge pull request #205 from Kentico/fix/184_deprecation_of_XbKConnec…
Browse files Browse the repository at this point in the history
…tionString

#184 deprecation of XbKConnectionString
  • Loading branch information
fialafilip authored Jul 15, 2024
2 parents a0214b5 + d69d071 commit 385963b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 304 deletions.
4 changes: 2 additions & 2 deletions Migration.Toolkit.CLI/ConfigurationValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static IEnumerable<ValidationMessage> GetValidationErrors(IConfigurationR
yield return new ValidationMessage(ValidationMessageType.Warning, Resources.ConfigurationValidator_GetValidationErrors_SourceCmsDirPath_IsRecommended);
}

if (CheckCfgValue(settings?.GetValue<string>(ConfigurationNames.XbKConnectionString)))
if (settings?.GetValue<string>(ConfigurationNames.XbKConnectionString) is {})
{
yield return new ValidationMessage(ValidationMessageType.Error, Resources.ConfigurationValidator_GetValidationErrors_TargetConnectionString_IsRequired);
yield return new ValidationMessage(ValidationMessageType.Warning, $"Configuration key '{ConfigurationNames.XbKConnectionString}' is deprecated, use 'Settings:ConnectionStrings:CMSConnectionString' instead");
}

if (CheckCfgValue(settings?.GetValue<string>(ConfigurationNames.XbKDirPath)))
Expand Down
7 changes: 4 additions & 3 deletions Migration.Toolkit.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@
}

var settingsSection = config.GetRequiredSection(ConfigurationNames.Settings);
var settings = settingsSection.Get<ToolkitConfiguration>();
settings.EntityConfigurations ??= new EntityConfigurations();
var settings = settingsSection.Get<ToolkitConfiguration>() ?? new();
var kxpApiSettings = settingsSection.GetSection(ConfigurationNames.XbKApiSettings);
settings.SetXbKConnectionStringIfNotEmpty(kxpApiSettings["ConnectionStrings:CMSConnectionString"]);

var services = new ServiceCollection();

Expand Down Expand Up @@ -150,7 +151,7 @@

services.UseKxpDbContext(settings);

var kxpApiSettings = settingsSection.GetSection(ConfigurationNames.XbKApiSettings);

services.UseKxpApi(kxpApiSettings, settings.XbKDirPath);
services.AddSingleton(settings);
services.AddSingleton<ICommandParser, CommandParser>();
Expand Down
32 changes: 15 additions & 17 deletions Migration.Toolkit.CLI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,23 @@ Before you run the migration, configure options in the `Migration.Toolkit.CLI/ap

Add the options under the `Settings` section in the configuration file.

| Configuration | Description |
|----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| KxConnectionString | The connection string to the source Kentico Xperience 13 database. |
| KxCmsDirPath | The absolute file system path of the **CMS** folder in the source Kentico Xperience 13 administration project. Required to migrate media library files. |
| XbKConnectionString | The connection string to the target Xperience by Kentico database. |
| XbKDirPath | The absolute file system path of the root of the target Xperience by Kentico project. Required to migrate media library and page attachment files. |
| XbKApiSettings | Configuration options set for the API when creating migrated objects in the target application.<br /><br />The `ConnectionStrings.CMSConnectionString`option is required - set the connection string to the target Xperience by Kentico database (the same value as `XbKConnectionString`). |
| MigrationProtocolPath | The absolute file system path of the location where the [migration protocol file](./MIGRATION_PROTOCOL_REFERENCE.md) is generated.<br /><br />For example: `"C:\\Logs\\Migration.Toolkit.Protocol.log"` |
| MigrateOnlyMediaFileInfo | If set to `true`, only the database representations of media files are migrated, without the files in the media folder in the project's file system. For example, enable this option if your media library files are mapped to a shared directory or Cloud storage.<br /><br />If `false`, media files are migrated based on the `KxCmsDirPath` location. |
| Configuration | Description |
|---------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| KxConnectionString | The connection string to the source Kentico Xperience 13 database. |
| KxCmsDirPath | The absolute file system path of the **CMS** folder in the source Kentico Xperience 13 administration project. Required to migrate media library files. |
| XbKDirPath | The absolute file system path of the root of the target Xperience by Kentico project. Required to migrate media library and page attachment files. |
| XbKApiSettings | Configuration options set for the API when creating migrated objects in the target application.<br /><br />The `ConnectionStrings.CMSConnectionString`option is required - set the connection string to the target Xperience by Kentico database (the same value as `XbKConnectionString`). |
| MigrationProtocolPath | The absolute file system path of the location where the [migration protocol file](./MIGRATION_PROTOCOL_REFERENCE.md) is generated.<br /><br />For example: `"C:\\Logs\\Migration.Toolkit.Protocol.log"` |
| MigrateOnlyMediaFileInfo | If set to `true`, only the database representations of media files are migrated, without the files in the media folder in the project's file system. For example, enable this option if your media library files are mapped to a shared directory or Cloud storage.<br /><br />If `false`, media files are migrated based on the `KxCmsDirPath` location. |
| MemberIncludeUserSystemFields | Determines which system fields from the *CMS_User* and *CMS_UserSettings* tables are migrated to *CMS_Member* in Xperience by Kentico. Fields that do not exist in *CMS_Member* are automatically created. <br /><br />The sample `appsettings.json` file included with the toolkit by default includes all user fields that can be migrated from Kentico Xperience 13. Exclude specific fields from the migration by removing them from this configuration option. |
| UseOmActivityNodeRelationAutofix | Determines how the migration handles references from Contact management activities to non-existing pages.<br /><br />Possible options:<br />`DiscardData` - faulty references are removed,<br />`AttemptFix` - references are updated to the IDs of corresponding pages created by the migration,<br />`Error` - an error is reported and the reference can be translated or otherwise handled manually |
| UseOmActivitySiteRelationAutofix | Determines how the migration handles site references from Contact management activities.<br /><br />Possible options: `DiscardData`,`AttemptFix`,`Error` |
| EntityConfigurations | Contains options that allow you to fine-tune the migration of specific object types. |
| EntityConfigurations.*&lt;object table name&gt;*.ExcludeCodeNames | Excludes objects with the specified code names from the migration. |
| UseOmActivityNodeRelationAutofix | Determines how the migration handles references from Contact management activities to non-existing pages.<br /><br />Possible options:<br />`DiscardData` - faulty references are removed,<br />`AttemptFix` - references are updated to the IDs of corresponding pages created by the migration,<br />`Error` - an error is reported and the reference can be translated or otherwise handled manually |
| UseOmActivitySiteRelationAutofix | Determines how the migration handles site references from Contact management activities.<br /><br />Possible options: `DiscardData`,`AttemptFix`,`Error` |
| EntityConfigurations | Contains options that allow you to fine-tune the migration of specific object types. |
| EntityConfigurations.*&lt;object table name&gt;*.ExcludeCodeNames | Excludes objects with the specified code names from the migration. |
| CreateReusableFieldSchemaForClasses | Specifies which page types are also converted to [reusable field schemas](#convert-page-types-to-reusable-field-schemas). |
| OptInFeatures.QuerySourceInstanceApi.Enabled | If `true`, [source instance API discovery](#source-instance-api-discovery) is enabled to allow advanced migration of Page Builder content for pages and page templates. |
| OptInFeatures.QuerySourceInstanceApi.Connections | To use [source instance API discovery](#source-instance-api-discovery), you need to add a connection JSON object containing the following values:<br />`SourceInstanceUri` - the base URI where the source instance's live site application is running.<br />`Secret` - the secret that you set in the *ToolkitApiController.cs* file on the source instance. |
| OptInFeatures.CustomMigration.FieldMigrations | Enables conversion of media selection text fields to media library files. See [Convert text fields with media links to media libraries](#convert-text-fields-with-media-links-to-media-libraries) for more information.|
| OptInFeatures.QuerySourceInstanceApi.Enabled | If `true`, [source instance API discovery](#source-instance-api-discovery) is enabled to allow advanced migration of Page Builder content for pages and page templates. |
| OptInFeatures.QuerySourceInstanceApi.Connections | To use [source instance API discovery](#source-instance-api-discovery), you need to add a connection JSON object containing the following values:<br />`SourceInstanceUri` - the base URI where the source instance's live site application is running.<br />`Secret` - the secret that you set in the *ToolkitApiController.cs* file on the source instance. |
| OptInFeatures.CustomMigration.FieldMigrations | Enables conversion of media selection text fields to media library files. See [Convert text fields with media links to media libraries](#convert-text-fields-with-media-links-to-media-libraries) for more information.|

### Example

Expand All @@ -351,7 +350,6 @@ Add the options under the `Settings` section in the configuration file.
"Settings": {
"KxConnectionString": "Data Source=myserver;Initial Catalog=Xperience13;Integrated Security=True;Persist Security Info=False;Connect Timeout=120;Encrypt=False;Current Language=English;",
"KxCmsDirPath": "C:\\inetpub\\wwwroot\\Xperience13\\CMS",
"XbKConnectionString": "Data Source=myserver;Initial Catalog=XperienceByKentico;Integrated Security=True;Persist Security Info=False;Connect Timeout=120;Encrypt=False;Current Language=English;",
"XbKDirPath": "C:\\inetpub\\wwwroot\\XP_Target",
"XbKApiSettings": {
"ConnectionStrings": {
Expand Down
1 change: 0 additions & 1 deletion Migration.Toolkit.CLI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"Settings": {
"KxConnectionString": "[TODO]",
"KxCmsDirPath": "[TODO]",
"XbKConnectionString": "[TODO]",
"XbKDirPath": "[TODO]",
"MigrateOnlyMediaFileInfo": false,
"XbKApiSettings": {
Expand Down
1 change: 1 addition & 0 deletions Migration.Toolkit.Common/ConfigurationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ConfigurationNames
public const string KxConnectionString = "KxConnectionString";
public const string KxCmsDirPath = "KxCmsDirPath";

[Obsolete("not needed anymore, connection string from Kentico config section is used")]
public const string XbKConnectionString = "XbKConnectionString";

public const string XbKDirPath = "XbKDirPath";
Expand Down
68 changes: 14 additions & 54 deletions Migration.Toolkit.Common/ToolkitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ public string KxConnectionString
#region Connection string of target instance

[ConfigurationKeyName(ConfigurationNames.XbKConnectionString)]
public string? XbKConnectionString { get; set; }
public string XbKConnectionString
{
get => _xbKConnectionString!;
set => _xbKConnectionString = value;
}

public void SetXbKConnectionStringIfNotEmpty(string? connectionString)
{
if (!string.IsNullOrWhiteSpace(connectionString))
{
_xbKConnectionString = connectionString;
}
}

#endregion

#region Path to root directory of target instance

private HashSet<string>? _classNamesCreateReusableSchema;
private string? _xbKConnectionString;

[ConfigurationKeyName(ConfigurationNames.XbKDirPath)]
public string? XbKDirPath { get; set; } = null;
Expand Down Expand Up @@ -82,59 +95,6 @@ public string KxConnectionString
StringComparer.InvariantCultureIgnoreCase
);

public Dictionary<int, int> RequireExplicitMapping<TEntityType>(Expression<Func<TEntityType, object>> keyNameSelector)
{
var memberName = keyNameSelector.GetMemberName();
var migratedIds = EntityConfigurations?.GetEntityConfiguration<TEntityType>()?.ExplicitPrimaryKeyMapping[memberName];
if (migratedIds == null)
{
throw new InvalidOperationException(string.Format(Resources.Exception_MappingIsRequired, typeof(TEntityType).Name, memberName));
}

return migratedIds.ToDictionary(kvp =>
{
if (int.TryParse(kvp.Key, out var id))
{
return id;
}
throw new InvalidOperationException(string.Format(Resources.Exception_MappingIsRequired, typeof(TEntityType).Name, memberName));
}, kvp =>
{
if (kvp.Value is { } id)
{
return id;
}
throw new InvalidOperationException(string.Format(Resources.Exception_MappingIsRequired, typeof(TEntityType).Name, memberName));
});
}

public void AddExplicitMapping<TEntityType>(Expression<Func<TEntityType, object>> keyNameSelector, int sourceId, int targetId)
{
var memberName = keyNameSelector.GetMemberName();
EntityConfigurations ??= new EntityConfigurations();

var entityConfiguration = EntityConfigurations.GetEntityConfiguration<TEntityType>();
var mapping = entityConfiguration.ExplicitPrimaryKeyMapping;
if (!mapping.ContainsKey(memberName))
{
mapping.Add(memberName, new());
}

if (!mapping[memberName].ContainsKey(sourceId.ToString()))
{
mapping[memberName].Add(sourceId.ToString(), targetId);
}
else
{
mapping[memberName][sourceId.ToString()] = targetId;
}

EntityConfigurations.SetEntityConfiguration<TEntityType>(entityConfiguration);
}


#region Opt-in features

[ConfigurationKeyName(ConfigurationNames.OptInFeatures)]
Expand Down
Loading

0 comments on commit 385963b

Please sign in to comment.