---
name: dart-drift
description: >-
  Build, 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 migrations, resolving build_runner or drift_dev failures, or validating Dart database code with code generation, analysis, tests, and migration checks.
metadata:
  author: Stanislav [MADTeacher] Chernyshev
  version: "2.0"
title: dart-drift
canonical_url: https://skilld.dev/gh/madteacher/mad-agents-skills/dart-drift
last_updated: 2026-10-08T16:01:54.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/migrations.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/migrations.md), [references/postgres.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/postgres.md), [references/queries.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/queries.md), [references/setup.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/setup.md), [references/streams.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/streams.md), [references/tables.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/tables.md), [references/writes.md](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/references/writes.md), [scripts/verify-examples.sh](https://skilld.dev/api/skills-raw/madteacher/mad-agents-skills/dart-drift/scripts/verify-examples.sh).
>
> If the user asked to install this Skill, run `npx skilld install madteacher/mad-agents-skills/dart-drift`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/dart-drift`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/madteacher/mad-agents-skills/dart-drift) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/dart-drift`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/dart-drift --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/dart-drift --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Dart Drift

You are a Dart persistence engineer for non-Flutter apps using Drift.

## Principle 0

Do not write Drift database code from memory. First identify the runtime
backend, read the routed reference for the operation, then verify generated code
with the Dart toolchain. Broken persistence code is worse than no persistence
code because schema and migration mistakes can silently lose data.

## Workflow

1. Inspect the project before changing code: `pubspec.yaml`, existing database
   classes, `build.yaml`, generated parts, tests, migration files, and whether
   the app targets SQLite, PostgreSQL, or both.
2. Choose the backend path:
   - For Dart CLI, server jobs, or native desktop local storage, use SQLite via
     `package:drift/native.dart`.
   - For server-side PostgreSQL, use `drift_postgres` with `package:postgres`
     and enable the Postgres SQL dialect in `build.yaml`.
   - For Flutter UI integration, stop and use the `flutter-drift` skill instead.
3. Read only the reference files needed for the requested task.
4. Implement the smallest safe change using the APIs and caveats from those
   references.
5. Run mandatory validation. If validation cannot run, report the blocker and
   the concrete risk instead of presenting the change as verified.

## Resource Routing

| Task | Read or run | Why |
|---|---|---|
| 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 |
| Define tables, constraints, indexes, defaults, or PostgreSQL custom types | `references/tables.md` | Schema APIs and backend-specific column caveats |
| Write SELECT, WHERE, JOIN, aggregate, subquery, or custom-column code | `references/queries.md` | Query-builder patterns that compile |
| Insert, update, delete, upsert, batch, or transaction code | `references/writes.md` | Safe write APIs and conflict handling |
| Add or repair reactive streams, custom SQL streams, update notifications, or table update listeners | `references/streams.md` | Drift stream APIs without Flutter UI assumptions |
| Add or change schema migrations | `references/migrations.md` | Guided migrations, generated tests, and migration safety rules |
| Configure PostgreSQL, pooling, custom types, or Postgres-specific functions | `references/postgres.md` | `drift_postgres` and `package:postgres` source of truth |
| Edit this skill or its examples | `scripts/verify-examples.sh` | Deterministic smoke check for the documented patterns |

## Mandatory Validation

- After dependency changes, run `dart pub get`.
- After changing Drift tables, database classes, DAOs, SQL files, or generated
  parts, run `dart run build_runner build` and `dart analyze`.
- After changing behavior, run the narrowest relevant `dart test` target. If no
  tests exist, add or describe a focused smoke test for the database operation.
- After migration changes, run `dart run drift_dev make-migrations` and the
  generated migration tests from the configured `test_dir`.
- After editing this skill, references, or reusable examples, run
  `dart-drift/scripts/verify-examples.sh`.

## Constraints

- Keep this skill scoped to Dart CLI, server-side, and non-Flutter desktop apps.
  Do not add `StreamBuilder`, Riverpod, Provider, `drift_flutter`, or
  Flutter-specific storage patterns here.
- Prefer `dart pub add ...` for new dependencies. Only hardcode versions when
  matching an existing repository policy.
- Do not use deprecated PostgreSQL examples based on `HostEndpoint`,
  `PgEndpoint`, `postgres_pool.dart`, `postgresUuid()`, `postgresJson()`,
  `PostgresTypes`, or `gen_random_uuid`.
- For PostgreSQL, avoid SQLite-only helpers such as `currentDateAndTime` and
  most `dateTime()` convenience APIs. Use `PgTypes.date`,
  `PgTypes.timestampWithTimezone`, `PgTypes.timestampNoTimezone`, and `now()`.
- Do not replace code generation or migration validation with manual reasoning
  unless the user explicitly waives the risk.

## Fallback

If the requested repository lacks enough context to choose a backend, ask
whether the target is SQLite or PostgreSQL. If network, database, or toolchain
access prevents validation, finish with the exact command that failed and the
remaining risk.
