Creating Our Database Migrations
For Postgres we need a table of to-do items to insert rows and perform queries. We can do this by running SQL scripts against the server on the startup of the server. These migrations can be generated using the SQLX client. To install the SQLX client, run the following command:
❯ cargo install sqlx-cli
With the SQLX client installed, we can now move to the root directory of our data access layer for our to-do nanoservice and create a .env
file with the following content:
DATABASE_URL=postgres://username:password@localhost/to_do
When running, the SQLX client will detect the .env
file and export the content as environment variables to connect to the database. Now that we have the environment variables to connect to the database, we can create our first migration with the command below:
❯ sqlx migration add initial-setup
You should get a printout congratulating you on creating the migration and you should also get the following file structure...