mirror of
https://github.com/elicpeter/nyx.git
synced 2026-06-09 19:45:13 +02:00
105 lines
3.2 KiB
Rust
105 lines
3.2 KiB
Rust
//! Phase 21 (Track M.3) — Prisma migration adapter (JS / TS).
|
|
//!
|
|
//! Prisma migrations are SQL files generated by `prisma migrate dev`
|
|
//! plus a TS / JS seed script that calls `prisma.$executeRaw`. Fires
|
|
//! when the surrounding source imports `@prisma/client` and the
|
|
//! function body invokes one of the raw-execution callees.
|
|
|
|
use crate::dynamic::framework::{FrameworkAdapter, FrameworkBinding};
|
|
use crate::evidence::EntryKind;
|
|
use crate::summary::FuncSummary;
|
|
use crate::symbol::Lang;
|
|
|
|
pub struct MigrationPrismaAdapter;
|
|
|
|
const ADAPTER_NAME: &str = "migration-prisma";
|
|
|
|
fn callee_is_prisma_migration(name: &str) -> bool {
|
|
let last = name.rsplit_once('.').map(|(_, s)| s).unwrap_or(name);
|
|
matches!(
|
|
last,
|
|
"$executeRaw"
|
|
| "$executeRawUnsafe"
|
|
| "$queryRaw"
|
|
| "$queryRawUnsafe"
|
|
| "migrate"
|
|
| "deploy"
|
|
| "up"
|
|
)
|
|
}
|
|
|
|
fn source_imports_prisma_migration(file_bytes: &[u8]) -> bool {
|
|
const NEEDLES: &[&[u8]] = &[
|
|
b"@prisma/client",
|
|
b"require('@prisma/client')",
|
|
b"require(\"@prisma/client\")",
|
|
b"from '@prisma/client'",
|
|
b"from \"@prisma/client\"",
|
|
b"prisma.$executeRaw",
|
|
b"prisma.$queryRaw",
|
|
b"PrismaClient",
|
|
];
|
|
NEEDLES
|
|
.iter()
|
|
.any(|n| file_bytes.windows(n.len()).any(|w| w == *n))
|
|
}
|
|
|
|
impl FrameworkAdapter for MigrationPrismaAdapter {
|
|
fn name(&self) -> &'static str {
|
|
ADAPTER_NAME
|
|
}
|
|
|
|
fn lang(&self) -> Lang {
|
|
Lang::JavaScript
|
|
}
|
|
|
|
fn detect(
|
|
&self,
|
|
summary: &FuncSummary,
|
|
_ast: tree_sitter::Node<'_>,
|
|
file_bytes: &[u8],
|
|
) -> Option<FrameworkBinding> {
|
|
let matches_call = super::any_callee_matches(summary, callee_is_prisma_migration);
|
|
let matches_source = source_imports_prisma_migration(file_bytes);
|
|
if matches_call || matches_source {
|
|
Some(FrameworkBinding {
|
|
adapter: ADAPTER_NAME.to_owned(),
|
|
kind: EntryKind::Migration { version: None },
|
|
route: None,
|
|
request_params: Vec::new(),
|
|
response_writer: None,
|
|
middleware: Vec::new(),
|
|
})
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn parse_js(src: &[u8]) -> tree_sitter::Tree {
|
|
let mut parser = tree_sitter::Parser::new();
|
|
let lang = tree_sitter::Language::from(tree_sitter_javascript::LANGUAGE);
|
|
parser.set_language(&lang).unwrap();
|
|
parser.parse(src, None).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn fires_on_prisma_raw_migration() {
|
|
let src: &[u8] = b"const { PrismaClient } = require('@prisma/client');\nconst prisma = new PrismaClient();\n\
|
|
async function up(name) { await prisma.$executeRawUnsafe('CREATE TABLE ' + name); }\n";
|
|
let tree = parse_js(src);
|
|
let summary = FuncSummary {
|
|
name: "up".into(),
|
|
..Default::default()
|
|
};
|
|
let binding = MigrationPrismaAdapter
|
|
.detect(&summary, tree.root_node(), src)
|
|
.expect("prisma migration binds");
|
|
assert_eq!(binding.adapter, "migration-prisma");
|
|
assert!(matches!(binding.kind, EntryKind::Migration { .. }));
|
|
}
|
|
}
|