Creating a baseline script
Published 11 October 2021
When working with an existing database, we recommend creating a baseline script that reflects the state of your objects and reference data that have already been deployed to production so that you can generate new migrations from this point. A baseline script is also necessary to spin up new environments for testing purposes or when deploying to new client sites.
- Open Flyway Desktop and open your project. Navigate to the Generate Migrations tab.
Note: You can only generate a baseline for a project with no migrations. - You will see a banner prompting you to baseline. Click Create baseline
- A dialog will appear allowing you to fill in the connection details for the database you want to use as your baseline.
- Note - Do you need to filter out any additional objects from Production?
Users and roles are automatically filtered out, but if there are other objects that you don't want included in your baseline script, you can click Static data & comparisons to edit your comparison options or configure static data before creating the baseline script. Filters can also be applied separately. See filters and comparison options. If you've setup filters and comparison options for your schema model, then these will already be excluded since the same settings are used.
- Note - Do you need to filter out any additional objects from Production?
- Click the Plus button. You will be shown a connection dialog.
- Enter the credentials for the database you want to use as the baseline, then click Test and save. If the databases across your environments are in sync, then you can use your development database for this. You can optionally customize the version number and description in the filename. Once the details have been filled in click Create baseline to generate the baseline script.
- At the start of the baseline process, Flyway Desktop will check to see if your target contains any invalid objects, e.g., a procedure that refers to a table that no longer exists. If so, it will present you a table of invalid objects. If you believe any are incorrectly raised as invalid, you can deselect them to ensure they're included in the resulting baseline script.
- You will then be able to view and edit your baseline script once it has been generated. You may want to edit your baseline script if it contains Production only information like usernames or permissions that shouldn't be part of the baseline script. You may also want to include additional SQL statements to populate static data or create seed data.
- If your development database is empty, you can bring it in line with the baseline script by migrating the development database. Use the Migrations tab, click Add target database..., select the Development database, and click Close. Then choose Migrate from the drop down and click Run migrate. This will apply the baseline and any pending migration scripts to the selected database. It will also add the flyway_schema_history table to the database and add entries about these scripts being applied, so they are not re-applied on future migrations.
- Make sure you complete the one-off task to baseline your downstream environments in preparation for your first deployment with flyway.