Skip to content

Add example with postgres and add rusqlite in new directory #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ members = [
"refinery_cli",
"refinery_core",
"refinery_macros",
"examples",
"examples/rusqlite",
"examples/postgres_sql",
]

[profile.release]
Expand Down
11 changes: 11 additions & 0 deletions examples/postgres_docker_sql/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "refinery-migrations-postgres"
authors = ["Jonatas Oliveira <[email protected]>"]
description = "Refinery usage example with postgres and docker"
version = "0.1.0"
edition = "2024"

[dependencies]
refinery = { version = "0.8", features = ["postgres", "tokio-postgres"]}
tokio = { version = "1", features = ["full"] }
tokio-postgres = "0.7"
21 changes: 21 additions & 0 deletions examples/postgres_docker_sql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM rust:slim as builder

WORKDIR /usr/src/app

COPY Cargo.toml Cargo.lock ./
COPY migrations ./migrations
COPY src ./src

RUN cargo build --release

FROM debian:stable-slim

RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates libssl-dev \
&& rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/src/app/target/release/refinery-migrations-postgres /usr/local/bin/refinery-migrations-postgres

ENV DATABASE_URL="postgres://postgres:password@db:5432/postgres"

ENTRYPOINT ["refinery-migrations-postgres"]
23 changes: 23 additions & 0 deletions examples/postgres_docker_sql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: refinery
volumes:
- db_data:/var/lib/postgresql/data
ports:
- "5432:5432"

migrations:
build: .
depends_on:
- db
restart: "on-failure"
environment:
DATABASE_URL: "postgres://postgres:password@db:5432/refinery"
tty: true

volumes:
db_data:
8 changes: 8 additions & 0 deletions examples/postgres_docker_sql/migrations/V1__init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
date DATE NOT NULL,
kind VARCHAR(6) NOT NULL,
amount NUMERIC NOT NULL,
description TEXT NOT NULL,
tag VARCHAR(50) NOT NULL
);
29 changes: 29 additions & 0 deletions examples/postgres_docker_sql/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use tokio_postgres::NoTls;

mod embedded {
use refinery::embed_migrations;
embed_migrations!("migrations");
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_url = std::env::var("DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres:password@localhost:5432/postgres".into());
let (client, connection) = tokio_postgres::connect(&db_url, NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
let mut client = client;
let report = embedded::migrations::runner()
.run_async(&mut client)
.await?;

println!("Migrations applied with success:");
for m in report.applied_migrations() {
println!("→ {} (version {})", m.name(), m.version());
}

Ok(())
}
11 changes: 11 additions & 0 deletions examples/postgres_sql/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "refinery-migrations-postgres"
authors = ["Jonatas Oliveira <[email protected]>"]
description = "Refinery usage example with postgres and docker"
version = "0.1.0"
edition = "2024"

[dependencies]
refinery = { path = "../../refinery", features = ["postgres", "tokio-postgres"] }
tokio = { version = "1", features = ["full"] }
tokio-postgres = "0.7"
8 changes: 8 additions & 0 deletions examples/postgres_sql/migrations/V1__init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE transactions (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of moving the existing examples to the rusqlite example dir, can we have them at the main examples level and use them in this example as well?

id SERIAL PRIMARY KEY,
date DATE NOT NULL,
kind VARCHAR(6) NOT NULL,
amount NUMERIC NOT NULL,
description TEXT NOT NULL,
tag VARCHAR(50) NOT NULL
);
29 changes: 29 additions & 0 deletions examples/postgres_sql/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use tokio_postgres::NoTls;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems equal to the postgres_docker_sql example, is there any value in adding this as well? I think we can just have the docker one with a README.md explaining how to run it in the main examples dir


mod embedded {
use refinery::embed_migrations;
embed_migrations!("migrations");
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_url = std::env::var("DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres:password@localhost:5432/postgres".into());
let (client, connection) = tokio_postgres::connect(&db_url, NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
let mut client = client;
let report = embedded::migrations::runner()
.run_async(&mut client)
.await?;

println!("Migrations apllied with success:");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
println!("Migrations apllied with success:");
println!("Migrations applied with success:");

for m in report.applied_migrations() {
println!("→ {} (version {})", m.name(), m.version());
}

Ok(())
}
2 changes: 1 addition & 1 deletion examples/Cargo.toml → examples/rusqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2021"
enums = ["refinery/enums"]

[dependencies]
refinery = { path = "../refinery", features = ["rusqlite"] }
refinery = { path = "../../refinery", features = ["rusqlite"] }
rusqlite = "0.35"
barrel = { version = "0.7", features = ["sqlite3"] }
log = "0.4"
Expand Down
File renamed without changes.
Loading