dotnet / dotnet/efcore

Add support for SQL-based migrations

Open
#31,790 11 comments 4 reactions 0 assignees View on GitHub
area-migrations customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

There are two well-known issues with migrations:

1. The SQL is not visible in merge/pull requests for review purposes.
2. The designer files repeat the entire schema for each migration.

I would like a way to enable SQL-based migrations such that:

```cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn(
name: "title",
table: "articles",
type: "longtext",
nullable: false)
.Annotation("MySql:CharSet", "utf8mb4");
}
```

... would instead be generated as:

```cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(
"ALTER TABLE `articles` ADD `title` longtext CHARACTER SET utf8mb4 NOT NULL;");
}
```

In this way:

1. The SQL is visible during merge/pull requests for review purposes.
2. The designer files are likely not required.

This is how migrations work in some other ORMs:

- Doctrine (PHP): https://www.doctrine-project.org/projects/doctrine-migrations/en/3.6/reference/generating-migrations.html#diffing-using-the-orm
- TypeORM (TypeScript): https://typeorm.io/migrations#generating-migrations

This could be implemented as a CLI argument (f.e. `dotnet ef migrations add --use-sql MigrationName`), or it could entirely replace the existing format of migrations.

The one argument that I could see against this would be "the migrations are then database/provider-dependent", however:

- Does that matter? You're usually only using one database provider at a time, and if you decide to switch databases, you would usually reset migrations anyway.
- If necessary, object-based migrations could remain the default, and SQL-based migrations would be opt-in.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.