Migration linting now checks the functions, stored procedures, and triggers each migration creates or changes, and reports those above your complexity limits.
The policy is the complexity analyzer of migration linting. It is off by default. To enable it, add its block to the lint configuration:
lint {complexity {cyclomatic = 10 # Defaults to 10.nesting = 3 # Defaults to 3.statements = 40 # Disabled by default.parameters = 8 # Disabled by default.error = true # Defaults to false.}}
Each attribute other than error sets a limit:
- cyclomatic: the number of independent paths through a routine.
- nesting: how deep IF and loop statements are nested.
- statements: how many statements the body has.
- parameters: how many parameters the routine takes.
A routine above a limit is reported as a warning. With error = true, it fails the lint.
Reported as Routines Grow
atlas migrate lint checks each function, procedure, and trigger that a migration file creates or changes. A changed routine is reported only if its complexity grew, with its previous value. This migration adds a guard to fulfill_orders, a function already above a cyclomatic limit of 10 and a statements limit of 30:
-CREATE FUNCTION fulfill_orders(p_warehouse int, p_limit int DEFAULT 100)+CREATE OR REPLACE FUNCTION fulfill_orders(p_warehouse int, p_limit int DEFAULT 100)...BEGIN+ IF p_limit > 1000 THEN+ RAISE EXCEPTION 'limit % is too large', p_limit;+ END IF;<<orders_loop>>FOR o IN SELECT * FROM orders ...
-- analyzing version 3-- complex routines detected:-- L1: Function "fulfill_orders" has a cyclomatic complexity of 13 (was 12), above thelimit of 10 https://atlasgo.io/lint/analyzers#CX101-- L1: Function "fulfill_orders" has 36 statements (was 34), above the limit of 30https://atlasgo.io/lint/analyzers#CX103-- suggested fix:-> Split complex routines into smaller functions or procedures that each handle a singletask
Supported Databases
The policy measures routines on PostgreSQL, CockroachDB, YugabyteDB, Aurora DSQL, HorizonDB, Redshift, MySQL, MariaDB, SQL Server, Oracle, Snowflake, and more.
With a Coding Agent
With the Atlas agent skills installed, the atlas skill runs atlas migrate lint on each migration the agent generates, so the policy applies to the routines it writes.
Migration linting is available to Atlas Pro users. Run atlas login to create an account or start a free trial.
For examples on each database and for adopting the policy on an existing schema, see the Routine Complexity Policy guide.