โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโ โโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโ โโโ โ
โ โโโโโโโโโโโโโโโ โโโโโโ โโโ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโ โโโโโโโโ โโโโโโโ โ
โ โ
โ ๐๏ธ SPEC-DRIVEN DB MIGRATION ๐๏ธ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
MISSION: Reverse-engineer a LEGACY DATABASE BEHEMOTH with Spec2Cloud! Extract 80+ stored procedures, decode 30+ triggers, and migrate to modern cloud โ WITHOUT LOSING A SINGLE BUSINESS RULE! ๐๐๏ธ
๐พ BUSINESS DOMAIN: GreenHarvest Agricultural Cooperative
๐ CATEGORY: Spec-Driven Development
โก PRIORITY: P3
๐ ๏ธ TECH STACK: SQL Server โข PostgreSQL โข Python โข Spec2Cloud
๐ข ORG: EmeaAppGbb
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ BEFORE (SQL Server 2012) ๐๏ธ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ 120+ tables (decades of schema evolution)โ
โ ๐ฎ 80+ stored procedures (hidden logic) โ
โ โก 30+ triggers (implicit dependencies) โ
โ ๐งฎ Computed columns (complex formulas) โ
โ ๐ Cross-DB references (commodity pricing) โ
โ ๐
SQL Agent jobs for batch sync โ
โ ๐คท Business rules? Only the DB knows... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AFTER (Cloud PostgreSQL) โ๏ธโจ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ Complete spec: every table, proc, triggerโ
โ ๐ง Business rules extracted & documented โ
โ ๐ Modern PostgreSQL schema โ
โ ๐ Python services (from stored procs) โ
โ โ
Validated migration (100% coverage) โ
โ ๐ Living documentation (Spec2Cloud spec) โ
โ ๐ฏ Zero business logic lost! โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Defeat these database migration demons:
| ๐พ BOSS | ๐ ATTACK PATTERN | โ๏ธ YOUR WEAPON |
|---|---|---|
| The Stored Proc Fortress | 80+ procedures hiding business logic | Spec2Cloud extraction |
| The Trigger Tangle | Implicit execution order dependencies | Specification mapping |
| The Computed Column Chaos | Complex formulas everywhere | Formula documentation |
| The Cross-DB Dragon | Linked servers and remote queries | Spec-driven refactor |
| The Lost Knowledge | "Only Bob knows how this works" | Automated reverse engineering |
| The Validation Void | Did we migrate everything? ๐คท | Spec-generated test suite |
๐ Explore Legacy DB โ Review schema, run stored procs
๐ Schema Analysis โ Spec2Cloud extracts tables & relationships
๐ Business Rule Extraction โ Analyze procs, triggers, UDFs
๐ Generate Migration Spec โ Complete specification with targets
๐ Create Target Schema โ PostgreSQL DDL from spec
๐ Extract Logic โ Python services from stored procedures
๐ Migrate Data โ Execute data migration scripts
๐ Replace Triggers โ Event handlers or PostgreSQL triggers
๐ Validate โ Spec-generated validation queries
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ SOUND EFFECTS: โ
โ โ
โ ๐ฌ "STORED PROCS ANALYZED!" โ
โ ๐ง "BUSINESS RULES EXTRACTED!" โ
โ ๐ "SCHEMA MIGRATED!" โ
โ ๐ "TRIGGER DECODED!" โ
โ ๐ "POSTGRESQL SCHEMA LIVE!" โ
โ โ
"VALIDATION PASSED!" โ
โ ๐ฏ "ZERO DATA LOSS!" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Complete this migration and gain:
- ๐ฌ Database Reverse Engineering โ Spec2Cloud mastery
- ๐ง Business Rule Extraction โ From stored procedures
- ๐ Spec-Driven Migration โ Spec = plan + acceptance criteria
- ๐ PostgreSQL Migration โ Modern cloud database
- ๐ Stored Proc โ Python โ Extract logic to services
- โก Trigger Replacement โ Event-driven architecture
- โ Migration Validation โ Automated completeness checks
Estimated Duration: 5โ7 hours โณ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ฅ BRONZE: Schema migrated โ
โ ๐ฅ SILVER: +All business rules extractedโ
โ ๐ฅ GOLD: +100% validation passingโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Gear up for migration:
- โ SQL Server experience (T-SQL, stored procedures, triggers)
- โ Basic PostgreSQL knowledge
- โ Python experience (for extracted logic)
- โ Docker Desktop (for SQL Server + PostgreSQL)
greenharvest-db/
โโโ Schema/
โ โโโ Tables/
โ โ โโโ CropManagement/ # ๐พ 30+ crop tables
โ โ โโโ Inventory/ # ๐ฆ 25+ inventory tables
โ โ โโโ Members/ # ๐ฅ 15+ member tables
โ โ โโโ Trading/ # ๐ฐ 20+ trading tables
โ โ โโโ Reference/ # ๐ 30+ reference tables
โ โโโ StoredProcedures/
โ โ โโโ ๐ฑ CropPlanning/ # Rotation & planning
โ โ โโโ ๐ Inventory/ # Stock management
โ โ โโโ ๐ต Pricing/ # Price calculations
โ โ โโโ ๐งฎ Settlement/ # Coop settlements
โ โ โโโ ๐ Reporting/ # Report data
โ โโโ Triggers/
โ โ โโโ ๐ AuditTriggers/ # Change tracking
โ โ โโโ ๐ก๏ธ IntegrityTriggers/ # Business rules
โ โ โโโ ๐ SyncTriggers/ # Data sync
โ โโโ Functions/
โ โโโ ๐งฎ Scalar/ # Calculations
โ โโโ ๐ TableValued/ # Complex filtering
โโโ Specs/ # โ SPEC2CLOUD OUTPUT!
โ โโโ ๐ schema-spec/
โ โโโ ๐ง business-rules/
โ โโโ ๐บ๏ธ migration-plan/
โ โโโ โ
validation/
โโโ Migration/
โโโ ๐ PostgreSQL/ # โ YOUR TARGET!
โ โโโ schema.sql
โ โโโ data-migration.sql
โโโ ๐ Python/
โโโ services/ # Extracted logic
main (๐ Complete lab)
โ
โโโโโโโโโโโดโโโโโโโฌโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโ
legacy step-1 step-2 step-3 step-4 step-5
(๐๏ธ) โ โ โ โ โ
Schema Business Migration Target Validate
Analysis Rules Spec Impl
Migration Phases:
- ๐ Schema Analysis โ Reverse-engineer 120+ tables
- ๐ง Business Rule Extraction โ 80+ stored procs decoded
- ๐ Migration Spec โ Complete specification generated
- ๐ Target Implementation โ PostgreSQL + Python services
- โ Validation โ Prove migration completeness
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 120+ tables โ
โ 80+ stored procedures โ
โ 30+ triggers โ
โ 20+ user-defined functions โ
โ 40+ views (some indexed) โ
โ dozens of computed columns โ
โ decades of business knowledge ๐ง โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- ๐ Crop rotation calculated by soil type + previous crops
- ๐ก๏ธ Planting dates enforced by growing season constraints
- ๐ Yield per acre computed from harvest data
- โ๏ธ Unit conversions (bushels โ tonnes โ hundredweight)
- ๐ฐ Settlement calculations based on grade/quality matrices
- โ SQL Server DB created with 120+ tables
- โ Stored procedures execute with sample data
- โ Triggers fire and enforce business rules
- โ Spec2Cloud generates complete schema spec
- โ Business rules extracted from stored procs
- โ PostgreSQL schema created from spec
- โ Python services replicate stored proc logic
- โ Data migration completes successfully
- โ Validation queries pass (100% coverage)
- โ APPMODLAB.md complete with frontmatter
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Run spec-generated validation tests: โ
โ โ
โ โ
Table count matches โ
โ โ
Row counts match โ
โ โ
Computed values match โ
โ โ
Business logic produces same results โ
โ โ
Triggers enforce same rules โ
โ โ
โ 100% PASS = MIGRATION COMPLETE! ๐ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
YOU DEFEATED:
- ๐๏ธ Legacy SQL Server 2012
- ๐ฎ Hidden business logic in stored procs
- โก Implicit trigger dependencies
- ๐งฎ Undocumented computed columns
- ๐คท "Tribal knowledge" syndrome
YOU UNLOCKED:
- ๐ Complete database specification
- ๐ง Documented business rules
- ๐ Modern PostgreSQL schema
- ๐ Extracted Python services
- โ Validated, lossless migration
- ๐ Living documentation!
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ ๐ฎ DATABASE MIGRATION MASTERED ๐ฎ โ
โ โ
โ From chaos to clarity! ๐ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ต Now playing: Data Dreams by Spec Master ๐ฟ
๐ฌ Status: 120 TABLES ANALYZED โ ZERO BUSINESS RULES LOST!
๐ Full lab guide: See APPMODLAB.md for spec-driven migration walkthrough
๐ Credits: Built with ๐๏ธ by EmeaAppGbb
Complete step-by-step execution using GitHub Copilot CLI (
gh copilot).
All outputs are on thesolution-finalbranch underassets/outputs/.
cd C:\code\gbb\labs\appmodlab-spec-driven-database-migration
git checkout main && git checkout -b solution-final
New-Item -ItemType Directory -Force -Path assets/outputsTag: step-01-explore-legacy-db ยท Output: assets/outputs/step-01-explore-legacy-db.md
gh copilot -- -p "Analyze the GreenHarvest Agricultural Co-op legacy SQL Server database in this repository. Explore Schema/ directory (Tables, StoredProcedures, Functions, Triggers, Views), Data/ (SeedData, SampleData), Specs/, and Migration/ folders. Produce a comprehensive analysis document at assets/outputs/step-01-explore-legacy-db.md covering: 1) Complete inventory of all database objects (6 tables across CropManagement, Members, Inventory, Trading schemas), 2) Column details with data types, constraints, defaults, indexes, 3) Foreign key relationships and referential integrity map, 4) Stored procedures with business rule extraction, 5) Scalar function fn_CalculateYieldBushels with conversion factors, 6) Trigger tr_AuditHarvestChanges behavior and audit log structure, 7) View vw_FieldProductivity definition and joins, 8) Data patterns from seed/sample data, 9) SQL Server-specific features (GEOGRAPHY, IDENTITY, MONEY, computed columns, PERSISTED, GETDATE, SUSER_SNAME, FOR JSON AUTO)." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-01.txt"
git add assets/outputs/step-01* && git commit -m "Step 01: Explore Legacy Database" && git tag step-01-explore-legacy-dbTag: step-02-generate-db-spec ยท Output: assets/outputs/step-02-database-specification.md
gh copilot -- -p "Generate a comprehensive database specification document for the GreenHarvest Agricultural Co-op SQL Server database. Read all files in Schema/, Data/, and Specs/. Create a detailed spec at assets/outputs/step-02-database-specification.md with: complete table catalog (every column, type, constraint), PK/FK relationships, index inventory, stored procedure signatures with business rules, scalar function spec, trigger spec, view definition, cross-reference matrix, and SQL Server-specific feature catalog." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-02.txt"
git add assets/outputs/step-02* && git commit -m "Step 02: Generate Database Specification" && git tag step-02-generate-db-specTag: step-03-analyze-complexity ยท Output: assets/outputs/step-03-migration-complexity.md
gh copilot -- -p "Analyze the migration complexity for migrating the GreenHarvest SQL Server database to PostgreSQL. Create assets/outputs/step-03-migration-complexity.md with: per-table/proc complexity ratings (Low/Medium/High/Critical), SQL Server to PostgreSQL feature mapping table, identified blockers and risks, effort estimation per object, recommended migration order, and risk mitigation strategies." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-03.txt"
git add assets/outputs/step-03* && git commit -m "Step 03: Analyze Migration Complexity" && git tag step-03-analyze-complexityTag: step-04-design-target-schema ยท Output: assets/outputs/step-04-target-schema.md
gh copilot -- -p "Design the modern target PostgreSQL schema for the GreenHarvest database migration. Create assets/outputs/step-04-target-schema.md with: design principles, schema mapping to snake_case, complete target DDL for all 6 tables, data type conversion table, PL/pgSQL functions, PostgreSQL trigger functions, view definitions, index strategy, and PostGIS extension requirements." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-04.txt"
git add assets/outputs/step-04* && git commit -m "Step 04: Design Target Schema" && git tag step-04-design-target-schemaTag: step-05-migration-scripts ยท Output: assets/outputs/step-05-migration-scripts.md
gh copilot -- -p "Generate complete migration scripts for the GreenHarvest SQL Server to PostgreSQL migration. Create assets/outputs/step-05-migration-scripts.md with: DDL migration (CREATE statements, indexes, constraints, PostGIS), PL/pgSQL functions, audit trigger, view creation, DML seed data conversion, stored procedure migration to PL/pgSQL, rollback scripts, and execution order." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-05.txt"
git add assets/outputs/step-05* && git commit -m "Step 05: Generate Migration Scripts" && git tag step-05-migration-scriptsTag: step-06-data-validation ยท Output: assets/outputs/step-06-data-validation.md
gh copilot -- -p "Create comprehensive data validation queries for the GreenHarvest database migration. Create assets/outputs/step-06-data-validation.md with: row count validation, schema validation, computed column verification, FK integrity checks, index existence verification, stored procedure output comparison, trigger behavior validation, view output comparison, seed data verification, and edge case tests." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-06.txt"
git add assets/outputs/step-06* && git commit -m "Step 06: Create Data Validation" && git tag step-06-data-validationTag: step-07-migration-plan ยท Output: assets/outputs/step-07-migration-plan.md
gh copilot -- -p "Create a comprehensive migration plan for the GreenHarvest SQL Server to PostgreSQL migration. Create assets/outputs/step-07-migration-plan.md with: executive summary, pre-migration phase (docker-compose setup), migration phases with timeline, rollback procedures, risk register, validation checklist, communication plan, post-migration tasks, and go/no-go decision criteria." --allow-all-tools --yolo 2>&1 | Tee-Object -FilePath "assets/outputs/step-07.txt"
git add assets/outputs/step-07* && git commit -m "Step 07: Document Migration Plan" && git tag step-07-migration-plangit push origin solution-final --tags| Step | Output File | Description |
|---|---|---|
| 01 | step-01-explore-legacy-db.md |
Legacy database exploration and analysis |
| 02 | step-02-database-specification.md |
Comprehensive database specification |
| 03 | step-03-migration-complexity.md |
Migration complexity assessment |
| 04 | step-04-target-schema.md |
Target PostgreSQL schema design |
| 05 | step-05-migration-scripts.md |
DDL + DML migration scripts |
| 06 | step-06-data-validation.md |
Data validation queries and procedures |
| 07 | step-07-migration-plan.md |
Full migration plan with rollback |
๐ May your schemas be normalized and your migrations be lossless! ๐