3liz / qgis-pgmetadata-plugin

QGIS Plugin to manage some metadata from PostgreSQL layer
GNU General Public License v2.0
12 stars 10 forks source link

Batch database upgrade #162

Open effjot opened 6 months ago

effjot commented 6 months ago

Is there a possibility for run the database upgrade as a batch process for all databases? If I select “Execute as batch process”, I would have to select the database connections by hand, because there is no auto-fill select from list as with batch processes for layers.

Gustry commented 6 months ago

If the GUI is not enough, then it can be possible with Python ?

Open your clock in Processing panel, and copy/paste the Python line. You can add a Python loop.

effjot commented 6 months ago

Thanks for your suggestion to take the Python processing.run() command from the processing log!

My main problem, however, was getting the list of all database connections configured in Qgis. With this list, I can write a small Python script or fill the fields in the batch processing GUI.

For the batch processiong interface, I wrote a “custom expression” Python function:

@qgsfunction(args='auto', group='Custom')
def postgresql_connections(feature, parent):
    md = QgsProviderRegistry.instance().providerMetadata('postgres')
    return list(md.connections())

Then I could call the expression in “Autofill → Add values by expression”.

This function (without the decorator) can also be used for the loop in the Python console that you suggested:

for db in postgresql_connections():
    print(db)
    processing.run("pg_metadata:upgrade_database_structure",
    {'CONNECTION_NAME': db,
    'RUN_MIGRATIONS': True})
Gustry commented 6 months ago

Yes, I think is an helper in the plugin. You can also use the PgMetadata Python API :

from pg_metadata.connection_manager import connections_list, 
connections, _ = connections_list()

# or check_pgmetadata_is_installed as well ?

I didn't try.

effjot commented 6 months ago

Ah yes, that should work, too, in a Python script. For the batch GUI I still need the decorated function.

But maybe a toolbox algorithm “Upgrade all [connected] databases” would be a nice addition.