feat(package): migrator: something

This commit is contained in:
2025-12-04 21:02:38 +00:00
parent dfa76b2a19
commit b786098867
12 changed files with 226 additions and 120 deletions

View File

@@ -69,7 +69,9 @@
packages.${system} = import ./package { inherit flake self inputs pkgs system; }; packages.${system} = import ./package { inherit flake self inputs pkgs system; };
devShells.${system} = import ./devshell { inherit flake self inputs pkgs system; }; devShells.${system} = import ./devshell { inherit flake self inputs pkgs system; };
legacyPackages.${system} = import ./legacy { inherit flake self inputs pkgs system; }; legacyPackages.${system} = import ./legacy { inherit flake self inputs pkgs system; };
nixosConfigurations = { }; nixosConfigurations = {
"xray|${system}" = import ./nixos/system/xray { inherit flake self inputs system; };
};
checks.${system} = import ./test { inherit flake self inputs pkgs system; }; checks.${system} = import ./test { inherit flake self inputs pkgs system; };
}) // { }) // {
lib = self-lib; lib = self-lib;

View File

@@ -0,0 +1,20 @@
{
flake,
self,
inputs,
system,
...
}: let
# Use folder name as name of this system
name = builtins.baseNameOf ./.;
in self.lib.nixpkgs-lib.nixosSystem {
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ self.overlays.default ];
};
modules = [
{ networking.hostName = name; }
(import ./${name}.nix { inherit flake self inputs; })
];
}

View File

@@ -0,0 +1,47 @@
{
inputs,
flake,
self,
}: {
lib,
pkgs,
modulesPath,
config,
...
}:
{
imports = [
self.nixosModules.hectic
];
services.xray = {
enable = true;
setting = ''
{
"inbounds": [
{
"port": 10086,
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "b831381d-6324-4d53-ad4f-8cda48b30811"
}
]
}
}
],
"outbounds": [
{
"protocol": "freedom"
}
]
}
'';
};
hectic = {
archetype.base.enable = true;
hardware.lenovo-legion.enable = true;
};
}

View File

