Skip to content
Merged
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
15 changes: 1 addition & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ endif
# PostgreSQL connection URL for diesel CLI.
POSTGRES_URL ?= postgresql://postgres:postgres@localhost:5432/postgres

# Migration directories and files.
# Generated database schema file (produced by `diesel print-schema`).
SCHEMA_OUTPUT = ./crates/nvisy-postgres/src/schema.rs
MIGRATIONS_IN_DIR = ./migrations
MIGRATIONS_OUT_DIR = ./crates/nvisy-postgres/src/migrations

# Auth secret keys.
PRIVATE_KEY_FILE = private.pem
Expand Down Expand Up @@ -68,17 +66,9 @@ generate-keys: ## Generates auth key pair and encryption key.

.PHONY: generate-migrations
generate-migrations: ## Regenerates the Postgres migrations and database schema.
@$(call log,Deleting embedded migrations directory...)
@rm -rf $(MIGRATIONS_OUT_DIR)
@$(call log,Embedded migrations directory deleted.)
@$(call log,Deleting a generated database schema file...)
@rm -f $(SCHEMA_OUTPUT)
@$(call log,Database schema file deleted.)
@$(call log,Ensuring migrations directory exists...)
@mkdir -p $(MIGRATIONS_OUT_DIR)
@$(call log,Copying migrations to $(MIGRATIONS_OUT_DIR)...)
@cp -r $(MIGRATIONS_IN_DIR)/* $(MIGRATIONS_OUT_DIR)
@$(call log,Migrations copied successfully.)
@$(call log,Running migrations...)
@DATABASE_URL=$(POSTGRES_URL) diesel migration run
@$(call log,Migrations applied successfully.)
Expand All @@ -88,9 +78,6 @@ generate-migrations: ## Regenerates the Postgres migrations and database schema.

.PHONY: clear-migrations
clear-migrations: ## Reverts all database migrations.
@$(call log,Deleting copied migrations...)
@rm -rf $(MIGRATIONS_OUT_DIR)
@$(call log,Copied migrations deleted.)
@$(call log,Reverting all migrations...)
@while DATABASE_URL=$(POSTGRES_URL) diesel migration list | grep -q "\\[X\\]"; do \
$(call log,Reverting migration...); \
Expand Down
2 changes: 1 addition & 1 deletion crates/nvisy-postgres/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
/// To work around this limitation, you can add a custom `build.rs` file to your crate.
/// This ensures the crate is rebuilt whenever the migration directory changes.
fn main() {
println!("cargo:rerun-if-changed=./src/migrations");
println!("cargo:rerun-if-changed=../../migrations");
}
6 changes: 5 additions & 1 deletion crates/nvisy-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#![doc = include_str!("../README.md")]

/// Embeds all migrations into the final binary.
///
/// Points at the single canonical `migrations/` directory at the repository root
/// (resolved from this crate's manifest directory), so there is one source of
/// truth for migrations rather than a copy kept in sync inside the crate.
pub(crate) const MIGRATIONS: diesel_migrations::EmbeddedMigrations =
diesel_migrations::embed_migrations!();
diesel_migrations::embed_migrations!("../../migrations");

/// Tracing target for database query operations.
///
Expand Down
5 changes: 2 additions & 3 deletions diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import_types = ["diesel::sql_types::*"]
custom_type_derives = ["diesel::query_builder::QueryId"]

[migrations_directory]
# The "./migrations" directory is copied from the root directory into
# the "./crates/nvisy-postgres/migrations" directory when running
# the task "make generate-migrations".
# The single source of truth for migrations. `embed_migrations!` in
# nvisy-postgres reads this same directory (via "../../migrations").
dir = "./migrations"