-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflake.nix
73 lines (68 loc) · 2.95 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{
inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; };
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
makeTest = pkgs.callPackage "${nixpkgs}/nixos/tests/make-test-python.nix";
in
{
checks.${system}.test-diesel-migration =
let
username = "postgres";
password = "password";
database = "database";
migrations_dir = "migrations-based";
in
makeTest
{
name = "test-diesel-migration";
nodes = {
server = { lib, config, pkgs, ... }: {
services.postgresql = {
enable = true;
ensureDatabases = [ database ];
ensureUsers = [{
name = username;
ensurePermissions = {
"DATABASE ${database}" = "ALL PRIVILEGES";
};
}];
initialScript = pkgs.writeScript "initScript" ''
ALTER USER postgres WITH PASSWORD '${password}';
'';
};
systemd.services.postgresql.postStart = lib.mkAfter ''
${pkgs.diesel-cli}/bin/diesel migration run --database-url "postgres://${username}:${password}@localhost/${database}" --migration-dir ${self}/${migrations_dir}
${pkgs.diesel-cli}/bin/diesel migration redo --database-url "postgres://${username}:${password}@localhost/${database}" --migration-dir ${self}/${migrations_dir}
${pkgs.diesel-cli}/bin/diesel migration run --database-url "postgres://${username}:${password}@localhost/${database}" --migration-dir ${self}/${migrations_dir}
'';
};
};
testScript = ''
start_all()
server.wait_for_unit("postgresql.service")
server.succeed("sudo -u postgres -- ${pkgs.diesel-cli}/bin/diesel print-schema --database-url postgres://${username}:${password}@localhost/${database} > schema.rs")
server.copy_from_vm("schema.rs", "")
'';
}
{
inherit pkgs;
inherit (pkgs) system;
};
packages.${system} = {
update-schema = pkgs.writeScriptBin "update-schema" ''
nix build ${self}#checks.${system}.test-diesel-migration
BUILD_DIR=$(nix build ${self}#checks.${system}.test-diesel-migration --no-link --print-out-paths)
rm -rf src/schema.rs
cp $BUILD_DIR/schema.rs src/schema.rs
'';
run-migration-based = pkgs.writeScriptBin "run-migration" ''
${pkgs.diesel-cli}/bin/diesel migration run --migration-dir ${self}/migrations-based
'';
};
devShells."x86_64-linux".default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ grpc protobuf websocketpp pkg-config postgresql_14 openssl diesel-cli ];
};
};
}