@@ -143,7 +143,6 @@ in {
migrator = pkgs.callPackage ./migrator {}; migrator = pkgs.callPackage ./migrator {};
nbt2json = pkgs.callPackage ./nbt2json {}; nbt2json = pkgs.callPackage ./nbt2json {};
hemar-parser = pkgs.callPackage ./hemar/parser {}; hemar-parser = pkgs.callPackage ./hemar/parser {};
hemar-grammar = pkgs.callPackage ./hemar/grammar {};
pg-17-ext-http = buildHttpExt pkgs "17"; pg-17-ext-http = buildHttpExt pkgs "17";
pg-17-ext-smtp-client = buildSmtpExt pkgs "17"; pg-17-ext-smtp-client = buildSmtpExt pkgs "17";
pg-17-ext-plhaskell = buildPlHaskellExt pkgs "17"; pg-17-ext-plhaskell = buildPlHaskellExt pkgs "17";

View File

@@ -204,7 +204,7 @@ migrate_down() {
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
--*|-*) --*|-*)
printf 'migrate argument %s does not exists' "$1" log error "\`migrate down\` argument $WHITE$1$NC does not exists"
exit 1 exit 1
;; ;;
''|*[!0-9]*) ''|*[!0-9]*)
@@ -226,7 +226,7 @@ migrate_up() {
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
--*|-*) --*|-*)
printf 'migrate argument %s does not exists' "$1" log error "\`migrate up\` argument $WHITE$1$NC does not exists"
exit 1 exit 1
;; ;;
''|*[!0-9]*) ''|*[!0-9]*)
@@ -245,19 +245,24 @@ migrate_up() {
} }
migrate_to() { migrate_to() {
local migration_name
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
--*|-*) --*|-*)
printf 'migrate argument %s does not exists' "$1" log error "\`migrate to\` argument $WHITE$1$NC does not exists"
exit 1 exit 1
;; ;;
*) *)
MIGRATION_NAME= # shellcheck disable=SC2016
[ "${migration_name+x}" ] && { log error '`migrate to` too many arguments'; exit 1; }
migration_name=$1
shift
;; ;;
esac esac
done done
[ "${MIGRATION_NAME+x}" ] || { log error "no migration name specified"; exit 1; } [ "${migration_name+x}" ] || { log error "no migration name specified"; exit 1; }
printf '%s' "$migration_name"
} }
migration_list() { migration_list() {
@@ -266,8 +271,10 @@ migration_list() {
migrate() { migrate() {
local fs_migrations db_migrations db_migration fs_migration psql_args var #target_migration local fs_migrations db_migrations db_migration fs_migration psql_args var #target_migration
MIGRATOR_REMAINING_ARS=
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
log trace "migrate arg $WHITE$1"
case $1 in case $1 in
up|down|to) up|down|to)
[ "${MIGRATE_SUBCOMMAND+x}" ] && { [ "${MIGRATE_SUBCOMMAND+x}" ] && {
@@ -289,11 +296,13 @@ migrate() {
VARIABLE_LIST="${VARIABLE_LIST+$VARIABLE_LIST }$2" VARIABLE_LIST="${VARIABLE_LIST+$VARIABLE_LIST }$2"
shift 2 shift 2
;; ;;
--*|-*) REMAINING_ARS="$REMAINING_ARS $(quote "$1")"; shift ;; # unknown global -> pass through --*|-*) MIGRATOR_REMAINING_ARS="$MIGRATOR_REMAINING_ARS $(quote "$1")"; shift ;; # unknown global -> pass through
*) REMAINING_ARS="$REMAINING_ARS $(quote "$1")"; shift ;; *) MIGRATOR_REMAINING_ARS="$MIGRATOR_REMAINING_ARS $(quote "$1")"; shift ;;
esac esac
done done
log debug "migrate REMAINING_ARGS: $WHITE$MIGRATOR_REMAINING_ARS"
[ "${FORCE+x}" ] && { [ "${FORCE+x}" ] && {
log error "migrate --force not implemented" log error "migrate --force not implemented"
exit 1 exit 1
@@ -330,14 +339,24 @@ migrate() {
i=$((i+1)) i=$((i+1))
done done
eval "set -- $REMAINING_ARS" eval "set -- $MIGRATOR_REMAINING_ARS"
target_migration="$("migrate_$MIGRATE_SUBCOMMAND" "$@")" target_migration="$("migrate_$MIGRATE_SUBCOMMAND" "$@")"
log debug "target_migration: ${target_migration}" log debug "target_migration: ${target_migration}"
exit 1 index_of
}
idx_of() {
name=$1
[ -z "$name" ] && { echo 0; return; }
i=1
printf '%s\n' "$fs_migrations" | while IFS= read -r m; do
[ "$m" = "$name" ] && { echo "$i"; return; }
i=$((i+1))
done
echo 0
} }
form_psql_args() { form_psql_args() {

0
package/migrator/util.sh Normal file
View File

View File

@@ -31,7 +31,7 @@
) (lib.filterAttrs (_: v: v != null) ) (lib.filterAttrs (_: v: v != null)
(lib.mapAttrs (n: t: mkTestDrv n t) testDir)); (lib.mapAttrs (n: t: mkTestDrv n t) testDir));
hemar = self.packages.${system}.hemar; hemar = self.packages.${system}.hemar-parser;
mkPgTest = testName: testDrv: pkgs.runCommand "hemar-test-${testName}" mkPgTest = testName: testDrv: pkgs.runCommand "hemar-test-${testName}"
{ {
nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused ]; nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused ];

View File

@@ -1,32 +1,32 @@
answer="$(printf '%s' 'some text' | hemar -c)" #answer="$(printf '%s' 'some text' | hemar -c)"
#
expected="$(printf '%s' '[ #expected="$(printf '%s' '[
{ # {
"type": "text", # "type": "text",
"value": "some text" # "value": "some text"
} # }
]')" #]')"
#
json_diff "$answer" "$expected" #json_diff "$answer" "$expected"
#
answer="$(printf '%s' 'some [] {} text' | hemar -c)" #answer="$(printf '%s' 'some [] {} text' | hemar -c)"
#
expected="$(printf '%s' '[ #expected="$(printf '%s' '[
{ # {
"type": "text", # "type": "text",
"value": "some [] {} text" # "value": "some [] {} text"
} # }
]')" #]')"
#
json_diff "$answer" "$expected" #json_diff "$answer" "$expected"
#
answer="$(printf '%s' 'some {' | hemar -c)" #answer="$(printf '%s' 'some {' | hemar -c)"
#
expected="$(printf '%s' '[ #expected="$(printf '%s' '[
{ # {
"type": "text", # "type": "text",
"value": "some {" # "value": "some {"
} # }
]')" #]')"
#
json_diff "$answer" "$expected" #json_diff "$answer" "$expected"

View File

@@ -1,38 +1,38 @@
answer="$(printf '%s' 'text begind {[ "inn \\e\"r"-t\"ext ]}' | hemar -c)" #answer="$(printf '%s' 'text begind {[ "inn \\e\"r"-t\"ext ]}' | hemar -c)"
#
expected="$(printf '%s' '[ #expected="$(printf '%s' '[
{ # {
"type": "text", # "type": "text",
"value": "text begind " # "value": "text begind "
}, # },
{ # {
"type": "interpolation", # "type": "interpolation",
"path": "inn \\e\"r-t\"ext" # "path": "inn \\e\"r-t\"ext"
} # }
]')" #]')"
#
json_diff "$answer" "$expected" #json_diff "$answer" "$expected"
#
[ "$(printf '%s' "$answer" | yq '.[1] | .path')" = 'inn \e"r-t"ext' ] || { #[ "$(printf '%s' "$answer" | yq '.[1] | .path')" = 'inn \e"r-t"ext' ] || {
log error 'unexpected' # log error 'unexpected'
exit 1 # exit 1
} #}
#
answer="$(printf '%s' 'text begind {[ [" "] ]}' | hemar -c)" #answer="$(printf '%s' 'text begind {[ [" "] ]}' | hemar -c)"
#
expected="$(printf '%s' '[ #expected="$(printf '%s' '[
{ # {
"type": "text", # "type": "text",
"value": "text begind " # "value": "text begind "
}, # },
{ # {
"type": "interpolation", # "type": "interpolation",
"path": "[ ]" # "path": "[ ]"
} # }
]')" #]')"
#
json_diff "$answer" "$expected" #json_diff "$answer" "$expected"
#
answer="$(printf '%s' 'text begind {[ [" "\ ] ]}' | hemar -c)" #answer="$(printf '%s' 'text begind {[ [" "\ ] ]}' | hemar -c)"
#
json_diff "$answer" "$expected" #json_diff "$answer" "$expected"

