Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/scripts/start-database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ case "$database" in
Unit|SQLite) exit 0 ;;
Hana) bash .github/scripts/start-hana.sh; exit 0 ;;
MySQL)
docker run -d --name migrator-db -p 3306:3306 -e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_DATABASE=testdb -e MYSQL_USER=testuser -e MYSQL_PASSWORD=testpass mysql:8.0.44
# Disposable CI server: permit the migration user to create triggers with binlogging enabled.
docker run -d --name migrator-db -p 3306:3306 -e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_DATABASE=testdb -e MYSQL_USER=testuser -e MYSQL_PASSWORD=testpass mysql:8.0.44 --log-bin-trust-function-creators=1
ready() { docker exec migrator-db mysql -uroot -prootpass -e 'SELECT 1' >/dev/null 2>&1; }
;;
MariaDB)
Expand Down
87 changes: 87 additions & 0 deletions docs/portable-schema-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Portable schema expressions and triggers

Import `DotNetProjects.Migrator.Framework` and use `SqlExpression` to compose
server expressions. Identifiers use the provider's quoting rules; explicitly
delimit mixed-case PostgreSQL names. `Raw` is a trusted, provider-specific SQL
escape hatch, never an input sanitization API. Literals are rendered for DDL,
where bind parameters are not permitted.

```csharp
using static DotNetProjects.Migrator.Framework.SqlExpression;

var finalized = Exists("Parent", "p",
Column("Id", "p").EqualTo(Old("ParentId"))
.And(Column("Finalized", "p").EqualTo(Value(1))));
Database.CreateGuardTrigger(new GuardTrigger("Guard_Child_Update", "Child",
TriggerEvent.Update, "Finalized parent", finalized, KeyColumns: ["Id"]));
Database.DropGuardTrigger("Guard_Child_Update", "Child");
```

The fluent equivalents are `builder.Create.GuardTrigger(definition)` and
`builder.Delete.Trigger(name, table)`. They support SQL preview and explicit
reverse operations. Create fails if the name already exists; drop fails if it
does not exist. Each definition has exactly one Insert, Update or Delete event.
Omitting Condition rejects all affected rows. Old is unavailable on INSERT;
New is unavailable on DELETE. Combine OLD and NEW parent predicates to protect
both ends of reparenting. `ChangedFrom` compares SQL NULL safely.

SQLite (including MonoSQLite), PostgreSQL (including the legacy dialect),
SQL Server 2012+ and MySQL/MariaDB have guard implementations. SQLite uses
RAISE(ABORT), PostgreSQL raises an exception, SQL Server uses THROW and MySQL
uses SIGNAL. An invalid row rejects the statement, including its trigger side
effects. SQL Server errors can invalidate the containing transaction; roll it
back before continuing. MySQL requires transactional tables (InnoDB).

SQL Server UPDATE triggers require KeyColumns naming a non-null unique key.
Assignments to these columns are rejected, even if the value is unchanged,
because joining inserted/deleted otherwise cannot reliably pair old and new
rows. Multi-row statements are evaluated with set-based EXISTS, never scalar
variables. UpdateColumns means column assignment (including an unchanged
value), and is supported on SQLite, PostgreSQL and SQL Server. MySQL rejects
this option; use an OLD/NEW change predicate there.

Oracle, DB2, Firebird, HANA, Informix and Sybase also have guard implementations,
with live fixtures for multi-row rejection, valid writes, reparenting and named
drop/recreate. Oracle uses raise_application_error, DB2 SIGNAL, HANA SIGNAL
SQL_ERROR_CODE, Informix an SPL error procedure, Firebird a named exception,
and Sybase ROLLBACK TRIGGER WITH RAISERROR. Firebird/Informix helper objects use
the trigger name plus `_error`/`_fn` and are removed by DropGuardTrigger. Firebird
helper DDL is transactional. Oracle and HANA retain their native DDL commit
behavior. Oracle predicates may query related tables but must respect Oracle's
mutating-table restrictions. Sybase uses set-based OLD/NEW pairing with the same
immutable key requirement as SQL Server. Firebird/HANA cannot detect assignments
to unchanged columns and reject UpdateColumns explicitly.

