diff --git a/Cargo.toml b/Cargo.toml index 9cd4a9ed..606beb96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,8 @@ members = [ "refinery_cli", "refinery_core", "refinery_macros", - "examples", + "examples/rusqlite", + "examples/postgres_sql", ] [profile.release] diff --git a/examples/postgres_docker_sql/Cargo.toml b/examples/postgres_docker_sql/Cargo.toml new file mode 100644 index 00000000..13a746ce --- /dev/null +++ b/examples/postgres_docker_sql/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "refinery-migrations-postgres" +authors = ["Jonatas Oliveira "] +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" diff --git a/examples/postgres_docker_sql/Dockerfile b/examples/postgres_docker_sql/Dockerfile new file mode 100644 index 00000000..c145c1b2 --- /dev/null +++ b/examples/postgres_docker_sql/Dockerfile @@ -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"] diff --git a/examples/postgres_docker_sql/docker-compose.yml b/examples/postgres_docker_sql/docker-compose.yml new file mode 100644 index 00000000..7c0bc266 --- /dev/null +++ b/examples/postgres_docker_sql/docker-compose.yml @@ -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: diff --git a/examples/postgres_docker_sql/migrations/V1__init.sql b/examples/postgres_docker_sql/migrations/V1__init.sql new file mode 100644 index 00000000..ac5e63cc --- /dev/null +++ b/examples/postgres_docker_sql/migrations/V1__init.sql @@ -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 +); diff --git a/examples/postgres_docker_sql/src/main.rs b/examples/postgres_docker_sql/src/main.rs new file mode 100644 index 00000000..2d351e8e --- /dev/null +++ b/examples/postgres_docker_sql/src/main.rs @@ -0,0 +1,29 @@ +use tokio_postgres::NoTls; + +mod embedded { + use refinery::embed_migrations; + embed_migrations!("migrations"); +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + 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(()) +} diff --git a/examples/postgres_sql/Cargo.toml b/examples/postgres_sql/Cargo.toml new file mode 100644 index 00000000..0e123dc2 --- /dev/null +++ b/examples/postgres_sql/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "refinery-migrations-postgres" +authors = ["Jonatas Oliveira "] +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" diff --git a/examples/postgres_sql/migrations/V1__init.sql b/examples/postgres_sql/migrations/V1__init.sql new file mode 100644 index 00000000..ac5e63cc --- /dev/null +++ b/examples/postgres_sql/migrations/V1__init.sql @@ -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 +); diff --git a/examples/postgres_sql/src/main.rs b/examples/postgres_sql/src/main.rs new file mode 100644 index 00000000..8e575b3c --- /dev/null +++ b/examples/postgres_sql/src/main.rs @@ -0,0 +1,29 @@ +use tokio_postgres::NoTls; + +mod embedded { + use refinery::embed_migrations; + embed_migrations!("migrations"); +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + 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:"); + for m in report.applied_migrations() { + println!("→ {} (version {})", m.name(), m.version()); + } + + Ok(()) +} diff --git a/examples/Cargo.toml b/examples/rusqlite/Cargo.toml similarity index 88% rename from examples/Cargo.toml rename to examples/rusqlite/Cargo.toml index f999299e..c44136b7 100644 --- a/examples/Cargo.toml +++ b/examples/rusqlite/Cargo.toml @@ -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" diff --git a/examples/migrations/V1__initial.sql b/examples/rusqlite/migrations/V1__initial.sql similarity index 100% rename from examples/migrations/V1__initial.sql rename to examples/rusqlite/migrations/V1__initial.sql diff --git a/examples/migrations/V2__add_cars_table.sql b/examples/rusqlite/migrations/V2__add_cars_table.sql similarity index 100% rename from examples/migrations/V2__add_cars_table.sql rename to examples/rusqlite/migrations/V2__add_cars_table.sql diff --git a/examples/migrations/V3__add_brand_to_cars_table.rs b/examples/rusqlite/migrations/V3__add_brand_to_cars_table.rs similarity index 100% rename from examples/migrations/V3__add_brand_to_cars_table.rs rename to examples/rusqlite/migrations/V3__add_brand_to_cars_table.rs diff --git a/examples/src/main.rs b/examples/rusqlite/src/main.rs similarity index 100% rename from examples/src/main.rs rename to examples/rusqlite/src/main.rs