View File

@@ -39,6 +39,8 @@
} '' } ''
${builtins.readFile self.legacyPackages.${system}.helpers.posix-shell.log} ${builtins.readFile self.legacyPackages.${system}.helpers.posix-shell.log}
test=${testDrv} test=${testDrv}
export HECTIC_LOG=trace
${builtins.readFile ./util.sh}
${builtins.readFile ./lauch.sh} ${builtins.readFile ./lauch.sh}
# success marker for Nix # success marker for Nix

View File

@@ -1,12 +1,21 @@
##!/bin/dash #!/bin/dash
#
#HECTIC_NAMESPACE=test-migration-list HECTIC_NAMESPACE=test-migration-list
#
#psql "$DATABASE_URL" 'CREATE TABLE profile (
# id INTEGER,
# username TEXT log notice "test case: ${WHITE}migration up"
#)' psql "$DATABASE_URL" 'CREATE TABLE profile (
# id INTEGER,
#migrator --db-url "$DATABASE_URL" migrate to 20251104192425-add-info-to-profile username TEXT
# )'
#exit 1
if ! migrator --db-url "$DATABASE_URL" migrate to 20251104192425-add-info-to-profile; then
log error "test failed: ${WHITE}error on migration up"
exit 1
fi
log notice "$(columns profile)"
exit 1

View File

@@ -0,0 +1,8 @@
#!/bin/bash
# columns(table)
columns() {
psql -Atc 'SELECT column_name
FROM information_schema.columns
WHERE table_name = '"${1};"
}