Skip to content

Commit

Permalink
examples: simplify sqlx template (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipetkov authored Nov 30, 2023
1 parent 514cd66 commit a332503
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions examples/sqlx/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
src = craneLib.path ./.; # The original, unfiltered source
filter = sqlOrCargo;
};
srcMigrations = craneLib.path ./migrations;

# Common arguments can be set here to avoid repeating them later
commonArgs = {
Expand All @@ -55,35 +54,19 @@
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

# Prepare a db with all of our migrations using only the migrations files
sqlx-db = pkgs.runCommand "sqlx-db-prepare"
{
nativeBuildInputs = [
pkgs.sqlx-cli
];
} ''
mkdir $out
export DATABASE_URL=sqlite:$out/db.sqlite3
sqlx database create
sqlx migrate run --source ${srcMigrations}
'';

# Build the actual crate itself, reusing the dependency
# artifacts from above.
my-crate = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;

preBuildPhases = [ "linkDb" ];

nativeBuildInputs = (commonArgs.nativeBuildInputs or [ ]) ++ [
pkgs.sqlite
pkgs.sqlx-cli
];

# Copy over the db so it's available for sqlx to check it's schema
linkDb = ''
mkdir -p db
cp --no-preserve=ownership,mode -t ./db ${sqlx-db}/db.sqlite3*
export DATABASE_URL=sqlite:./db/db.sqlite3
preBuild = ''
export DATABASE_URL=sqlite:./db.sqlite3
sqlx database create
sqlx migrate run
'';
});
in
Expand Down

0 comments on commit a332503

Please sign in to comment.