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

@@ -66,15 +66,17 @@
system,
pkgs,
}: {
packages.${system} = import ./package { 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; };
nixosConfigurations = { };
checks.${system} = import ./test { 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; };
legacyPackages.${system} = import ./legacy { inherit flake self inputs pkgs system; };
nixosConfigurations = {
"xray|${system}" = import ./nixos/system/xray { inherit flake self inputs system; };
};
checks.${system} = import ./test { inherit flake self inputs pkgs system; };
}) // {
lib = self-lib;
overlays.default = import ./overlay { inherit flake self inputs; };
nixosModules = import ./nixos/module { inherit flake self inputs; };
templates = import ./template { inherit flake self inputs; };
overlays.default = import ./overlay { inherit flake self inputs; };
nixosModules = import ./nixos/module { inherit flake self inputs; };
templates = import ./template { inherit flake self inputs; };
};
}

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 {};
nbt2json = pkgs.callPackage ./nbt2json {};
hemar-parser = pkgs.callPackage ./hemar/parser {};
hemar-grammar = pkgs.callPackage ./hemar/grammar {};
pg-17-ext-http = buildHttpExt pkgs "17";
pg-17-ext-smtp-client = buildSmtpExt pkgs "17";
pg-17-ext-plhaskell = buildPlHaskellExt pkgs "17";

View File

@@ -37,8 +37,8 @@ while [ $# -gt 0 ]; do
case $1 in
migrate|create|fetch|list|init)
[ "${SUBCOMMAND+x}" ] && {
log error "ambiguous subcommand, decide ${WHITE}$SUBCOMMAND ${NC}or ${WHITE}$1";
exit 2;
log error "ambiguous subcommand, decide ${WHITE}$SUBCOMMAND ${NC}or ${WHITE}$1";
exit 2;
}
SUBCOMMAND=$1
shift
@@ -71,7 +71,7 @@ init() {
shift 2
;;
--set|-v)
VARIABLE_LIST="${VARIABLE_LIST+$VARIABLE_LIST }$2"
VARIABLE_LIST="${VARIABLE_LIST+$VARIABLE_LIST }$2"
shift 2
;;
--*|-*)
@@ -204,16 +204,16 @@ migrate_down() {
while [ $# -gt 0 ]; do
case $1 in
--*|-*)
printf 'migrate argument %s does not exists' "$1"
log error "\`migrate down\` argument $WHITE$1$NC does not exists"
exit 1
;;
''|*[!0-9]*)
log error "down argument not a number";
exit 1;
log error "down argument not a number";
exit 1;
;;
*)
DOWN_NUMBER=$2
shift 2;
DOWN_NUMBER=$2
shift 2;
;;
esac
done
@@ -226,16 +226,16 @@ migrate_up() {
while [ $# -gt 0 ]; do
case $1 in
--*|-*)
printf 'migrate argument %s does not exists' "$1"
log error "\`migrate up\` argument $WHITE$1$NC does not exists"
exit 1
;;
''|*[!0-9]*)
log error "up argument not a number";
exit 1;
log error "up argument not a number";
exit 1;
;;
*)
UP_NUMBER=$2
shift 2;
UP_NUMBER=$2
shift 2;
;;
esac
done
@@ -245,19 +245,24 @@ migrate_up() {
}
migrate_to() {
local migration_name
while [ $# -gt 0 ]; do
case $1 in
--*|-*)
printf 'migrate argument %s does not exists' "$1"
log error "\`migrate to\` argument $WHITE$1$NC does not exists"
exit 1
;;
*)
MIGRATION_NAME=
# shellcheck disable=SC2016
[ "${migration_name+x}" ] && { log error '`migrate to` too many arguments'; exit 1; }
migration_name=$1
shift
;;
esac
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() {
@@ -266,16 +271,18 @@ migration_list() {
migrate() {
local fs_migrations db_migrations db_migration fs_migration psql_args var #target_migration
MIGRATOR_REMAINING_ARS=
while [ $# -gt 0 ]; do
log trace "migrate arg $WHITE$1"
case $1 in
up|down|to)
[ "${MIGRATE_SUBCOMMAND+x}" ] && {
log error "ambiguous migrate subcommand, decide ${WHITE}$MIGRATE_SUBCOMMAND ${NC}or ${WHITE}$1";
exit 2
log error "ambiguous migrate subcommand, decide ${WHITE}$MIGRATE_SUBCOMMAND ${NC}or ${WHITE}$1";
exit 2
}
MIGRATE_SUBCOMMAND="$1"
shift
MIGRATE_SUBCOMMAND="$1"
shift
;;
--db-url|-u)
DB_URL="$2"
@@ -286,14 +293,16 @@ migrate() {
shift
;;
--set|-v)
VARIABLE_LIST="${VARIABLE_LIST+$VARIABLE_LIST }$2"
VARIABLE_LIST="${VARIABLE_LIST+$VARIABLE_LIST }$2"
shift 2
;;
--*|-*) REMAINING_ARS="$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 ;; # unknown global -> pass through
*) MIGRATOR_REMAINING_ARS="$MIGRATOR_REMAINING_ARS $(quote "$1")"; shift ;;
esac
done
log debug "migrate REMAINING_ARGS: $WHITE$MIGRATOR_REMAINING_ARS"
[ "${FORCE+x}" ] && {
log error "migrate --force not implemented"
exit 1
@@ -330,14 +339,24 @@ migrate() {
i=$((i+1))
done
eval "set -- $REMAINING_ARS"
eval "set -- $MIGRATOR_REMAINING_ARS"
target_migration="$("migrate_$MIGRATE_SUBCOMMAND" "$@")"
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() {
@@ -383,7 +402,7 @@ create() {
while [ $# -gt 0 ]; do
case $1 in
--name|-n)
# shellcheck disable=SC2034
# shellcheck disable=SC2034
MIGRATION_NAME=$2
shift 2
;;
@@ -414,7 +433,7 @@ fetch() {
while [ $# -gt 0 ]; do
case $1 in
--db-url|-u)
# shellcheck disable=SC2034
# shellcheck disable=SC2034
DB_URL=$2
shift 2
;;

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

View File

@@ -31,7 +31,7 @@
) (lib.filterAttrs (_: v: v != null)
(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}"
{
nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused ];

View File

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

View File

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

View File

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

View File

@@ -1,12 +1,21 @@
##!/bin/dash
#
#HECTIC_NAMESPACE=test-migration-list
#
#psql "$DATABASE_URL" 'CREATE TABLE profile (
# id INTEGER,
# username TEXT
#)'
#
#migrator --db-url "$DATABASE_URL" migrate to 20251104192425-add-info-to-profile
#
#exit 1
#!/bin/dash
HECTIC_NAMESPACE=test-migration-list
log notice "test case: ${WHITE}migration up"
psql "$DATABASE_URL" 'CREATE TABLE profile (
id INTEGER,
username TEXT
)'
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};"
}