Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup.py setup-db triggers file not found on first run of pgadmin4 #7266

Closed
J0x90 opened this issue Mar 9, 2024 · 5 comments
Closed

setup.py setup-db triggers file not found on first run of pgadmin4 #7266

J0x90 opened this issue Mar 9, 2024 · 5 comments
Labels

Comments

@J0x90
Copy link

J0x90 commented Mar 9, 2024

Please note that security bugs or issues should be reported to security@pgadmin.org.

Describe the bug

When attempting to run pgadmin4 with python (https://www.pgadmin.org/download/pgadmin-4-python/), I have observed that when entering the last command in the instructions, I get an error.
Command:
pgadmin4
Error:
FileNotFoundError: [Errno 2] No such file or directory: '/var/lib/pgadmin/pgadmin4.db' -> '/var/lib/pgadmin/pgadmin4.db.20240309203301'

Looking through the source code, it appears that when the sqlite db does not exist, the program flow eventually ends up executing
flask_migrate.upgrade(migration_folder) within db_upgrade.py

For example, below we see upgrade_db() is entered because this is not cli_mode, and the SQLITE_PATH doesn't exist.

    def run_migration_for_sqlite():
        with app.app_context():
            # Run migration for the first time i.e. create database
            # If version not available, user must have aborted. Tables are not
            # created and so its an empty db
            if not os.path.exists(SQLITE_PATH) or get_version() == -1:
                # If running in cli mode then don't try to upgrade, just raise
                # the exception
                if not cli_mode:
                    upgrade_db()
                else:
                    if not os.path.exists(SQLITE_PATH):
                        raise FileNotFoundError(
                            'SQLite database file "' + SQLITE_PATH +
                            '" does not exists.')
                    raise RuntimeError(
                        'The configuration database file is not valid.')

Next we see db_upgrade(app) is entered.

    def upgrade_db():
        """
        Execute the migrations.
        """
        try:
            db_upgrade(app)
            os.environ['CORRUPTED_DB_BACKUP_FILE'] = ''
        except Exception:
            app.logger.error('Database migration failed')
            app.logger.error(traceback.format_exc())
            backup_db_file()

Finally, we see flask_migrate

def db_upgrade(app):
    from pgadmin.utils import u_encode, fs_encoding
    with app.app_context():
        migration_folder = os.path.join(
            os.path.dirname(os.path.realpath(u_encode(__file__, fs_encoding))),
            os.pardir, os.pardir,
            'migrations'
        )
        flask_migrate.upgrade(migration_folder)

Are yall sure that flask_migrate.upgrade is the right command to be ran for a first run of pgadmin4. Aka, will this create and initialize the db, because it appears that it is throwing an error when the db doesn't exist initially.

To Reproduce

Steps to reproduce the behavior:

  1. Open terminal on Debian12
  2. Enter the following commands
sudo adduser pgadmin4
sudo usermod -aG sudo pgadmin4
su - pgadmin4
sudo apt install python3.11-venv -y
sudo mkdir /var/lib/pgadmin
sudo mkdir /var/log/pgadmin
sudo chown $USER /var/lib/pgadmin
sudo chown $USER /var/log/pgadmin
python3 -m venv pgadmin4
source pgadmin4/bin/activate
pip install pgadmin4
pgadmin4
  1. See the error
out-1

Expected behavior

Successful installation.

Error message

If possible, include the full, unedited text of any error messages displayed.

FileNotFoundError: [Errno 2] No such file or directory: '/var/lib/pgadmin/pgadmin4.db' -> '/var/lib/pgadmin/pgadmin4.db.20240309203909'

Screenshots

If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context

Add any other context about the problem here.

@J0x90 J0x90 added the Bug label Mar 9, 2024
@khushboovashi
Copy link
Contributor

khushboovashi commented Mar 11, 2024

@J0x90,
There should be some permission issue, so the db file wasn't created for you.
Please provide the entire log file here.

@ShaheedHaque
Copy link

ShaheedHaque commented Mar 11, 2024

As per the thread at https://www.postgresql.org/message-id/CAHAc2je_TBjuZ86nBkAjjAmjgFmAhb03kLNaROSM7nxRoor1Zw%40mail.gmail.com, this problem occurs newly in flask-migrate 4.0.6. It looks as though the changes in miguelgrinberg/Flask-Migrate@6f3f889 introduce some new behaviour which I guess pgadmin4 does not yet support.

@adityatoshniwal
Copy link
Contributor

adityatoshniwal commented Mar 11, 2024

The x-arg was optional previously. Looks like they made it mandatory. A breaking change was released with minor version number. Unfortunately, pgAdmin was released on 7th and Flask-Migrate on 9th.

@adityatoshniwal
Copy link
Contributor

@J0x90 @ShaheedHaque Flask-Migrate has fixed the issue and released a new version - 4.0.7. Please upgrade your venv with latest Flask-Migrate

@ShaheedHaque
Copy link

Flask-migrate 4.0.7 does indeed fix the issue for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants