mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 14:34:19 +00:00
Add docker-compose and .env
This commit is contained in:
parent
b5504b27cd
commit
33521d6b74
4 changed files with 39 additions and 3 deletions
1
examples/postgres/mockable-todos/.env.example
Normal file
1
examples/postgres/mockable-todos/.env.example
Normal file
|
@ -0,0 +1 @@
|
|||
DATABASE_URL="postgres://postgres:password@localhost/todos"
|
|
@ -6,19 +6,27 @@ This example is based on the ideas in [this blog post](https://medium.com/better
|
|||
|
||||
## Setup
|
||||
|
||||
1. Declare the database URL
|
||||
1. Run `docker-compose up -d` to run Postgres in the background.
|
||||
|
||||
2. Declare the database URL, either by exporting it:
|
||||
|
||||
```
|
||||
export DATABASE_URL="postgres://postgres:password@localhost/todos"
|
||||
```
|
||||
|
||||
2. Create the database.
|
||||
or by making a `.env` file:
|
||||
|
||||
```
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
3. Create the database.
|
||||
|
||||
```
|
||||
$ sqlx db create
|
||||
```
|
||||
|
||||
3. Run sql migrations
|
||||
4. Run sql migrations
|
||||
|
||||
```
|
||||
$ sqlx migrate run
|
||||
|
@ -43,3 +51,11 @@ List all todos
|
|||
```
|
||||
cargo run
|
||||
```
|
||||
|
||||
## Cleanup
|
||||
|
||||
To destroy the Postgres database, run:
|
||||
|
||||
```
|
||||
docker-compose down --volumes
|
||||
```
|
||||
|
|
17
examples/postgres/mockable-todos/docker-compose.yml
Normal file
17
examples/postgres/mockable-todos/docker-compose.yml
Normal file
|
@ -0,0 +1,17 @@
|
|||
version: "3"
|
||||
|
||||
services:
|
||||
database:
|
||||
image: "postgres"
|
||||
ports:
|
||||
- "5432:5432"
|
||||
restart: always
|
||||
environment:
|
||||
- POSTGRES_USER=postgres
|
||||
- POSTGRES_PASSWORD=password
|
||||
- POSTGRES_DB=todos
|
||||
volumes:
|
||||
- database_data:/var/lib/postgresql/data
|
||||
volumes:
|
||||
database_data:
|
||||
driver: local
|
|
@ -1,4 +1,5 @@
|
|||
use async_trait::async_trait;
|
||||
use dotenv;
|
||||
use sqlx::postgres::PgPool;
|
||||
use sqlx::Done;
|
||||
use std::{env, io::Write, sync::Arc};
|
||||
|
@ -19,6 +20,7 @@ enum Command {
|
|||
#[async_std::main]
|
||||
#[paw::main]
|
||||
async fn main(args: Args) -> anyhow::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
|
||||
let todo_repo = PostgresTodoRepo::new(pool);
|
||||
let mut writer = std::io::stdout();
|
||||
|
|
Loading…
Reference in a new issue