Quickstart - Flyway AutoPilot
Published 31 July 2023
Flyway AutoPilot allows you to trial an end-to-end database development and automated deployment process with very little setup. With Flyway AutoPilot, you get:
- A SQL script to create ready-to-use sample databases, with the ability to test additional objects and scenarios
- A pre-configured Flyway project to get started quickly
- Integration with version control to track your changes and collaborate with your team
- Example migration scripts that are already deployed to the sample databases and the ability to generate new migration scripts (both versioned and undo scripts)
- A pipeline for Continuous Integration and Continuous Deployments (CI/CD) to your sample databases
- Reports to provide more visibility and safety checks integrated into the pipeline
Anyone can use Flyway AutoPilot to explore the database development and deployment possibilities with your team. This first project uses SQL Server / PostgreSQL and GitHub as an example. This should give you a good understanding of how database changes can be version controlled and flow through an automated pipeline regardless of what database or CI/CD system (Azure DevOps, GitLab, Octopus Deploy, etc) you use. Flyway works with over 20 database systems and any CI/CD system, so use this to get started and then explore the rest of our documentation to learn more about your specific needs. We hope to add more examples for PostgreSQL and Oracle soon. Get in touch if you have any questions.
Introducing Flyway Autopilot
Steps to follow
Prerequisites
- Have a GitHub account. There’s a free edition or a free trial for paid tiers.
- Install the following on your developer machine:
- Flyway Enterprise - Desktop - There’s a free 28-day trial.
- Git, if you don’t already have it
- Permission to create some SQL Server databases.
- Ideally, this is on a local SQL Server instance that’s accessible at localhost:1433. If not, that’s ok. You’ll be prompted to change your project's connection strings, which we explain below.
- You can install the free SQL Server Express edition on your developer machine, if needed.
Getting started
- Go to Flyway SQL Server AutoPilot on GitHub or the PostgreSQL AutoPilot on GitHub and click Use As Template. This means that you have your own copy, all of the files you need to get started, as well as your own Git Actions Pipeline.
- Give this repository an easily accessible name and set it to Private, allowing us to potentially move our own schema into it without it being public! Then click Create Repository.
- In your new repo’s Code tab, click Code and copy the URL. You’ll need this to create a new project from version control in Flyway Desktop.
- Launch Flyway Desktop from the Start Menu on your developer machine.
- Click the Open project… drop down > Open from version control. Paste the copied url, select an empty folder as the directory, and click Clone.
→ - Select flyway.toml file and click Open.
- You will get an error about your database connection. This is because we haven’t created them yet. Click on the blue folder icon in the upper right to jump to the files on disk so we can create the necessary databases.
- Open the CreateWidgetDatabases.sql in your preferred IDE (SSMS, VS Code, etc.). If possible, connect to SQL Server localhost and execute the script to create the databases. Make sure you are connected with a user that has permission to create databases. (See below for information on what databases are created and why.)
- If you don’t have a local SQL Server, then execute the script on an instance that you can create databases on for the purposes of a Proof of Concept (POC).
In your IDE (eg. SSMS), run the following query to alter the stored procedure on WidgetDev.
USE WidgetDev GO CREATE OR ALTER PROCEDURE [dbo].[GetAllWidgets] AS BEGIN SELECT w.RecordID, w.Description , wp.Price FROM Widgets w INNER JOIN dbo.WidgetPrices wp ON wp.RecordID = w.RecordID END
- In Flyway Desktop, click Refresh. You'll see a list of all the objects in your database that have not been saved to the Schema model yet. The Schema model is the CREATE DDL script for each object in your development database so you can see how it changes over time and go back to previous versions if necessary.
- If you could not create your databases on localhost on the default instance and port, then Edit development database connection to the Server, Instance, and Port that you used and clicked Save.
- If you could not create your databases on localhost on the default instance and port, then Edit development database connection to the Server, Instance, and Port that you used and clicked Save.
- In this example, there are 2 changes; one is the stored procedure change we made above. You can click on any object to see a side-by-side difference at the bottom.
This completes the initial set up. We can now start capturing changes to our development database in Version Control.
Saving your changes to the Schema Model
- Select the checkbox in front of GetAllWidgets and click Save to project. The create DDL for the object is now saved to the schema-model on disk. This will be committed to Git to see how the object changes overtime.
- Click on Generate migration.
- If you did not create your databases on localhost:1433, you’ll get an error. You can click on the button to update your Shadow Database details.
- Select the checkbox in front of GetAllWidgets and click Generate scripts. We give you the option of selecting the objects again in case there are only a subset of objects ready for deployment.
- Review the script and optionally give it a more descriptive name. The version number automatically picks up the latest versioned migration script number in the project and increments it by one. It also adds a timestamp to avoid conflicts with other developers because each version must be unique. This project is setup to create Undo scripts as well, which can be reviewed at the bottom. Click Save.
- Click on Commit changes.
- If you want to click Verify generated migrations instead, you can. This will run the new Versioned Migration Script against the Shadow Database to make sure there are not any syntax or dependency errors. This is especially helpful if you edited the script. After verifying, click on the Version control tab to continue with the next step. We'll have resources to learn more about the Shadow Database at the end of this page.
- If you want to click Verify generated migrations instead, you can. This will run the new Versioned Migration Script against the Shadow Database to make sure there are not any syntax or dependency errors. This is especially helpful if you edited the script. After verifying, click on the Version control tab to continue with the next step. We'll have resources to learn more about the Shadow Database at the end of this page.
- You should see scripts that represent your schema model changes and how you want to deploy this change (the Versioned and Undo scripts). If you had to change your connection string details, then you’ll also see flyway.*.toml in the list. Select all the changes, enter a commit message, and click Commit and push (or Commit and then Push changes at the top as two separate actions).
- In your GitHub repository, refresh to see the changes you just pushed.
Deploying your changes
- In your GitHub repository, click on Actions. You can review and accept the forked workflows to enable them.
→ - If the CI/CD process doesn’t kick off automatically, go into Actions > workflow and click Run workflow. Make sure you are on the main.yml workflow and not the reporting.yml.
→- This example doesn’t use any branches, so just use main and click Run workflow.
- This example doesn’t use any branches, so just use main and click Run workflow.
- Click on the run to open it. You might see that the run is Queued.
→ - Click on the Deploy Build job to see the logs. If the logs show that it is waiting for a runner (a machine that will execute the steps in this workflow), we need to set this up.
- Add a GitHub runner:
- Click on Settings. Then click on Actions > Runners in the left menu and click New self-hosted runner.
- Follow the instructions for setting up your self-hosted runner on your Operating System. For me, I used Windows and executed the commands in the Windows PowerShell IDE on my machine.
→
- Click on Settings. Then click on Actions > Runners in the left menu and click New self-hosted runner.
- After adding the runner, you’ll need to enter a Flyway trial license key. Go to GitHub > Settings > Secrets and variables > Actions on the left menu and click New repository secret.
- The Name must be FLYWAY_LICENSE_KEY. Enter your license key and click Add secret. If you need a Flyway Enterprise trial key, contact us.
- Go back to your failed workflow and Re-run jobs.
- Your queued build should now run.
- In your repository, check out the reports.
About the databases
WidgetDev, WidgetTest, WidgetStaging, and WidgetProduction represent a simple, linear pipeline where changes flow from Dev to Test, to a Staging environment that is similar to Production, and then to Prod.
The script also creates the following databases that a user shouldn’t have to interact with. These are just to support the process:
- WidgetZBuild – This is the database that is used for your CI process.
- WidgetZCheck – This database is used during your CD process when deploying to Prod to produce a changes and drift report. Learn more.
- WidgetZShadow – This database exists in development only and is needed to generate migrations. Learn more.
Your turn
Now it’s your turn. You can play around with your project as much as you want to try different scenarios. Remember to:
- Make changes to your development database.
- In Flyway Desktop, select the changes and save them to the schema model to have an object level history of how your database changes.
- Generate a migration script to deploy changes that have been saved to the schema model.
- Commit and push your changes.
- Watch the CI/CD pipeline run and see the changes deployed in your downstream environments (Test > Staging > Prod).
Here are some other scenarios you can try:
- Create a new table or procedure.
- Add a column to an existing table.
- Add a new table and a new stored procedure that queries that table. This will show how dependencies work.
- Add Static Data to your project. You can configure this on the Schema model tab.
- Share your GitHub URL with your team members to test multiple developers working on the project. How can you tell what changes they have made?
- You could even try configuring a new project in Flyway Desktop using your own databases.
- If you’re familiar with GitHub actions, modify the yml to add an approval step before going to Prod.
What other scenarios did you test? Let us know.
Questions
- Check out our online training courses on Redgate University.
See our documentation for different CI/CD systems.
Get in touch if you have any questions.