Skip to content

SQL objects

A *.sql file is migrated by the runtime. Recognized DDL is executed; anything else is only recorded (so a plain CREATE TABLE will not create a table — use an entity for that):

  • Executed: CREATE [OR ALTER] PROCEDURE / FUNCTION / VIEW / TRIGGER (SQL Server or PostgreSQL identifier styles). Use CREATE OR ALTER for idempotency.
  • Recorded only: everything else (plain statements).
CREATE OR ALTER VIEW Sales.vw_SalesSummary AS
SELECT c.Id AS CustomerId, COUNT(s.Id) AS SaleCount, ISNULL(SUM(s.TotalAmount),0) AS TotalSales
FROM Sales.Customers c LEFT JOIN Sales.Sales s ON s.CustomerId = c.Id AND s.IsDeleted = 0
WHERE c.IsDeleted = 0 GROUP BY c.Id;

Call procedures from a row action: <Sql>EXEC Sales.usp_MarkSalePaid @SaleId = @Id;</Sql>. Worked examples: sample/Inventory/models/sql/ (Functions, Views, StoredProcedures, Triggers — e.g. Views/vw_ProductStock.sql, StoredProcedures/usp_PurchaseOrder_Receive.sql).

*.sql files run after entities and views, in full-path alphabetical order across every subfolder. You don’t need to name or arrange files so that dependencies run first:

  • SQL Server resolves names when a procedure or function runs, not when it is created, so order doesn’t matter.
  • PostgreSQL checks LANGUAGE sql bodies when they are created. A function that calls another function or uses a type defined in a file that sorts later fails with undefined function / table / object (SQLSTATE 42883 / 42P01 / 42704). The runtime defers that file and retries it after the rest of the batch. It keeps running retry passes while each pass applies at least one deferred file. Any file that still fails after that is reported with its original error. A file is recorded as applied only after it succeeds, so a file that fails is attempted again on the next startup.

Recommended practice: put one object per file and make every file re-runnable (CREATE OR ALTER on SQL Server, CREATE OR REPLACE on PostgreSQL). Then a retried or forced re-run (MigrationExecution=Forced) is always safe.