Redgate Flyway

Use the CLI to save database changes to the Schema Model

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

  1. 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 

  1. 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
  2. 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.

  3. Optional, see more details about how each object in the diff artifact has changed.
    flyway diffText

    The output will look like:


  4. Save the diff artifact to disk.  
    flyway model

    The output will look like:

    These files are now written to disk.

  5. 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:


  6. You could use the git command line to push to your remote repository or create a PR/MR.

 

Next Steps

  1. Use the CLI to generate migrations

Didn't find what you were looking for?