Skip to content

nileshmishra46/SQLDBCompare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

README.md

πŸ” SQLDBCompare

A PHP-based web tool for Database Administrators to detect and resolve schema drift between two Microsoft SQL Server databases β€” via live connection or uploaded .sql DDL files.


πŸ“Œ Why This Tool?

In multi-environment SQL Server setups (Dev β†’ UAT β†’ Production) or multi-tenant deployments, databases are expected to share an identical schema but serve different data. Over time, schema drift occurs β€” a missing index here, an unapplied constraint there β€” causing silent bugs, performance issues, or broken application logic.

SQLDBCompare gives DBAs a fast, reliable way to catch these differences and get a ready-to-run T-SQL fix script in one go.


✨ Features

  • πŸ”Œ Two Input Modes β€” connect live to SQL Server or upload two .sql DDL files
  • πŸ” TrustServerCertificate Support β€” compatible with SQL Server 2019+ and SSMS 19+
  • 🧩 Deep Schema Comparison across all major object types:
    • Tables & Columns (type, nullability, defaults)
    • Primary Keys & Foreign Keys
    • Unique, Check & Default Constraints
    • Non-Clustered & Clustered Indexes (with included columns + filter definitions)
    • Triggers
  • πŸ“Š Colour-Coded HTML Report with per-category tabbed view
  • πŸ“„ Downloadable T-SQL Fix Script β€” review and run directly against your target DB
  • ⚠️ Missing Table Detection β€” flags tables present in one DB but absent in the other
  • πŸ›‘οΈ Security-First β€” parameterised queries, upload validation, XSS-sanitised output

πŸ–₯️ Tech Stack

Layer Technology
Language PHP 8.1+
DB Driver sqlsrv / pdo_sqlsrv (Microsoft)
Frontend HTML5 + Bootstrap 5
Schema Queries SQL Server DMVs & INFORMATION_SCHEMA views
SQL Parsing Custom PHP DDL parser
Output HTML Report + .sql Fix Script
Launcher C# (Launcher.cs) β€” optional desktop launcher
Build PowerShell (build_dist.ps1) β€” distribution packager

πŸ“‹ Prerequisites

PHP Extensions

extension=sqlsrv
extension=pdo_sqlsrv

Linux (Ubuntu/Debian):

sudo apt install php8.1-dev unixodbc-dev
pecl install sqlsrv pdo_sqlsrv

Windows (XAMPP / Laragon): Download php_sqlsrv_81_ts_x64.dll and php_pdo_sqlsrv_81_ts_x64.dll from the Microsoft PHP Driver releases and place them in your PHP ext/ directory.

Microsoft ODBC Driver

Required by the sqlsrv extension. β†’ Install ODBC Driver for SQL Server


πŸš€ Getting Started

# Clone the repository
git clone https://github.com/nileshmishra46/SQLDBCompare.git
cd SQLDBCompare

# Set permissions for upload and output directories
chmod 750 uploads/
chmod 750 output/fix_scripts/

# Configure settings
cp config/settings.example.php config/settings.php

Open config/settings.php and adjust as needed:

return [
    'trust_server_certificate' => true,   // Required for SQL Server 2019+ / SSMS 19+
    'encrypt'                  => true,
    'default_port'             => 1433,
    'upload_max_size_mb'       => 20,
    'comparison_ignore_case'   => true,
];

Serve via Apache/Nginx or PHP's built-in server:

php -S localhost:8080

Then open http://localhost:8080 in your browser.

Windows users: You can also use Launcher.cs to launch the tool as a desktop application, or run build_dist.ps1 to package a distributable release.


πŸ—‚οΈ Project Structure

SQLDBCompare/
β”œβ”€β”€ index.php                  # Mode selector (Live DB or File Upload)
β”œβ”€β”€ connect.php                # Live connection form + handler
β”œβ”€β”€ upload.php                 # .sql file upload form + handler
β”œβ”€β”€ compare.php                # Comparison controller
β”œβ”€β”€ report.php                 # HTML diff report renderer
β”œβ”€β”€ run_tests.php              # Test runner for comparison engine
β”œβ”€β”€ Launcher.cs                # Optional C# desktop launcher (Windows)
β”œβ”€β”€ build_dist.ps1             # PowerShell build & distribution packager
β”œβ”€β”€ .gitignore
β”‚
β”œβ”€β”€ engine/
β”‚   β”œβ”€β”€ DbInspector.php        # Fetches schema via DMVs from live SQL Server
β”‚   β”œβ”€β”€ SqlFileParser.php      # Parses uploaded .sql DDL files
β”‚   β”œβ”€β”€ SchemaComparator.php   # Produces structured diff between two schemas
β”‚   └── ScriptGenerator.php   # Generates T-SQL fix scripts from diff
β”‚
β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ db.php                 # PDO connection helper (TrustServerCertificate)
β”‚   └── settings.php           # App configuration
β”‚
β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ css/style.css
β”‚   └── js/app.js
β”‚
β”œβ”€β”€ uploads/                   # Temp storage for uploaded .sql files
└── output/fix_scripts/        # Generated .sql fix files per run

πŸ”„ How It Works

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Choose Input Mode               β”‚
β”‚  A) Live SQL Server Connection   β”‚
β”‚  B) Upload .sql DDL Files        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚ Schema Fetcher β”‚  ← DbInspector (live) or SqlFileParser (file)
       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚   Comparator   β”‚  ← Compares Source vs Target across all object types
       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                     β”‚
β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ HTML Reportβ”‚   β”‚ T-SQL Fix Script  β”‚
β”‚ (browser)  β”‚   β”‚ (.sql download)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Š What Gets Compared

Object Compared By
Tables Presence in both DBs
Columns Name, data type, length, nullability, default value
Primary Keys Constraint name, column(s), key order
Foreign Keys Constraint name, parent/reference table & column, ON DELETE/UPDATE action
Unique Constraints Constraint name, column(s)
Check Constraints Constraint name, check expression
Default Constraints Constraint name, column, default value expression
Indexes Name, type (clustered/nonclustered), columns, included columns, filter, uniqueness
Triggers Name, parent table, event type, enabled/disabled state

πŸ“„ Sample Fix Script Output

-- Generated by SQLDBCompare
-- Run Date: 2025-06-12
-- Source: ProductionDB | Target: StagingDB
-- WARNING: Review before executing in production

USE [StagingDB];
GO

-- MISSING INDEX: IX_Orders_CustomerId on dbo.Orders
CREATE NONCLUSTERED INDEX [IX_Orders_CustomerId]
ON [dbo].[Orders] ([CustomerId] ASC)
INCLUDE ([OrderDate], [Status]);
GO

-- MISSING FOREIGN KEY: FK_Orders_Customers on dbo.Orders
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_Orders_Customers]
FOREIGN KEY ([CustomerId])
REFERENCES [dbo].[Customers] ([CustomerId])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

πŸ›‘οΈ Security

  • All schema queries use SQL Server system views β€” no user input is interpolated into SQL
  • Credentials are held only in the PHP session and never logged or stored
  • Uploaded files are validated by extension and MIME type, stored outside the web root
  • All DB object names are htmlspecialchars() escaped before rendering in the report

πŸ—ΊοΈ Roadmap

  • Stored procedure & view comparison
  • Three-way compare (Dev / UAT / Prod)
  • Email report delivery
  • Scheduled nightly comparison with drift alerts
  • Apply fix script directly to Target DB (with confirmation step)
  • Comparison history log (SQLite)
  • REST API mode (JSON in / JSON out)

🀝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you'd like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/stored-proc-compare)
  3. Commit your changes (git commit -m 'Add stored procedure comparison')
  4. Push to the branch (git push origin feature/stored-proc-compare)
  5. Open a Pull Request

Screenshots

Screenshot 2026-06-12 003517 Screenshot 2026-06-12 004948 Screenshot 2026-06-12 003824 Screenshot 2026-06-12 004218 Screenshot 2026-06-12 004255 Screenshot 2026-06-12 004511 Screenshot 2026-06-12 004543

πŸ‘€ Author

Nilesh Mishra β€” Database Administrator

About

This is a web based SQL Server database schema comparison tool, it can be used on live systems or can work with SSMS generated database schema for comparision

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors