Making Technology Bulletproof

AIRedgate

That Code Review with OLlama and Qwen 2.5

I just posted on how to use a PowerShell script in a flexible manner to perform different tasks with generative AI locally with Ollama, but I realized I hadn’t gone into just how cool and simple it was to run this on my laptop, along with the beauty of the script output.

Pulling from the Demo

For the demo scenario, the following exists:

  • A Postgres 17.5 database, source and target for a migration.
  • Four migration scripts requiring code review for the following:
    • Syntax
    • Row Level Security issues inside the database (RLS referenced in the prompt)
    • Missing index options
  • Flyway CLI is configured and in use for the databases and will be used as the orchestrator.

There is a series of scripts and the parameters/variables are set beforehand for the postgres user password, Flyway path, database connection info, etc.

The ai-review.ps1 script, called as part of the parent script, is written as follows:

param(
[string]$HostName,
[int]$Port,
[string]$Database,
[string]$User,
[string]$Password
)

# AI-assisted review using Ollama 

$ollama = Get-Command ollama -ErrorAction SilentlyContinue
if (-not $ollama) {
Write-Host "Ollama not found. Skipping AI review." -ForegroundColor DarkYellow
exit 0
}

$model = $env:AI_REVIEW_MODEL
if (-not $model) { $model = "qwen2.5-coder" } # choose any local model you have

#Gather SQL diffs(new files)

$files = Get-ChildItem ../sql -Filter "V__.sql" | Sort-Object Name
$summary = @()
foreach($f in $files){
$content = Get-Content $f.FullName -Raw
$summary += "### File: $($f.Name)n````sqln$content`n````"
}
$prompt = @"
You are an expert database reviewer. Analyze these PostgreSQL migration files for:

- security issues (e.g., missing RLS, overly broad grants)-peformance pitfalls (missing indexes, risky scans) -rollback/reversibility notes -correctness and deployment risks

Respond with concise bullet points and a PASS/REVIEW flag per file.
$($summary -join "nn")
"@

Write-Host "Running AI review with model '$model'…" -ForegroundColor Cyan
$result = & ollama run $model $prompt
$result | Set-Content -Path ./ai_review_report.md
Write-Host "AI review written to ./ai_review_report.md" -ForegroundColor Green

If you read through the script, once the parameters are set, the script goes in and performs a number of checks to verify that Ollama and the LLM can be found, as well as the DDL files.  It then tells Ollama as part of the AI prompt that it is an “expert database reviewer” and as part of this Postgres DDL review, it will check for the list I discussed in the first section of this post.  It then will print the results in a report named “ai_review_report.md”.

The End-to-End

What does the execution of the parent script, in it’s full end-to-end look like?

As this is part of a larger scripting scenario, notice it tests connectivity, performs the code review, outputs the report, verifies integrity, runs the Flyway migrations, performs some smoke tests and then looks at some top queries on the Postgres cluster after testing the new code that’s been released.  

AI Code Review Report

The report, which is the goal of the ai-review.ps1 is the most important part, as that is the focus of this post:

The Ollama AI and Qwen 2.5 LLM used the prompt posed to it and created a summary report with information on each of the four scripts, checking on security, performance, rollback/reversibility and correctness, then granted a FAIL, REVIEW or PASS status flag to each of the scripts.  If any of the scripts had received a FAIL status, the migration would have exited and rolled back as part of the migration requirements.

In Summary

AI can do a lot of things, but rarely do we ask if it should or explain how it solves a problem that plagues us every day.  Code reviews is a demand on many developer and DBAs time that can be offloaded to AI quite easily and in a way that doesn’t require a lot of GPU or tokens to perform when done with a lightweight AI such as Ollama with a local LLM.

Kellyn

http://about.me/dbakevlar