Use the CLI to save database changes to the Schema Model
Published 11 December 2024
This example follows on from Setup and baseline a project via the command line (cli). In this example, we'll make changes to a development database and then use the Flyway CLI to capture the new changes in the schema model on disk. This could be done via a Pull Request (PR) (or Merge Request (MR) in GitLab) or as part of an automated process that is kicked off as part of another process (e.g., using a Change Request system, or manually kciking off a "Save development database changes" pipeline, etc.).
Setup
- Complete the Setup and baseline a project via the command line (cli) how-to guide. Or, you can use the below as a guide.
Saving development database changes to the Schema Model
- Make changes to your development database in the IDE of your choice. In this example, you can run the following SQL on the AutoPilotDev DB.
USE AutoPilotDev; GO ALTER TABLE Customers.Customer ADD MiddleName NVARCHAR(50); GO CREATE PROCEDURE Customers.GetCustomerByLastName @LastName NVARCHAR(50) AS BEGIN SELECT CustomerID, FirstName, MiddleName, LastName, Email, DateOfBirth, Phone, Address FROM Customers.Customer WHERE LastName LIKE '%' + @LastName + '%'; END
- In a command prompt at your project folder, run the following command to see all the differences between your development database and your SchemaModel on disk. In this example, we expect the two changes we made above: the MiddleName column added to the Cusotmer table and a new stored procedure.
flyway diff -source=env:development -target=schemaModel
The output will be a diff artifact, which looks like:
Nothing has been written to disk yet. - Optional, see more details about how each object in the diff artifact has changed.
flyway diffText
The output will look like: - Save the diff artifact to disk.
flyway model
The output will look like:
These files are now written to disk. - At this point, you could use the git command line to commit your schema model to your local repo.
git commit -am "Added MiddleName for Customers"
The output should look like: