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

Migration from old tables broken if they have more than one row #1343

Open
nhoening opened this issue Feb 26, 2025 · 0 comments
Open

Migration from old tables broken if they have more than one row #1343

nhoening opened this issue Feb 26, 2025 · 0 comments

Comments

@nhoening
Copy link
Contributor

nhoening commented Feb 26, 2025

Saw this on the staging server which tried to upgrade from an older state:

remote:   File "/home/seita/flexmeasures-staging/flexmeasures-work/flexmeasures/data/migrations/versions/ad98460751d9_remove_obsolete_tables.py", line 42, in upgrade        
remote:     result = db.session.execute(        
remote:   File "/home/seita/.virtualenvs/bvp-staging-venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 1487, in scalar_one_or_none        
remote:     return self._only_one_row(        
remote:   File "/home/seita/.virtualenvs/bvp-staging-venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py", line 805, in _only_one_row        
remote:     raise exc.MultipleResultsFound(        
remote: sqlalchemy.exc.MultipleResultsFound: Multiple rows were found when one or none was required    

This code doesn't seem to be able to deal with multiple rows, and was not tested well:

           if inspect.has_table(table):
                result = db.session.execute(
                    sa.text(f"SELECT 1 FROM {table};")
                ).scalar_one_or_none()
                if result:
                    tables_with_data.append(table)
            else:
                print(f"Table {table} not found, continuing...")

...

    if tables_with_data:
        click.confirm(
            f"The following tables still have data and will be dropped by this upgrade: {tables_with_data}. Use `flexmeasures db-ops dump` to create a backup. Are you sure you want to upgrade the database?: ",
            abort=True,
        )

This migration is over a year old, so the urgency is not super-high, as there are probably no other old databases around at this time.

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

No branches or pull requests

1 participant