`GuardTriggerProviderTests` runs the same five behavioral cases against all
eleven implementations through both direct SQL and the database-independent
Insert/Update/Delete/Select APIs: write rejection, statement atomicity for bulk
updates/deletes, null-safe changes and reparenting. Projected INSERT tests also
cover multi-row insert atomicity once that API is available. "Portable" refers to the shared C# definition;
the library renders native trigger SQL for each database. It is not an arbitrary
trigger-body translator. HANA guard creation reads native column types from its
catalog for OLD/NEW scalar bindings; offline preview of predicates with these
bindings is currently unavailable and throws `NotSupportedException`.

Domain messages must be representable by the database and connection character
sets. The Informix and ASE CI images use legacy single-byte locales, so their
shared guard cases use ASCII messages; the other nine fixtures also assert a
Japanese message round trip. Apostrophes and backslashes are tested on all eleven.

Ingres supports rules and error-raising procedures but is not implemented by
this API yet; it throws NotSupportedException. This is a library implementation
gap, not a claim that Ingres lacks database-enforced write guards.

MySQL servers with binary logging require sufficient trigger-creation privileges
or an administrator-configured log_bin_trust_function_creators setting. The CI
container enables that setting only on its disposable test server.

Guard predicates do not serialize concurrent transactions. Cross-row uniqueness
invariants also require a database UNIQUE constraint or suitable isolation and
locking. A predicate tested against a serial fixture is not a concurrency lock.
PostgreSQL trigger functions use the trigger's name plus `_fn` in the table's
schema; reserve that name and drop through this API to remove both objects.

