Generate an SQL Schema from a Migrations Directory
Question
How to generate an SQL schema file from an existing migrations directory to quickly set up a database for integration tests?
Answer
-
Inspect the Migration Directory: Dump the migration directory to an SQL file using the
atlas schema inspectcommand:atlas schema inspect \
--url "file://path/to/migrations" \
--dev-url "<DEV-URL>" \
--format "{{ sql . }}" > schema.sqlDev URLReplace
<DEV-URL>with the URL of your dev-database -
Apply the Schema to Your Test Database: Once the
schema.sqlfile is generated, you can also apply it to your database using theatlas schema applycommand:atlas schema apply \
--url "<DATABASE-URL>" \
--dev-url "<DEV-URL>" \
--to "file://schema.sql"Dev URLReplace
<DEV-URL>with the URL of your dev-database, and<DATABASE-URL>with the URL of your test database.
Alternative Approach
If you prefer to apply the schema represented by the migrations directory directly to your test database without generating a schema file, you can use the atlas schema apply command with the migration directory as the desired state.
atlas schema apply \
--url "<DATABASE-URL>" \
--dev-url "<DEV-URL>" \
--to "file://path/to/migrations"