Skip to content

feat: add db-schema subcommand for exporting database schema SQL#26

Open
cxhello wants to merge 8 commits intonacos-group:mainfrom
cxhello:feat/db-schema-command
Open

feat: add db-schema subcommand for exporting database schema SQL#26
cxhello wants to merge 8 commits intonacos-group:mainfrom
cxhello:feat/db-schema-command

Conversation

@cxhello
Copy link
Copy Markdown
Contributor

@cxhello cxhello commented Apr 1, 2026

Summary

  • Add nacos-setup db-schema subcommand to export full database schema SQL for a given Nacos version
  • Support MySQL and PostgreSQL with correct filename mapping (pg-schema.sql for PostgreSQL)
  • Local-first resolution: installed Nacos → cache → GitHub remote download with new/old path fallback
  • Interactive mode when args are missing, non-interactive safe ([ -t 0 ] detection)
  • All log output to stderr, schema SQL to stdout (pipe-friendly)

Closes #25 (Part B)

Usage

# Full parameter mode
nacos-setup db-schema -v 3.2.0 --type mysql

# Pipe directly to database
nacos-setup db-schema -v 3.2.0 --type mysql | mysql -h localhost -u root -p nacos

# Interactive mode
nacos-setup db-schema

Test plan

  • 19 unit/integration tests added (tests/test_db_schema.sh)
  • Full test suite passes (113/113)
  • Smoke tested: MySQL 3.2.0 (new path), MySQL 3.1.1 (old path fallback), PostgreSQL 3.2.0
  • Verified stderr/stdout separation
  • Test on Linux environment

cxhello added 7 commits April 2, 2026 00:01
Signed-off-by: cxhello <caixiaohuichn@gmail.com>
Signed-off-by: cxhello <caixiaohuichn@gmail.com>
Signed-off-by: cxhello <caixiaohuichn@gmail.com>
…dout/stderr separation

Signed-off-by: cxhello <caixiaohuichn@gmail.com>
Signed-off-by: cxhello <caixiaohuichn@gmail.com>
find_local_schema returns non-zero when no local schema file exists,
which causes the script to abort under set -e before attempting the
download fallback. Add || true to suppress the exit-on-error.

Also register test_db_schema.sh in the test runner.

Signed-off-by: cxhello <caixiaohuichn@gmail.com>
Nacos repository uses pg-schema.sql for PostgreSQL, not
postgresql-schema.sql. Add _schema_filename() mapping function
and update local/remote lookup to use actual filenames.

Signed-off-by: cxhello <caixiaohuichn@gmail.com>
@qiacheng7
Copy link
Copy Markdown
Collaborator

qiacheng7 commented Apr 2, 2026

cool,is it compatible with Windows?

Verify that full init SQL and incremental schema-change SQL can be output to a local temp folder, with the matching commands printed in the terminal; DBs: cover PostgreSQL and MySQL

Port db-schema subcommand to Windows PowerShell, mirroring the bash
implementation. Includes schema filename mapping (pg-schema.sql for
PostgreSQL), local/cache/remote resolution, and interactive prompts.

Tested with pwsh 7.6.0 on macOS.

Signed-off-by: cxhello <caixiaohuichn@gmail.com>
@cxhello
Copy link
Copy Markdown
Contributor Author

cxhello commented Apr 2, 2026

Thanks for the review!

Windows compatibility: Added in the latest commit (cc8eb76). windows/lib/db_schema.ps1 mirrors the bash implementation, tested with pwsh 7.6.0 on macOS.

Incremental schema-change SQL: Not included in this PR. Nacos upstream doesn't provide versioned migration SQL files — only full schema per release. Generating reliable ALTER statements from diffing two full schemas is error-prone (can't handle renames, data migrations, etc.). Happy to revisit if Nacos adds official migration scripts in the future.

Output to temp folder with commands: Currently outputs to stdout for pipe-friendly usage (db-schema ... | mysql ...). We could add a --output flag to save to a file and print the matching command — would you like that as part of this PR or as a follow-up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terminal output and CLI UX & Database schema output

2 participants