Syntax references: [SQLite triggers](https://www.sqlite.org/lang_createtrigger.html),
[PostgreSQL triggers](https://www.postgresql.org/docs/current/sql-createtrigger.html),
[SQL Server multi-row triggers](https://learn.microsoft.com/en-us/sql/relational-databases/triggers/create-dml-triggers-to-handle-multiple-rows-of-data),
[MySQL SIGNAL](https://dev.mysql.com/doc/refman/8.0/en/signal.html).
90 changes: 90 additions & 0 deletions src/Migrator.Tests/GuardTriggerExportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Data;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Framework.Fluent;
using DotNetProjects.Migrator.Providers;
using DotNetProjects.Migrator.Providers.Impl.SQLite;
using Microsoft.Data.Sqlite;
using NUnit.Framework;

namespace Migrator.Tests;

[Category("Unit")]
public class GuardTriggerExportTests
{
[TestCase("CurrentStatus", "PreviousStatus")]
[TestCase("Σ", "σ")]
public void ExportRejectsUnknownQueuedColumns(string actual, string watched)
{
var b = new MigrationBuilder();
b.Create.Table("Child").WithColumn(actual).AsInt32();
b.Create.GuardTrigger(new("guard", "main.child", TriggerEvent.Update, "Denied", UpdateColumns: [watched]));
Assert.Throws<ArgumentException>(() => b.Preview(new SqlGenerationContext(ProviderTypes.SQLite)));
}

[TestCase("CurrentStatus")]
[TestCase("rowid")]
public void ExportReportsWhenExistingColumnMetadataIsUnavailable(string watched)
{
var b = new MigrationBuilder();
b.Create.GuardTrigger(new("guard", "Child", TriggerEvent.Update, "Denied", UpdateColumns: [watched]));
Assert.That(Assert.Throws<MigrationException>(() => b.Preview(new SqlGenerationContext(ProviderTypes.SQLite))).Message, Does.Contain("definition"));
}

[Test]
public void ExistingDefinitionsValidateOrdinaryColumnsButCannotProveImplicitRowids()
{
var b = new MigrationBuilder();
b.Create.GuardTrigger(new("guard", "Child", TriggerEvent.Update, "Denied", UpdateColumns: ["currentstatus"]));
Assert.That(b.Preview(new SqlGenerationContext(ProviderTypes.SQLite, _ => [new Column("CurrentStatus", DbType.Int32)]))[0], Does.Contain("CREATE TRIGGER"));
Assert.Throws<ArgumentException>(() => b.Preview(new SqlGenerationContext(ProviderTypes.SQLite, _ => [new Column("PreviousStatus", DbType.Int32)])));
b = new MigrationBuilder();
b.Create.GuardTrigger(new("guard", "Child", TriggerEvent.Update, "Denied", UpdateColumns: ["rowid"]));
Assert.Throws<NotSupportedException>(() => b.Preview(new SqlGenerationContext(ProviderTypes.SQLite, _ => [new Column("Id", DbType.Int32)])));
Assert.That(b.Preview(new SqlGenerationContext(ProviderTypes.SQLite, _ => [new Column("rowid", DbType.Int32)]))[0], Does.Contain("CREATE TRIGGER"));
}

[TestCase("rowid")]
[TestCase("OID")]
[TestCase("_RoWiD_")]
public void QueuedRowidTablesCanExportImplicitAliases(string watched)
{
var b = new MigrationBuilder(); b.Create.Table("Child").WithColumn("Id").AsInt32();
b.Create.GuardTrigger(new("guard", "Child", TriggerEvent.Update, "Denied", UpdateColumns: [watched]));
Assert.That(b.Preview(new SqlGenerationContext(ProviderTypes.SQLite)), Has.Count.EqualTo(2));
}

[Test, Category("SQLite")]
public void ExportTracksRenamedTablesAndColumns()
{
var b = new MigrationBuilder(); b.Create.Table("Child").WithColumn("CurrentStatus").AsInt32();
b.Rename.Column("CurrentStatus").OnTable("Child").To("FinalStatus");
b.Rename.Table("Child").To("Renamed");
b.Create.GuardTrigger(new("guard", "main.renamed", TriggerEvent.Update, "Renamed guard", UpdateColumns: ["finalstatus", "rowid"]));
Assert.That(b.Preview(new SqlGenerationContext(ProviderTypes.SQLite)), Has.Count.EqualTo(4));
}

[Test]
public void ExportRejectsRemovedTargetsAndSchemaInvalidatedByRawSql()
{
var b = new MigrationBuilder(); b.Create.Table("Child").WithColumn("Status").AsInt32(); b.Delete.Table("Child");
b.Create.GuardTrigger(new("guard", "main.child", TriggerEvent.Update, "Denied", UpdateColumns: ["Status"]));
Assert.Throws<MigrationException>(() => b.Preview(new SqlGenerationContext(ProviderTypes.SQLite)));
b = new MigrationBuilder(); b.Execute.Sql("CREATE TABLE Child(Status INTEGER)");
b.Create.GuardTrigger(new("guard", "Child", TriggerEvent.Update, "Denied", UpdateColumns: ["Status"]));
Assert.Throws<NotSupportedException>(() => b.Preview(new SqlGenerationContext(ProviderTypes.SQLite)));
}

[Test, Category("SQLite")]
public void ExportedGuardActuallyProtectsDatabaseIndependentUpdates()
{
var b = new MigrationBuilder(); b.Create.Table("Child").WithColumn("Id").AsInt32().WithColumn("CurrentStatus").AsInt32();
b.Create.GuardTrigger(new("guard", "main.child", TriggerEvent.Update, "Exported guard", UpdateColumns: ["currentstatus"]));
using var connection = new SqliteConnection("Data Source=:memory:"); connection.Open();
using var p = new SQLiteTransformationProvider(new SQLiteDialect(), connection, null, null);
foreach (var sql in b.Preview(new SqlGenerationContext(ProviderTypes.SQLite))) p.ExecuteNonQuery(sql);
p.Insert("Child", ["Id", "CurrentStatus"], [1, 0]);
Assert.That(p.Update("Child", ["Id"], [2], ["Id"], [1]), Is.EqualTo(1));
Assert.That(Assert.Catch(() => p.Update("Child", ["CurrentStatus"], [1], ["Id"], [2])).ToString(), Does.Contain("Exported guard"));
}
}
Loading
Loading