-
Notifications
You must be signed in to change notification settings - Fork 142
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
base: main
Are you sure you want to change the base?
Changes from all commits
7876efa
8ea5b09
1f32a77
8364cca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" |
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"] |
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: |
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 | ||
); |
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(()) | ||
} |
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" |
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 | ||
); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||
use tokio_postgres::NoTls; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems equal to the |
||||||
|
||||||
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:"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for m in report.applied_migrations() { | ||||||
println!("→ {} (version {})", m.name(), m.version()); | ||||||
} | ||||||
|
||||||
Ok(()) | ||||||
} |
There was a problem hiding this comment.
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?