{"id":16064500,"url":"https://github.com/marvin-hansen/bazel-diesel-postgres","last_synced_at":"2025-10-09T00:13:05.918Z","repository":{"id":253732372,"uuid":"844344781","full_name":"marvin-hansen/bazel-diesel-postgres","owner":"marvin-hansen","description":"Bazel configuration to build a Rust crate that uses Diesel to access PostgreSQL DB ","archived":false,"fork":false,"pushed_at":"2024-09-15T10:52:14.000Z","size":8885,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-17T20:39:38.583Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marvin-hansen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-19T04:05:53.000Z","updated_at":"2024-09-15T10:52:17.000Z","dependencies_parsed_at":"2024-09-15T12:51:43.516Z","dependency_job_id":"af33f66b-4a05-4e9c-bb25-af833b3ccbf0","html_url":"https://github.com/marvin-hansen/bazel-diesel-postgres","commit_stats":null,"previous_names":["marvin-hansen/bazel-diesel-postgres"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marvin-hansen%2Fbazel-diesel-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marvin-hansen%2Fbazel-diesel-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marvin-hansen%2Fbazel-diesel-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marvin-hansen%2Fbazel-diesel-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marvin-hansen","download_url":"https://codeload.github.com/marvin-hansen/bazel-diesel-postgres/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247313111,"owners_count":20918576,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T05:08:17.377Z","updated_at":"2025-10-09T00:13:00.878Z","avatar_url":"https://github.com/marvin-hansen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bazel-diesel-postgres\n\nBazel configuration to build a Rust crate that uses Diesel with custom array data types in Postgres.\n\nA cargo-only version of this repo has recently (August 2024) been contributed to the Diesel project. \nSee [PR #4169](https://github.com/diesel-rs/diesel/pull/4169) for more details. \n\n### Setup:\n\n1) Start a Postgres server with database postgres and password postgres, either locally or in Docker i.e.\n\n`docker run --name postgres-5432 -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres:16.3-bookworm`\n\nAny postgres version past version 14 should suffice. Versions before 14 may work but have not been tested.\n\n2) Add an .evn file to the repo root with the DB URL:\n\n`echo \"POSTGRES_DATABASE_URL=postgres://postgres:postgres@localhost/postgres\" \u003e .env`\n\nNote, if your postgres server uses a different default database or password,\nensure you update the POSTGRES_DATABASE_URL in the .env file accordingly.\n\nTo build the crate with Bazel, ensure you have [Bazelisk installed](https://github.com/bazelbuild/bazelisk/releases).\n\n3) Run the example\n\nNote, the tests run an embedded database migration to create all tables, types, and schema meaning\nthis example is self-contained given a working Postgres server is accessible.\n\na) Cargo:\n\nBuild the project:\n\n`cargo b`\n\nRun all tests:\n\n`cargo t`\n\nb) Bazel:\n\n`bazel build //...`\n\nRun all tests:\n\n`bazel test //...`\n\n\nIf you want to preserve CARGO compatibility i.e. for better IDE support in JetBrains RustRover, \nensure your Bazel dependencies match exactly those in the Cargo.toml file.\n\n### Embedded DB Migrations\n\nConventionally, you would use the `diesel_migrations` macro to manage your embedded database migrations.\nHowever, because of\na very [unfortunate discrepancy related to how Bazel via PWD and Cargo set the CARGO_MANIFEST_DIR](https://internals.rust-lang.org/t/build-scripts-pwd-and-cargo-manifest-dir-strangeness/16833/2)\nenvironment variable required by the macro, the `diesel_migrations` crate does not work in Bazel. \nThere is no good solution available for the time being. A pull request with a solution is very much welcome.\n\nThe main implication of this issue is that the macro cannot bundle all migrations automatically into an migration embedding, \nbut still can perform the migrations from a given migration embedding.\nTherefore you have to write a custom migration embedding as shown in the [embed_migration file](src/embed_migrations.rs)\nto ensure embedded migrations are applied automatically before tests and when your application starts.\nThe CARGO_MANIFEST_DIR environment variable used in the custom migration is overwritten by Bazel to ensure \nthat the project compiles with both, Cargo and Bazel. \n\nIn a larger multi-crate project you want to rename that environment variable in the Bazel BUILD file \nto something more obvious i.e. DIESEL_MIGRATION_DIR, but doing so requires you to ensure Cargo \nalso sets an environment variable of the same name if you want to preserve that\nCargo and Bazel build the same project. \n\nIn a Bazel only project, however, you can simplify the environment variable by replacing \nthe genrule with a location lookup i.e.:\n\n```python\ncopy_directory(\n     name = \"copy_migrations\",\n     src = \"migrations\",\n     out = \"migrations\",\n )\n \nrust_library(\n    name = \"pg_smdb\",\n    srcs = glob([\n        \"src/**\",\n    ]),\n    compile_data = [\n        \":copy_migrations\",\n    ],\n    rustc_env = {\n        \"DIESEL_MIGRATION_DIR\": \"$(location :copy_migrations)\",\n    },\n    crate_root = \"src/lib.rs\",\n    visibility = [\"//visibility:public\"],\n    deps = [ ..\n    ],\n) \n```\nNote, in that case, all files and folders are relative to the migrations folder so you would have to access them as shown below:\n\n```rust \nconst DIESEL_UP: \u0026'static str = include_str!(concat!(\nenv!(\"DIESEL_MIGRATION_DIR\") // Path relative to the migrations folder\n\"/00000000000000_diesel_initial_setup/up.sql\"\n));\n```\n\nIf you work in a Cargo only project and don't need Bazel, you can uncomment the `diesel_migrations` macro  in lib.rs and delete the custom embed_migrations file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarvin-hansen%2Fbazel-diesel-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarvin-hansen%2Fbazel-diesel-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarvin-hansen%2Fbazel-diesel-postgres/lists"}