Skip to content
Closed
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
11 changes: 11 additions & 0 deletions .github/workflows/node-prisma-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ jobs:
- name: Build package
run: npm run build

- name: Cache dsql-lint
id: cache-dsql-lint
uses: actions/cache@v4
with:
path: ~/.cargo/bin/dsql-lint
key: dsql-lint-0.1.2

- name: Install dsql-lint
if: steps.cache-dsql-lint.outputs.cache-hit != 'true'
run: cargo install dsql-lint@0.1.2

- name: Download Amazon Root Cert for SSL
run: wget -O "$SSL_CERT_FILE" https://www.amazontrust.com/repository/AmazonRootCA1.pem

Expand Down
52 changes: 25 additions & 27 deletions node/prisma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ CLI tools for using [Prisma ORM](https://www.prisma.io/) with [Amazon Aurora DSQ
This package provides:

1. **Schema Validator** - Validates Prisma schemas for DSQL compatibility
2. **Migration Transformer** - Converts Prisma migrations to DSQL-compatible SQL
3. **All-in-one Migrate Command** - Validates, generates, and transforms in one step
2. **Migration Transformer** - Converts Prisma migrations to DSQL-compatible SQL using [`dsql-lint`](https://github.com/awslabs/aurora-dsql-tools/tree/main/dsql-lint)
3. **Migration Linter** - Checks SQL migrations for DSQL compatibility without modifying them
4. **All-in-one Migrate Command** - Validates, generates, and transforms in one step

Aurora DSQL has [specific PostgreSQL compatibility limitations](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-postgresql-compatibility-unsupported-features.html). These tools help you catch issues early and automate the required transformations.

Expand All @@ -25,6 +26,16 @@ npm install --save-dev @aws/aurora-dsql-prisma-tools

**Supported Node.js versions:** 20+ (Active and LTS releases)

### Prerequisites

This package requires [`dsql-lint`](https://github.com/awslabs/aurora-dsql-tools/tree/main/dsql-lint) for SQL transformation and linting. Install it via:

```bash
cargo install dsql-lint
```

You can also set the `DSQL_LINT_PATH` environment variable to point to the binary location.

## Quick Start

Generate a DSQL-compatible migration in one command:
Expand All @@ -47,17 +58,7 @@ npx aurora-dsql-prisma validate prisma/schema.prisma

#### What the Validator Checks

| Check | Type | DSQL Limitation |
| -------------------------------------- | ------- | ------------------------------- |
| Missing `relationMode = "prisma"` | Error | Foreign keys not supported |
| `autoincrement()` | Error | Sequences not supported |
| `@db.Serial` | Error | Sequences not supported |
| `@db.SmallSerial` | Error | Sequences not supported |
| `@db.BigSerial` | Error | Sequences not supported |
| `@@fulltext` | Error | Full-text indexes not supported |
| `Int @id` without autoincrement | Warning | Manual ID management needed |
| `BigInt @id` | Warning | Typically requires sequences |
| `gen_random_uuid()` without `@db.Uuid` | Warning | Should use proper UUID type |
The validator checks that `relationMode = "prisma"` is set in the datasource block (DSQL does not support foreign keys). All other SQL compatibility checks are delegated to [`dsql-lint`](https://github.com/awslabs/aurora-dsql-tools/tree/main/dsql-lint) — the validator generates SQL from your schema and lints it. See the [dsql-lint README](https://github.com/awslabs/aurora-dsql-tools/tree/main/dsql-lint) for the full list of rules.

#### Example Output

Expand All @@ -79,7 +80,7 @@ Transform Prisma-generated migrations to be DSQL-compatible:
# Transform from file
npx aurora-dsql-prisma transform raw.sql -o migration.sql

# Transform using pipes
# Transform using pipes (stdin)
npx prisma migrate diff \
--from-empty \
--to-schema prisma/schema.prisma \
Expand All @@ -88,11 +89,15 @@ npx prisma migrate diff \

#### What the Transformer Does

| Transformation | Reason |
| ----------------------------------------------- | ----------------------------------------------------- |
| Wraps each statement in `BEGIN/COMMIT` | DSQL requires one DDL statement per transaction |
| Converts `CREATE INDEX` to `CREATE INDEX ASYNC` | DSQL requires asynchronous index creation |
| Removes foreign key constraints | DSQL requires application-layer referential integrity |
The transform command uses [`dsql-lint --fix`](https://github.com/awslabs/aurora-dsql-tools/tree/main/dsql-lint) to apply DSQL compatibility fixes. See the [dsql-lint README](https://github.com/awslabs/aurora-dsql-tools/tree/main/dsql-lint) for the full list of rules and transformations.

### Lint Migrations

Check a SQL migration file for DSQL compatibility without applying fixes:

```bash
npx aurora-dsql-prisma lint migration.sql
```

### All-in-One Migrate

Expand Down Expand Up @@ -124,14 +129,7 @@ This requires a `prisma.config.ts` that provides database credentials. See the [

### Handling Unsupported Statements

Sometimes Prisma generates `DROP CONSTRAINT` statements when comparing against a live database. DSQL doesn't support `DROP CONSTRAINT`, so use `--force` to skip these if the constraint isn't actually changing:

```bash
npx aurora-dsql-prisma migrate prisma/schema.prisma \
-o prisma/migrations/002_add_email/migration.sql \
--from-config-datasource \
--force
```
Sometimes Prisma generates `DROP CONSTRAINT` statements when comparing against a live database. DSQL doesn't support `DROP CONSTRAINT`. If `dsql-lint` reports unfixable errors, review its output and manually adjust the migration.

## Prisma Schema Requirements

Expand Down
3 changes: 2 additions & 1 deletion node/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
"validate": "tsx src/cli/index.ts validate",
"dsql-transform": "tsx src/cli/index.ts transform",
"dsql-migrate": "tsx src/cli/index.ts migrate"
"dsql-migrate": "tsx src/cli/index.ts migrate",
"dsql-lint": "tsx src/cli/index.ts lint"
},
"keywords": [
"prisma",
Expand Down
48 changes: 48 additions & 0 deletions node/prisma/src/cli/dsql-lint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { spawnSync } from "child_process";
import * as fs from "fs";
import * as path from "path";

function findDsqlLint(): string {
const envPath = process.env["DSQL_LINT_PATH"];
if (envPath) {
if (!fs.existsSync(envPath)) {
throw new Error(
`DSQL_LINT_PATH points to '${envPath}' which does not exist`,
);
}
return envPath;
}

const pathDirs = (process.env["PATH"] ?? "").split(path.delimiter);
for (const dir of pathDirs) {
const candidate = path.join(dir, "dsql-lint");
if (fs.existsSync(candidate)) {
return candidate;
}
}

throw new Error(
"dsql-lint not found. Install it:\n" +
" cargo install dsql-lint\n" +
"Or set DSQL_LINT_PATH to the binary location.",
);
}

export interface DsqlLintResult {
exitCode: number;
stderr: string;
}

export function runDsqlLint(args: string[]): DsqlLintResult {
const binary = findDsqlLint();
const result = spawnSync(binary, args, { encoding: "utf-8" });

if (result.error) {
throw new Error(`Failed to execute dsql-lint: ${result.error.message}`);
}

return {
exitCode: result.status ?? 1,
stderr: result.stderr ?? "",
};
}
Loading
Loading