orchardcore-data-migrations
Scannednpx machina-cli add skill CrestApps/CrestApps.AgentSkills/orchardcore-data-migrations --openclawFiles (1)
SKILL.md
6.9 KB
Orchard Core Data Migrations - Prompt Templates
Create Data Migrations
You are an Orchard Core expert. Generate data migration code for Orchard Core modules.
Guidelines
- Data migrations inherit from
DataMigration. - Migrations define content types, parts, fields, and database indexes.
- Use
CreateAsync()for initial migration,UpdateFrom1Async(),UpdateFrom2Async()for incremental updates. - Register migrations in
Startup.csusingservices.AddScoped<IDataMigration, Migrations>(). IContentDefinitionManageris used to define content types and parts.SchemaBuilderis used to create and alter YesSql index tables.- Always seal classes.
Basic Migration with Content Type
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;
public sealed class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
public Migrations(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}
public async Task<int> CreateAsync()
{
await _contentDefinitionManager.AlterTypeDefinitionAsync("{{ContentType}}", type => type
.DisplayedAs("{{DisplayName}}")
.Creatable()
.Listable()
.Draftable()
.Versionable()
.WithPart("TitlePart", part => part.WithPosition("0"))
.WithPart("AutoroutePart", part => part
.WithPosition("1")
.WithSettings(new AutoroutePartSettings
{
AllowCustomPath = true,
Pattern = "{{ ContentItem | display_text | slugify }}"
})
)
.WithPart("HtmlBodyPart", part => part
.WithPosition("2")
.WithEditor("Wysiwyg")
)
.WithPart("{{ContentType}}Part", part => part.WithPosition("3"))
);
return 1;
}
}
Migration with Custom Content Part and Fields
public sealed class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
public Migrations(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}
public async Task<int> CreateAsync()
{
// Define the custom part with fields
await _contentDefinitionManager.AlterPartDefinitionAsync("{{PartName}}", part => part
.WithField("{{FieldName}}", field => field
.OfType("TextField")
.WithDisplayName("{{FieldDisplayName}}")
.WithPosition("0")
.WithSettings(new TextFieldSettings
{
Required = true,
Hint = "{{FieldHint}}"
})
)
.WithField("Description", field => field
.OfType("TextField")
.WithDisplayName("Description")
.WithPosition("1")
.WithEditor("TextArea")
)
.WithField("Image", field => field
.OfType("MediaField")
.WithDisplayName("Image")
.WithPosition("2")
)
);
// Define the content type with the custom part
await _contentDefinitionManager.AlterTypeDefinitionAsync("{{ContentType}}", type => type
.DisplayedAs("{{DisplayName}}")
.Creatable()
.Listable()
.WithPart("TitlePart", part => part.WithPosition("0"))
.WithPart("{{PartName}}", part => part.WithPosition("1"))
);
return 1;
}
}
Migration with YesSql Index Table
using OrchardCore.Data.Migration;
using YesSql.Sql;
public sealed class Migrations : DataMigration
{
public async Task<int> CreateAsync()
{
await SchemaBuilder.CreateMapIndexTableAsync<{{IndexName}}>(table => table
.Column<string>(nameof({{IndexName}}.ContentItemId), col => col.WithLength(26))
.Column<string>(nameof({{IndexName}}.{{PropertyName}}), col => col.WithLength(256))
.Column<bool>(nameof({{IndexName}}.Published))
.Column<DateTime>(nameof({{IndexName}}.CreatedUtc))
);
await SchemaBuilder.AlterIndexTableAsync<{{IndexName}}>(table => table
.CreateIndex(
"IDX_{{IndexName}}_{{PropertyName}}",
nameof({{IndexName}}.{{PropertyName}}),
nameof({{IndexName}}.Published)
)
);
return 1;
}
}
Incremental Migration (UpdateFrom)
public sealed class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
public Migrations(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}
public async Task<int> CreateAsync()
{
// Initial migration
await _contentDefinitionManager.AlterTypeDefinitionAsync("{{ContentType}}", type => type
.DisplayedAs("{{DisplayName}}")
.Creatable()
.Listable()
.WithPart("TitlePart")
);
return 1;
}
public async Task<int> UpdateFrom1Async()
{
// Add a new field in version 2
await _contentDefinitionManager.AlterPartDefinitionAsync("{{ContentType}}", part => part
.WithField("Category", field => field
.OfType("TextField")
.WithDisplayName("Category")
.WithPosition("2")
)
);
return 2;
}
public async Task<int> UpdateFrom2Async()
{
// Add an index table in version 3
await SchemaBuilder.CreateMapIndexTableAsync<{{ContentType}}Index>(table => table
.Column<string>("ContentItemId", col => col.WithLength(26))
.Column<string>("Category", col => col.WithLength(256))
.Column<bool>("Published")
);
return 3;
}
}
Registering Migrations and Indexes
using OrchardCore.Data.Migration;
using YesSql.Indexes;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDataMigration, Migrations>();
services.AddIndexProvider<{{IndexName}}Provider>();
}
}
Uninstall Migration
Handle cleanup when a feature is disabled:
public sealed class Migrations : DataMigration
{
public async Task UninstallAsync()
{
await SchemaBuilder.DropMapIndexTableAsync<{{IndexName}}>();
}
}
Source
git clone https://github.com/CrestApps/CrestApps.AgentSkills/blob/main/src/CrestApps.AgentSkills/orchardcore/orchardcore-data-migrations/SKILL.mdView on GitHub Overview
Orchard Core Data Migrations lets you define schema and content model changes through DataMigration classes. It covers content types, parts, fields, YesSql index tables, and data seeding, with versioned updates.
How This Skill Works
Create a sealed class that inherits DataMigration, implement CreateAsync for initial setup, and UpdateFromXAsync methods for incremental changes. Use IContentDefinitionManager to define content types and parts, and SchemaBuilder to manage YesSql index tables. Register migrations in Startup.cs with services.AddScoped<IDataMigration, Migrations>().
When to Use It
- Adding a new content type or part to an Orchard Core module.
- Creating or updating YesSql index tables via SchemaBuilder.
- Seeding initial data during module installation.
- Applying incremental schema/content migrations across versions.
- Managing migration versioning with CreateAsync and UpdateFromXAsync.
Quick Start
- Step 1: Create a sealed Migrations class that inherits DataMigration and inject IContentDefinitionManager.
- Step 2: Implement CreateAsync to define content types/parts and YesSql indexes, then return 1.
- Step 3: Register the migration in Startup.cs with services.AddScoped<IDataMigration, Migrations>() and run the app.
Best Practices
- Always seal migration classes.
- Use CreateAsync for initial setup and UpdateFromXAsync for incremental changes.
- Use IContentDefinitionManager to define content types and parts.
- Register migrations in Startup.cs with services.AddScoped<IDataMigration, Migrations>().
- Test migrations in isolation and aim for idempotent upgrades.
Example Use Cases
- Create an initial content type with TitlePart, AutoroutePart, and HtmlBodyPart using AlterTypeDefinitionAsync.
- Define a custom part with fields (TextField, MediaField) via AlterPartDefinitionAsync and add it to a content type.
- Create and alter YesSql index tables using SchemaBuilder in CreateAsync and UpdateFrom1Async.
- Seed initial content data after the first migration to bootstrap the module.
- Add a versioned update to introduce a new field or index with UpdateFrom1Async.
Frequently Asked Questions
Add this skill to your agents