Blog Post

AI - Machine Learning Blog
8 MIN READ

Automating Database Migration with Autogen 0.4

antonslutsky's avatar
antonslutsky
Icon for Microsoft rankMicrosoft
Mar 02, 2025

The article provides an example of converting Sybase stored procedure code to PostgreSQL syntax using Autogen 0.4. The process involves installing the Autogen Python library, configuring conversational agents, defining a high-level prompt, executing the Autogen agent conversation, and evaluating the agents' execution and results. The AI-powered agents iteratively address technical issues and modify execution code to successfully convert Sybase stored procedure code to PostgreSQL syntax, conduct comprehensive unit testing, and deploy the generated stored procedure in the target database without human intervention.

Introduction

Modern multi-agent AI systems use frameworks that allow for the orchestration of complex tasks through specialized agents.  Autogen is a Microsoft Research project designed to enhance scalability and efficiency of AI systems by leveraging layered and event-driven architecture.

The framework relies on a three-layer architecture:

  • The Core Layer acts as the fundamental infrastructure, handling basic communication and resource management.
  • The AgentChat Layer provides high-level interaction capabilities.
  • The Extensions Layer enables specialized functionalities.

The Autogen project has seen significant interest given its potential to revolutionize AI development by moving beyond single-model systems to collaborative AI, where multiple specialized agents work together to solve complex problems.

Overview

One of the standout features of Autogen is its chat agents, which facilitate conversational flows for executing software processes in both local and remote environments.  Essentially, Autogen enables the structuring of agent interactions that can execute individual pieces of code or entire programs.  Moreover, the iterative and conversational nature of Autogen allows these agents to identify and address technical issues in the execution of specific programs, adjusting the execution code as needed, with or without human intervention.

Problem Statement

Autogen's extensive and innovative AI tools make it a perfect fit for various software development and engineering applications. Modern reasoning models, such as OpenAI o1/o3 and DeepSeek R1, have advanced to the point where they can swiftly and efficiently handle many tasks that were once the domain of human engineers.

One of the most challenging, time-consuming, and often expensive areas of applications engineering is database migration.  Enterprises such as banks, financial institutions, and other large organizations frequently encounter difficulties when upgrading from older legacy database implementations during modernization efforts.  These enterprise databases often contain highly complex business logic and vendor-specific code, requiring sophisticated knowledge to effectively implement modernization efforts.

Autogen Database Migration Solution

As previously discussed, legacy database migration presents significant challenges to organizations due to the high costs and complexity involved in the conversion process.  This article aims to demonstrate how Autogen and Generative AI can significantly reduce the overhead associated with these efforts and automate many of the complex tasks required for successful modernization.

To showcase the modern AI capabilities and their application to database migration tasks, this article will focus on an example of converting Sybase stored procedure code to PostgreSQL syntax. It will demonstrate how Autogen can be used to quickly and seamlessly materialize Sybase objects in a target database location without the need for human expertise.

The illustrative example is organized as follows:

  1. Install Autogen Python library
  2. Configure Autogen’s conversational agents
  3. Define a high-level prompt to direct the agents towards the task
  4. Execute the Autogen agent conversation using autogen 0.2 framework
  5. Evaluate agents’ execution and validate the results

1. Configuring Conversational Agents

Autogen is a flexible framework with a pluggable architecture that supports the use of reasoning from multiple vendors.  These include:

  1. OpenAI’s GPT models
  2. Locally run models such as Llama
  3. Novel reasoning contenders, such as DeepSeek R1 and others.

In this example, we will use Azure AI Foundry GPT 4o endpoint to demonstrate the capabilities, but it should be noted that other vendor models may produce similar results.

To configure Autogen, the first task is to install the python dependencies.  This may be easily accomplished via the following command:

>pip install -U "autogen-agentchat" "autogen-ext[openai]" "autogen-ext[azure]"

Once the library is installed, conversation flow may be configured.  Autogen offers a large variety of agent implementations that support various use case patterns.  Some examples of agents include (but not limited to):

AssistantAgent

Built-in agent that uses language models and has the ability to use various tool

ConversableAgent

Generic class of agents that are able to in multiple ways

UserProxyAgent

A proxy agent that accepts human user feedback

LocalCommandLineCodeExecutor

Agent designed to execute code or other programs using local command line environment

DockerCommandLineCodeExecutor

Similar to LocalCommandLineCodeExecutor, this agent implementation executes code through a command line environment in a Docker container

In this exercise, we will utilize the AssistantAgent to interface with the GPT 4o AI model and a LocalCommandLineCodeExecutor agent to execute instructions and code generated by the AI.  Our agents will communicate iteratively and self-reflectively to overcome any challenges during the conversion process.  Conceptually, agent interactions can be visualized as follows:

To configure this conversational flow using we must first import the necessary agent classes into our Python environment

Next, the AssistantAgent instance needs to be configured to access the reasoning LLM endpoint.  In this example, we will set up the connection to an Azure AI Foundry GPT 4o endpoint using AzureAIChatCompletionClient. Autogen requires the connection configuration to be provided in the following forma:

Here, api_key and base_url attributes are references to the values in the endpoint deployment configuration of the Azure AI Foundry endpoint:

Once the client instance is defined, AssistantAgent may be instantiated together with the CodeExecution tool as follows:

2. Defining the task prompt

One of the most fascinating capabilities of modern reasoning models is their ability to handle high-level, general prompts. Unlike previous approaches that required highly specific technical guidance, such as output formats, these reasoning models can iteratively deduce the necessary technical steps when provided with appropriate feedback.

