Overview
Hello, we are a group of developers that wish to extend the SonarC# plugin. The rules that concern the risk of data loss in migrations (see below).
Therefore, most of the rules are focused on the MigrationBuilder, and the various methods that can be envoked on it.
Most rules mentioned in this post are currently being developed and, therefore, this post will be updated to represent their current state.
In the following list, we list all the rules, information about each rule, and reasoning behind each rule. This segment is intended to aid the moderators, and the rules’, types and severities are potentially subject to change.
Rules
DropColumn
Language: C#
Reason:
Dropping a column can lose important data, and should be verified to be intentional.
Tags: ef-core
Type: Bug
Severity: Critical
Display name:
MigrationBuilder should not unintentionally call "DropColumn"
Description: DropColumn can cause data loss
Noncompliant code:
migrationsbuilder.DropColumn("Email");
DropTable
Language: C#
Reason:
Dropping a table can lose important data, and should be verified to be intentional.
Tags: ef-core
Type: Bug
Severity: Critical
Display name: MigrationBuilder should not unintentionally call "DropTable"
Description:
DropTable can cause data loss.
Noncompliant code:
migrationBuilder.DropTable("Users");
DropSchema
Language: C#
Reason:
Dropping a Schema can lose a lot of data,
and should only be used for major restructuring,
therfore it must be verified to be intentional.
Tags: ef-core
Type: Bug
Severity: Blocker
Display name: "DropSchema" should not be called
Description: DropSchema can cause structural data loss.
Description: Make sure dropping schema is safe
Noncompliant code:
migrationBuilder.DropSchema("Database");
DeleteData
Language: C#
Reason:
DeleteData can lose data.
Most cases of DeleteData are intended, but should be tracked.
Tags: ef-core
Type: Code Smell
Category: Info
Display name: Track uses of "DeleteData"
Description: Track DeleteData method calls
Noncompliant code:
migrationBuilder.DeleteData(
"Users","Email", "www.foo.com"
);
UpdateData
Language: C#
Reason:
UpdateData can lose data,
if the replacing data is null or empty,
and should be tracked.
Tags: ef-core
Type: Code Smell
Category: Info
Display name: Track uses of "UpdateData"
Description: Track UpdateData method calls
Noncompliant code:
migrationBuilder.UpdateData(
table: "Users", keyColumn: "Id",
keyValue: "1", column:"Email", value: ""
);
DropCheckConstraint
Language: C#
Reason:
DropCheckConstraint can lose database constraints for columns, and should be tracked.
Tags: ef-core
Type: Code Smell
Category: Info
Display name: Track uses of "DropCheckConstraint"
Description: Track DropCheckConstraint method calls
Noncompliant code:
migrationBuilder.DropCheckConstraint(
"CheckName", "Users"
);
DropUniqueConstraint
Language: C#
Reason:
DropUniqueConstraint can database constraints for columns, and should be tracked
tags: ef-core
Type: Code Smell
Category: Info
Display name: Track usage of "DropUniqueConstraint"
Description: Track DropUniqueConstraint method calls
Description:
Noncompliant code:
migrationBuilder.DropUniqueConstraint(
"UniqueEmail", "Email"
);
DropIndex
Language: C#
Reason:
Dropping indexes unintentionally, can make a database slower.
If an index exists we assume it's needed.
Tags: ef-core
Type: Bug
Severity: Minor
Display name: "DropIndex" should be used to increase performance
Description: DropIndex can cause performance deficiency.
Noncompliant code:
migrationBuilder.DropIndex(
"Email", "Users"
);
DropForeignKey
Language: C#
Reason:
DropForeignKey is used to remove connections between tables,
and should only be used cautiously.
Tags: ef-core
Type: Bug
Severity: Minor
Display name: "DropForeignKey" should be used to increase performance
Description: DropForeignKey can ruin database structure
Noncompliant code:
migrationBuilder.DropForeignKey(
"PersonID", "Orders"
);