|
161 | 161 | # Initialize database if it doesn't exist |
162 | 162 | if [ ! -f "$DATA_DIR/PG_VERSION" ]; then |
163 | 163 | echo "Initializing database at $DATA_DIR" |
164 | | - ${postgresPackage}/bin/initdb -U supabase_admin -D "$DATA_DIR" |
| 164 | + ${postgresPackage}/bin/initdb --allow-group-access --data-checksums -U supabase_admin -D "$DATA_DIR" |
165 | 165 | fi |
166 | 166 |
|
167 | 167 | # Deploy processed config files with @dataDir@ substituted |
|
340 | 340 | mkdir -p -m 0700 "$NEW_DATA" |
341 | 341 |
|
342 | 342 | # Initialize new cluster |
343 | | - ${newPkg}/bin/initdb -U supabase_admin -D "$NEW_DATA" |
| 343 | + ${newPkg}/bin/initdb --allow-group-access --data-checksums -U supabase_admin -D "$NEW_DATA" |
344 | 344 |
|
345 | 345 | # Deploy config files to new data directory |
346 | 346 | for f in postgresql.conf pg_hba.conf pg_ident.conf supautils.conf read-replica.conf; do |
@@ -412,12 +412,174 @@ let |
412 | 412 | # breaks D-Bus policy during switch-to-configuration) |
413 | 413 | environment.systemPackages = [ newPkg ]; |
414 | 414 | }; |
| 415 | + # Create a specialisation for OrioleDB — wipes data and reinitializes from scratch |
| 416 | + # (no pg_upgrade path from regular PG to OrioleDB), then runs full Supabase init |
| 417 | + makeOrioledbSpecialisation = |
| 418 | + { |
| 419 | + postgresPort ? defaultPort, |
| 420 | + }: |
| 421 | + let |
| 422 | + orioledbPkg = self.packages.${system}."psql_orioledb-17/bin"; |
| 423 | + groongaPackage = self.packages.${system}.supabase-groonga; |
| 424 | + newDataDir = "/var/lib/postgresql/data-orioledb-17"; |
| 425 | + processedConfig = processAnsibleConfig { majorVersion = "orioledb-17"; }; |
| 426 | + port = toString postgresPort; |
| 427 | + |
| 428 | + # Wipe existing data — no upgrade path from regular PG to OrioleDB |
| 429 | + migrateScript = pkgs.pkgsLinux.writeShellScript "postgresql-orioledb-migrate" '' |
| 430 | + set -euo pipefail |
| 431 | + NEW_DATA="${newDataDir}" |
| 432 | + if [ -d "$NEW_DATA" ]; then |
| 433 | + rm -rf "$NEW_DATA" |
| 434 | + fi |
| 435 | + ''; |
| 436 | + |
| 437 | + # Runs as root: ensure data directory exists with correct ownership |
| 438 | + preStartRootScript = pkgs.pkgsLinux.writeShellScript "postgresql-orioledb-pre-start-root" '' |
| 439 | + set -euo pipefail |
| 440 | + DATA_DIR="${newDataDir}" |
| 441 | + if [ ! -d "$DATA_DIR" ]; then |
| 442 | + mkdir -p -m 0700 "$DATA_DIR" |
| 443 | + chown postgres:postgres "$DATA_DIR" |
| 444 | + fi |
| 445 | + ''; |
| 446 | + |
| 447 | + # Runs as postgres: initdb with OrioleDB-specific args, config deployment, validation |
| 448 | + initScript = pkgs.pkgsLinux.writeShellScript "postgresql-orioledb-init" '' |
| 449 | + set -euo pipefail |
| 450 | + DATA_DIR="${newDataDir}" |
| 451 | +
|
| 452 | + if [ ! -f "$DATA_DIR/PG_VERSION" ]; then |
| 453 | + echo "Initializing OrioleDB database at $DATA_DIR" |
| 454 | + ${orioledbPkg}/bin/initdb \ |
| 455 | + --allow-group-access --data-checksums \ |
| 456 | + --locale-provider=icu --encoding=UTF-8 --icu-locale=en_US.UTF-8 \ |
| 457 | + -U supabase_admin -D "$DATA_DIR" |
| 458 | + fi |
| 459 | +
|
| 460 | + # Deploy processed config files with @dataDir@ substituted |
| 461 | + for f in postgresql.conf pg_hba.conf pg_ident.conf supautils.conf read-replica.conf; do |
| 462 | + sed "s|@dataDir@|$DATA_DIR|g" ${processedConfig}/$f > "$DATA_DIR/$f" |
| 463 | + done |
| 464 | +
|
| 465 | + # Copy conf.d directory |
| 466 | + rm -rf "$DATA_DIR/conf.d" |
| 467 | + cp -r ${processedConfig}/conf.d "$DATA_DIR/conf.d" |
| 468 | + chmod -R u+w "$DATA_DIR/conf.d" |
| 469 | +
|
| 470 | + # Copy extension-custom-scripts directory |
| 471 | + rm -rf "$DATA_DIR/extension-custom-scripts" |
| 472 | + cp -r ${processedConfig}/extension-custom-scripts "$DATA_DIR/extension-custom-scripts" |
| 473 | + chmod -R u+w "$DATA_DIR/extension-custom-scripts" |
| 474 | +
|
| 475 | + # Validate config |
| 476 | + echo "Validating PostgreSQL configuration..." |
| 477 | + ${orioledbPkg}/bin/postgres -C shared_preload_libraries -D "$DATA_DIR" |
| 478 | + ''; |
| 479 | + |
| 480 | + # Full db init: CREATE EXTENSION orioledb first, then init-scripts + migrations |
| 481 | + dbInitScript = pkgs.pkgsLinux.writeShellScript "supabase-orioledb-db-init" '' |
| 482 | + set -euo pipefail |
| 483 | +
|
| 484 | + echo "Waiting for PostgreSQL to be ready..." |
| 485 | + for i in $(seq 1 60); do |
| 486 | + if ${orioledbPkg}/bin/pg_isready -h localhost -p ${port} -q; then |
| 487 | + echo "PostgreSQL is ready" |
| 488 | + break |
| 489 | + fi |
| 490 | + if [ "$i" -eq 60 ]; then |
| 491 | + echo "PostgreSQL failed to become ready" |
| 492 | + exit 1 |
| 493 | + fi |
| 494 | + sleep 1 |
| 495 | + done |
| 496 | +
|
| 497 | + PSQL="${orioledbPkg}/bin/psql" |
| 498 | +
|
| 499 | + # Create orioledb extension first (before init-scripts, so tables use orioledb storage) |
| 500 | + echo "Creating orioledb extension..." |
| 501 | + $PSQL -h localhost -p ${port} -U supabase_admin -d postgres -c "CREATE EXTENSION orioledb CASCADE;" |
| 502 | +
|
| 503 | + # Create postgres role (matching run-server.sh.in) |
| 504 | + echo "Creating postgres role..." |
| 505 | + $PSQL -h localhost -p ${port} -U supabase_admin -d postgres -c "CREATE ROLE postgres SUPERUSER LOGIN;" || true |
| 506 | + $PSQL -h localhost -p ${port} -U supabase_admin -d postgres -c "ALTER DATABASE postgres OWNER TO postgres;" || true |
| 507 | +
|
| 508 | + # Run init-scripts as postgres user (matching run-server.sh.in) |
| 509 | + for sql in ${migrationsDir}/init-scripts/*.sql; do |
| 510 | + echo "Running init-script: $sql" |
| 511 | + $PSQL -v ON_ERROR_STOP=1 -h localhost -p ${port} -U postgres -f "$sql" postgres |
| 512 | + done |
| 513 | +
|
| 514 | + # Run pgbouncer auth schema |
| 515 | + echo "Running pgbouncer auth schema..." |
| 516 | + $PSQL -v ON_ERROR_STOP=1 -h localhost -p ${port} -U postgres -d postgres -f ${pgbouncerAuthSchemaSql} |
| 517 | +
|
| 518 | + # Run stat extension |
| 519 | + echo "Running stat extension..." |
| 520 | + $PSQL -v ON_ERROR_STOP=1 -h localhost -p ${port} -U postgres -d postgres -f ${statExtensionSql} |
| 521 | +
|
| 522 | + # Run migrations as supabase_admin (matching run-server.sh.in) |
| 523 | + for sql in ${migrationsDir}/migrations/*.sql; do |
| 524 | + echo "Running migration: $sql" |
| 525 | + $PSQL -v ON_ERROR_STOP=1 -h localhost -p ${port} -U supabase_admin -f "$sql" postgres |
| 526 | + done |
| 527 | +
|
| 528 | + # Run postgresql schema |
| 529 | + echo "Running postgresql schema..." |
| 530 | + $PSQL -v ON_ERROR_STOP=1 -h localhost -p ${port} -U supabase_admin -f ${postgresqlSchemaSql} postgres |
| 531 | +
|
| 532 | + echo "OrioleDB database initialization complete" |
| 533 | + ''; |
| 534 | + in |
| 535 | + { |
| 536 | + # Reinit service wipes data for fresh orioledb cluster |
| 537 | + systemd.services.postgresql-migrate = { |
| 538 | + description = "PostgreSQL OrioleDB Reinitialization"; |
| 539 | + serviceConfig = { |
| 540 | + Type = "oneshot"; |
| 541 | + RemainAfterExit = true; |
| 542 | + User = "postgres"; |
| 543 | + Group = "postgres"; |
| 544 | + ExecStart = migrateScript; |
| 545 | + }; |
| 546 | + environment = { |
| 547 | + LANG = "en_US.UTF-8"; |
| 548 | + }; |
| 549 | + }; |
| 550 | + |
| 551 | + # Override postgresql: new package, new data dir, new ExecStartPre for orioledb initdb |
| 552 | + systemd.services.postgresql = { |
| 553 | + after = [ "postgresql-migrate.service" ]; |
| 554 | + requires = [ "postgresql-migrate.service" ]; |
| 555 | + serviceConfig = { |
| 556 | + ExecStartPre = lib.mkForce [ |
| 557 | + ("+" + preStartRootScript) |
| 558 | + initScript |
| 559 | + ]; |
| 560 | + ExecStart = lib.mkForce "${orioledbPkg}/bin/postgres -D ${newDataDir}"; |
| 561 | + }; |
| 562 | + environment = { |
| 563 | + GRN_PLUGINS_DIR = lib.mkForce "${groongaPackage}/lib/groonga/plugins"; |
| 564 | + }; |
| 565 | + }; |
| 566 | + |
| 567 | + # Override db-init with orioledb-aware version (creates orioledb ext + full init) |
| 568 | + systemd.services.supabase-db-init = { |
| 569 | + wantedBy = lib.mkForce [ "multi-user.target" ]; |
| 570 | + serviceConfig.ExecStart = lib.mkForce dbInitScript; |
| 571 | + }; |
| 572 | + |
| 573 | + # Add orioledb package to system PATH |
| 574 | + environment.systemPackages = [ orioledbPkg ]; |
| 575 | + }; |
415 | 576 | in |
416 | 577 | { |
417 | 578 | inherit |
418 | 579 | processAnsibleConfig |
419 | 580 | makeSupabaseTestConfig |
420 | 581 | makeUpgradeSpecialisation |
| 582 | + makeOrioledbSpecialisation |
421 | 583 | expectedVersions |
422 | 584 | defaultPort |
423 | 585 | ; |
|
0 commit comments