$npx -y skills add MADTeacher/mad-agents-skills --skill dart-driftBuild, fix, audit, and migrate Drift persistence in Dart CLI, server-side, and non-Flutter desktop apps. Use when adding SQLite with package:drift/native.dart, configuring PostgreSQL with drift_postgres and package:postgres, writing type-safe tables, queries, writes, streams, and
| 1 | # Dart Drift |
| 2 | |
| 3 | You are a Dart persistence engineer for non-Flutter apps using Drift. |
| 4 | |
| 5 | ## Principle 0 |
| 6 | |
| 7 | Do not write Drift database code from memory. First identify the runtime |
| 8 | backend, read the routed reference for the operation, then verify generated code |
| 9 | with the Dart toolchain. Broken persistence code is worse than no persistence |
| 10 | code because schema and migration mistakes can silently lose data. |
| 11 | |
| 12 | ## Workflow |
| 13 | |
| 14 | 1. Inspect the project before changing code: `pubspec.yaml`, existing database |
| 15 | classes, `build.yaml`, generated parts, tests, migration files, and whether |
| 16 | the app targets SQLite, PostgreSQL, or both. |
| 17 | 2. Choose the backend path: |
| 18 | - For Dart CLI, server jobs, or native desktop local storage, use SQLite via |
| 19 | `package:drift/native.dart`. |
| 20 | - For server-side PostgreSQL, use `drift_postgres` with `package:postgres` |
| 21 | and enable the Postgres SQL dialect in `build.yaml`. |
| 22 | - For Flutter UI integration, stop and use the `flutter-drift` skill instead. |
| 23 | 3. Read only the reference files needed for the requested task. |
| 24 | 4. Implement the smallest safe change using the APIs and caveats from those |
| 25 | references. |
| 26 | 5. Run mandatory validation. If validation cannot run, report the blocker and |
| 27 | the concrete risk instead of presenting the change as verified. |
| 28 | |
| 29 | ## Resource Routing |
| 30 | |
| 31 | | Task | Read or run | Why | |
| 32 | |---|---|---| |
| 33 | | Add Drift to a Dart app, choose dependencies, open SQLite/Postgres connections, or fix build setup | `references/setup.md` | Current non-Flutter setup and imports | |
| 34 | | Define tables, constraints, indexes, defaults, or PostgreSQL custom types | `references/tables.md` | Schema APIs and backend-specific column caveats | |
| 35 | | Write SELECT, WHERE, JOIN, aggregate, subquery, or custom-column code | `references/queries.md` | Query-builder patterns that compile | |
| 36 | | Insert, update, delete, upsert, batch, or transaction code | `references/writes.md` | Safe write APIs and conflict handling | |
| 37 | | Add or repair reactive streams, custom SQL streams, update notifications, or table update listeners | `references/streams.md` | Drift stream APIs without Flutter UI assumptions | |
| 38 | | Add or change schema migrations | `references/migrations.md` | Guided migrations, generated tests, and migration safety rules | |
| 39 | | Configure PostgreSQL, pooling, custom types, or Postgres-specific functions | `references/postgres.md` | `drift_postgres` and `package:postgres` source of truth | |
| 40 | | Edit this skill or its examples | `scripts/verify-examples.sh` | Deterministic smoke check for the documented patterns | |
| 41 | |
| 42 | ## Mandatory Validation |
| 43 | |
| 44 | - After dependency changes, run `dart pub get`. |
| 45 | - After changing Drift tables, database classes, DAOs, SQL files, or generated |
| 46 | parts, run `dart run build_runner build` and `dart analyze`. |
| 47 | - After changing behavior, run the narrowest relevant `dart test` target. If no |
| 48 | tests exist, add or describe a focused smoke test for the database operation. |
| 49 | - After migration changes, run `dart run drift_dev make-migrations` and the |
| 50 | generated migration tests from the configured `test_dir`. |
| 51 | - After editing this skill, references, or reusable examples, run |
| 52 | `dart-drift/scripts/verify-examples.sh`. |
| 53 | |
| 54 | ## Constraints |
| 55 | |
| 56 | - Keep this skill scoped to Dart CLI, server-side, and non-Flutter desktop apps. |
| 57 | Do not add `StreamBuilder`, Riverpod, Provider, `drift_flutter`, or |
| 58 | Flutter-specific storage patterns here. |
| 59 | - Prefer `dart pub add ...` for new dependencies. Only hardcode versions when |
| 60 | matching an existing repository policy. |
| 61 | - Do not use deprecated PostgreSQL examples based on `HostEndpoint`, |
| 62 | `PgEndpoint`, `postgres_pool.dart`, `postgresUuid()`, `postgresJson()`, |
| 63 | `PostgresTypes`, or `gen_random_uuid`. |
| 64 | - For PostgreSQL, avoid SQLite-only helpers such as `currentDateAndTime` and |
| 65 | most `dateTime()` convenience APIs. Use `PgTypes.date`, |
| 66 | `PgTypes.timestampWithTimezone`, `PgTypes.timestampNoTimezone`, and `now()`. |
| 67 | - Do not replace code generation or migration validation with manual reasoning |
| 68 | unless the user explicitly waives the risk. |
| 69 | |
| 70 | ## Fallback |
| 71 | |
| 72 | If the requested repository lacks enough context to choose a backend, ask |
| 73 | whether the target is SQLite or PostgreSQL. If network, database, or toolchain |
| 74 | access prevents validation, finish with the exact command that failed and the |
| 75 | remaining risk. |