When working with database migrations, automation is the key to speed and safety. Redgate Flyway already helps us structure and deploy migrations consistently, but what if we could add AI-powered checks directly into our existing pipelines and do so in a safe manner as well? I’ve been promoting this as part of my open-source and Postgres event sessions for the last few weeks and I’d like to discuss it further. Recently Ollama offered the option for a Windows 11 installation. No GPU or tokens required, (although having GPUs won’t hurt) I did timed tests and found it to be a solid choice for simple generative AI work when needed locally and even added optimization options for folks who are interested.
Now armed with a local AI and LLM, I began combining PowerShell scripts with to create command line solutions for tasks that incorporated what I was working with, (link to the code below) including Flyway CLI and Postgres. With this post, I’d like to narrow in on how a single, flexible PowerShell pattern can drive code reviews, performance checks, conflict detection, and even release documentation as part of projects, as this is how I often view the code that I write.
I am going to ask for one thing though: no one judge my PowerShell code too harshly, that’s an area I’m still a novice.
Prerequisites
Before jumping in, let’s discuss what you’ll need to get this to work:
- Flyway installed and configured with your database.
- Ollama installed locally (e.g., running ollama and running a qwen2.5 LLM downloaded locally also).
- PowerShell 7.x (though most examples will work in Windows PowerShell 5.1).
- A local database of your preferred flavor. For my example here, I have a Postgres 17.5 database running on my PC.
Base Script: AI-Powered Code Review
I’ve pulled out the code from the overall script, but here’s the minimal script that reviews a SQL migration file. You can add or subtract from the AI prompt to have the review do more or less as you require:
param(
[string]$SqlFile
)
# Load migration script contents
$sqlContent = Get-Content $SqlFile -Raw
# Run AI review with Ollama
$review = & ollama run qwen2.5 "Review this SQL migration for syntax issues, style problems, and possible risks: $sqlContent"
Write-Host "=== AI Review for $SqlFile ==="
Write-Output $review
Run it with:
.\ai-review.ps1 -SqlFile ".\migrations\V1__create_table.sql"
You’ll get natural-language feedback from the local model, flagging potential issues before running flyway migrate. Manual review is expected at this state, but you could add in a parser to look for any file flagged for review or fail, stopping the migration if found in the code review.
Extending the Script for Other Tasks
Because the script is prompt-driven, you only need to change the prompt text to perform a new kind of check. Let’s walk through some examples as we build this out to do more. This is what I really loved about this script. You see how an AI prompt is issued as part of the PowerShell script, so why not take that further and perform other tasks?
1. Performance Impact Check
param(
[string]$SqlFile
)
$sqlContent = Get-Content $SqlFile -Raw
$review = & ollama run qwen2.5 "Analyze the following SQL migration for potential performance issues on large data sets. Suggest indexing, partitioning, or query rewrites if needed: $sqlContent"
Write-Host "=== Performance Analysis for $SqlFile ==="
Write-Output $review
This helps you catch long-running changes (e.g., unindexed ALTER TABLE … ADD COLUMN on massive tables) early. Depending on the platform, you could look for potential lock escalation problems in SQL Server or TX waits or TEMP usage in Oracle, too.
2. Conflict Detection with Existing Schema
param(
[string]$SqlFile,
[string]$SchemaDump
)
$sqlContent = Get-Content $SqlFile -Raw
$schemaContent = Get-Content $SchemaDump -Raw
$review = & ollama run qwen2.5 "Check if the following migration conflicts with the existing schema. Highlight duplicate indexes, conflicting constraints, or dropped columns still in use. Migration: $sqlContent Existing Schema: $schemaContent"
Write-Host "=== Conflict Check for $SqlFile ==="
Write-Output $review
Pair this with a schema export (pg_dump -s for Postgres, expdp (DataPump) of the metadata for Oracle, sqlpackage for SQL Server) to give the model context. The script appears simple, but can be expanded upon to perform platform specific tasks, creating a framework that all platforms can be based from for AI actions and use local AI, offering a more secure method to begin an organization AI testing method from.
3. Standards / Best Practices Validation
param(
[string]$SqlFile
)
$sqlContent = Get-Content $SqlFile -Raw
$review = & ollama run qwen2.5 "Check this SQL migration against best practices: use of transactions, consistent naming conventions, avoiding SELECT *, correct use of primary keys, etc. Return specific issues and recommendations. Script: $sqlContent"
Write-Host "=== Standards Check for $SqlFile ==="
Write-Output $review
This enforces team or company-level guidelines automatically. I don’t know a DBA out there that wouldn’t find this useful and couldn’t build on this to help developers help themselves and create recommended practices. It also offers us a way to identify if AI solutions provide value or if they don’t, while not requiring a lot of time-laden or cost heavy investment.
4. Automatic Documentation Generation
param(
[string]$SqlFile
)
$sqlContent = Get-Content $SqlFile -Raw
$review = & ollama run qwen2.5 "Summarize the following SQL migration in plain English for release notes. Include the affected tables, columns, and purpose of the change: $sqlContent"
Write-Host "=== Release Note for $SqlFile ==="
Write-Output $review
Now your migration can generate its own release note entry and one less tedious/manual task that most of us dislike performing.
Orchestrating with Flyway
As the original script is based off was part of a larger demonstration script suite I wrote, I can’t help but mention, these were all part of steps to make deployments more robust. Any of these scripting options I put in this post could be integrated as scripts into a larger Flyway-driven pipeline. Here’s an example run.ps1 orchestrator, (although not as dynamic as my goal to create in my end product):
param(
[string]$Stage = "All",
[string]$MigrationsPath = ".\migrations"
)
$migrationFiles = Get-ChildItem $MigrationsPath -Filter *.sql
foreach ($file in $migrationFiles) {
if ($Stage -eq "All" -or $Stage -eq "Review") {
.\ai-review.ps1 -SqlFile $file.FullName
}
if ($Stage -eq "Performance") {
.\ai-performance.ps1 -SqlFile $file.FullName
}
if ($Stage -eq "Conflicts") {
.\ai-conflicts.ps1 -SqlFile $file.FullName -SchemaDump ".\schema_dump.sql"
}
if ($Stage -eq "Docs") {
.\ai-docs.ps1 -SqlFile $file.FullName
}
}
if ($Stage -eq "All" -or $Stage -eq "Migrate") {
flyway migrate
}
Once your Flyway project is created, this allows you to call the following from the command line and simply let it do the automation with Flyway as the orchestrator:
.\run.ps1 -Stage Review
.\run.ps1 -Stage All
Depending on if you’re running a specific check or all, you can run different AI-driven checks as part of your release cycle.
Why Local AI Fits This Workflow
Running Ollama locally avoids the two biggest barriers to adopting AI in database workflows:
- Security: SQL scripts and schema never leave your environment.
- Control: You choose the model (Qwen, Llama, Mistral, etc.) and tune prompts without API quotas or vendor lock-in.
- Performance: Depending on the resources available and requirements, you can build out or simplify as needed.
This makes it a practical way to embed AI into CI/CD pipelines where sensitive schema changes are in play.
Worth it
By wrapping Ollama calls inside PowerShell, you unlock a reusable, modular pattern for some of the most common needs we may use expensive tools or consume time manually on:
- Code review – catch mistakes before they run.
- Performance checks – anticipate slowdowns.
- Conflict detection – spot schema collisions.
- Best practices validation – enforce standards.
- Documentation generation – automate release notes.
Each task is simply a different prompt. Combined with Flyway, this becomes a flexible foundation for AI-augmented DevOps pipelines that are secure, local, and adaptable. If you’d like to download the original script suite demo and cheat sheet to get started, feel free to go over to my GitHub zip file and download it. Have fun with it, as I think AI should be fun…and I prefer when it’s fun AND SAFE.