In this exercise, we will task an AI reasoning model with the complex job of converting and testing a SQL stored procedure from one language flavor to another. Additionally, we will require the AI to conduct comprehensive unit testing and use command line tools to deploy the generated stored procedure in the target database, thereby completing the migration process.

We will take the following sample Sybase store procedure as an example:

 

Then, to guide the AI, we provide the following prompt definitions:

“Deploy the following Sybase store procedure as Postgres to the local Postgres database test_autogen with username: "postgres" and password: "postgres".

Use only python. Must be a stored procedure and not a function.

Once the Postgres stored procedure has been created, generate a file test_TopCustomer.sql that contains a set of comprehensive tests for the stored procedure.

The unit tests should test all reasonable combinations of inputs including possible corner cases.

Execute the unit tests in the test_TopCustomer.sql file and output the results to a file test_TopCustomer_results.txt

test_TopCustomer_results.txt should be in the following format:

<test name>,<short description of the test>,<pass or fail result>

Evaluate the test results and modify the code to fix failures.

Ensure that the final version of test_TopCustomer_results.txt contains results for all the unit tests. ”

The above prompt instructs the AI as to:

  1. The high-level task it is required to perform (i.e.: migrate a stored procedure from Sybase to Postgres)
  2. The necessary information for the target Postgres database where the new stored procedure is to be deployed
  3. Coding standards instructions for code generation (i.e.: stored procedure vs. function)
  4. Types of unit testing required prior to the generated stored procedure deployment
  5. Output specification for testing results reporting

3. Execute the Autogen agent conversation

To execute the code migration task, we need to establish a conversational flow using Autogen.  Our conversational agents will analyze the specified task prompt, leverage AI's knowledge of relevant tools and techniques, and iteratively address various responses and error messages until the task is completed and the stored procedure is successfully deployed to the target database.

Defining a conversation flow using Autogen 0.4 is straightforward and requires only a few code instructions.  First step is to define the self-reflective loop using an AutoGen’s 0.4 RoundRobinGroupChat chat manager instance:

Next, the conversational flow is started via the chat stream on the team chat manager:

4. Evaluate agents’ execution and validate the results

The next step is to execute the conversation flow and evaluate the reasoning steps undertaken by the GPT-4o model. In our example, the model's first step was to generate a detailed Python script with the Postgres-specific implementation of the stored procedure:

Unfortunately, the initial attempt to deploy the generated stored procedure to PostgreSQL was unsuccessful due to a syntax error:

---------- assistant ----------

'Error creating procedure: syntax error at or near "TRANSACTION"

LINE 8:             BEGIN TRANSACTION;’

The next few steps highlight the key advantages of using Autogen in conjunction with modern reasoning models.  Instead of giving up and reporting the error to the user, our AI agent continued its attempts to complete the migration task. Leveraging Autogen’s ability to iteratively reflect on the output, the AI-powered AssistantAgent was able to understand the nature of the error and modify the generated SQL code to remedy the issue:

---------- assistant ----------

The error occurred because the PostgreSQL `BEGIN` keyword is used to start a transaction, while `BEGIN TRANSACTION` is a T-SQL (SQL Server/Sybase) command that is not supported in PostgreSQL. I will correct this and re-execute the procedure creation. Let me fix the issue and retry.

The new version of the generated code does not contain the offending ‘BEGIN TRANSACTION’ statement:

 

At this point, the stored procedure has been deployed to the Postres instance, which may be verified using the pgAdmin interface:

Next step for the AI is generate the necessary unit tests and validate the quality of the generated stored procedure code.  As part of the task execution, the AI generated the following unit tests:

Again, the initial version of the unit test suite resulted in an error:

---------- assistant ----------

Error executing tests: syntax error at or near "\"

LINE 33:             \COPY (SELECT * FROM test_results) TO 'test_TopC...

As before, the Autogen-empowered AI was able to reflect on the error and augment the code to alleviate the issue:
---------- assistant ----------

It appears the same issue persists because the SQL file still contains references to `\COPY`, which is a psql meta-command and not valid within programmatic execution. I will clean the SQL file directly, remove `\COPY`, and replace it with standard SQL to populate the test results file. Let me resolve this issue.

Executing the above unit test collection resulted in the following successful report:

test_name,description,result

Test Case 1,No records in the table,PASS

Test Case 2,Price greater than $60,PASS

Test Case 3,Price between $30 and $60,PASS

Test Case 4,Price less than or equal to $30,PASS

Conclusion

This article provided a straightforward yet illustrative example of using reasoning AI models to automate a complex database migration task with the Autogen 0.4 library and Azure AI Foundry.  The example demonstrated how Autogen's conversational agents can iteratively address technical issues and modify execution code to successfully convert Sybase stored procedure code to PostgreSQL syntax, conduct comprehensive unit testing, and deploy the generated stored procedure in the target database without human intervention. This showcases the potential of Autogen to significantly reduce the overhead and complexity associated with legacy database migration efforts.

To further enhance the effectiveness of database migration using AI, future steps could include exploring more sophisticated agent orchestration configurations and integrating additional AI models to handle a wider range of database systems and migration scenarios. Additionally, developing best practices for configuring and managing these AI-powered agents can help organizations streamline their modernization efforts and ensure successful outcomes. By leveraging the advanced capabilities of Autogen and other modern reasoning models, enterprises can achieve more efficient and cost-effective database migrations

Updated Mar 02, 2025
Version 4.0
No CommentsBe the first to comment