Skip to content

Move log and history files to $XDG_STATE_HOME#1571

Open
jrraymond wants to merge 1 commit intodbcli:mainfrom
jrraymond:fix/xdg-state-home-log-history
Open

Move log and history files to $XDG_STATE_HOME#1571
jrraymond wants to merge 1 commit intodbcli:mainfrom
jrraymond:fix/xdg-state-home-log-history

Conversation

@jrraymond
Copy link
Copy Markdown

@jrraymond jrraymond commented Apr 3, 2026

Fixes #1497.

Summary

Per the XDG Base Directory Specification, $XDG_STATE_HOME (default ~/.local/state) is the correct location for state data that persists across restarts but is not user-configuration. Log and history fit this definition; config and casing remain in $XDG_CONFIG_HOME.

  • Add state_location() to config.py, mirroring config_location() but using $XDG_STATE_HOME (defaults to ~/.local/state/pgcli/). On Windows, returns the same path as config_location() since there is no XDG equivalent.
  • Add migrate_file(old, new) helper that silently moves a file from the old location to the new one on first launch after upgrade. Errors are logged rather than raised.
  • initialize_logging() and run_cli() now use state_location() for the default log and history paths, and call migrate_file() to handle the one-time upgrade migration.
  • Update default path comments in pgclirc.
  • Add tests for state_location() and all migration cases (old only, both exist, old missing, error logged on failure).

Testing

> pgcli
Server: PostgreSQL 18.3 (Homebrew)
Version: 4.4.0
Home: http://pgcli.com
justinraymond> select * from foo;
Goodbye!

# logs/history resepect XDG_STATE_HOME
> cat ~/.local/state/pgcli/history

# 2026-04-03 11:57:59.545613
+select * from foo;

> cat ~/.local/state/pgcli/log
2026-04-03 11:51:19,816 (77436/MainThread) pgcli.main INFO - No default pager found in environment. Using os default pager
...

# move history/logs to old location to test migration
> mv ~/.local/state/pgcli/* ~/.config/pgcli/
> rm -r ~/.local/state/pgcli/

> pgcli
Server: PostgreSQL 18.3 (Homebrew)
Version: 4.4.0
Home: http://pgcli.com
justinraymond> select * from bar;
Goodbye!

> cat ~/.local/state/pgcli/history

# 2026-04-03 11:57:59.545613
+select * from foo;

# 2026-04-03 11:59:40.069136
+select * from bar;

> cat ~/.local/state/pgcli/log
2026-04-03 11:51:19,816 (77436/MainThread) pgcli.main INFO - No default pager found in environment. Using os default pager
...

# old location empty
> tree ~/.config/pgcli/
/Users/justinraymond/.config/pgcli/
└── config

1 directory, 1 file

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • I verified that my changes work as expected.
  • Please squash merge this pull request.

🤖 Generated with Claude Code

Log and history are state files (persist across restarts, not
user-configuration), so they belong in \$XDG_STATE_HOME
(~/.local/state/pgcli/) rather than \$XDG_CONFIG_HOME
(~/.config/pgcli/).

On first launch after upgrade, existing files at the old location are
silently migrated to the new location. Errors during migration are
logged rather than raised.

Fixes dbcli#1497

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

Log and history should go to $XDG_STATE_HOME instead of $XDG_CONFIG_HOME

1 participant