def processAlgorithm(self, parameters, context, feedback): if Qgis.QGIS_VERSION_INT >= 31400: connection_name = self.parameterAsConnectionName( parameters, self.CONNECTION_NAME, context) else: connection_name = self.parameterAsString( parameters, self.CONNECTION_NAME, context) # Write the file out again project_file = self.parameterAsString(parameters, self.PROJECT_FILE, context) metadata = QgsProviderRegistry.instance().providerMetadata('postgres') connection = metadata.findConnection(connection_name) # Read in the template file template_file = resources_path('projects', 'pg_metadata_administration.qgs') with open(template_file, 'r') as fin: file_data = fin.read() # Replace the database connection information file_data = file_data.replace("service='pgmetadata'", connection.uri()) with open(project_file, 'w') as fout: fout.write(file_data) add_connection(connection_name) msg = tr('QGIS Administration project has been successfully created from the database connection') msg += ': {}'.format(connection_name) feedback.pushInfo(msg) return {}
def processAlgorithm(self, parameters, context, feedback): metadata = QgsProviderRegistry.instance().providerMetadata('postgres') if Qgis.QGIS_VERSION_INT >= 31400: connection_name = self.parameterAsConnectionName( parameters, self.CONNECTION_NAME, context) else: connection_name = self.parameterAsString(parameters, self.CONNECTION_NAME, context) connection = metadata.findConnection(connection_name) if not connection: raise QgsProcessingException( tr("The connection {} does not exist.").format( connection_name)) sql = "SELECT pgmetadata.refresh_dataset_calculated_fields();" try: connection.executeSql(sql) except QgsProviderConnectionException as e: feedback.reportError(str(e)) add_connection(connection_name) results = {} return results
def processAlgorithm(self, parameters, context, feedback): metadata = QgsProviderRegistry.instance().providerMetadata('postgres') names = list(metadata.connections().keys()) databases = self.parameterAsEnums(parameters, self.DATABASES, context) database_names = [names[i] for i in databases] reset_connections() for database in database_names: feedback.pushDebugInfo(tr("Setting up : {}").format(database)) add_connection(database) return {}
def processAlgorithm(self, parameters, context, feedback): metadata = QgsProviderRegistry.instance().providerMetadata('postgres') if Qgis.QGIS_VERSION_INT >= 31400: connection_name = self.parameterAsConnectionName( parameters, self.CONNECTION_NAME, context) else: connection_name = self.parameterAsString(parameters, self.CONNECTION_NAME, context) connection = metadata.findConnection(connection_name) if not connection: raise QgsProcessingException( tr("The connection {} does not exist.").format( connection_name)) for template in ["contact", "link", "main"]: feedback.pushInfo(tr('Reset {}.html').format(template)) sql = ("DELETE FROM pgmetadata.html_template " "WHERE section = '{}'").format(template) try: connection.executeSql(sql) except QgsProviderConnectionException as e: feedback.reportError(str(e)) html_file = resources_path("html", "{}.html".format(template)) with open(html_file, "r") as f: sql = ( "INSERT INTO pgmetadata.html_template (section, content) " "VALUES ('{section}', '{value}');".format(section=template, value=f.read())) try: connection.executeSql(sql) except QgsProviderConnectionException as e: feedback.reportError(str(e)) add_connection(connection_name) results = {} return results
def processAlgorithm(self, parameters, context, feedback): if Qgis.QGIS_VERSION_INT >= 31400: connection_name = self.parameterAsConnectionName( parameters, self.CONNECTION_NAME, context) else: connection_name = self.parameterAsString(parameters, self.CONNECTION_NAME, context) metadata = QgsProviderRegistry.instance().providerMetadata('postgres') connection = metadata.findConnection(connection_name) connection: QgsAbstractDatabaseProviderConnection if not connection: raise QgsProcessingException( tr("The connection {} does not exist.").format( connection_name)) if not connection.tableExists(SCHEMA, 'qgis_plugin'): raise QgsProcessingException( tr("The table {}.{} does not exist. You must first create the database structure." ).format(SCHEMA, 'qgis_plugin')) db_version = self.database_version(connection) feedback.pushInfo("Current database version '{}'.".format(db_version)) # Get plugin version plugin_version = version() if plugin_version in ["master", "dev"]: migrations = available_migrations(000000) last_migration = migrations[-1] plugin_version = (last_migration.replace("upgrade_to_", "").replace(".sql", "").strip()) feedback.reportError( tr("Be careful, running the migrations on a development branch!" )) feedback.reportError( tr("Latest available migration is {}").format(plugin_version)) else: feedback.pushInfo( tr("Plugin's version is {}").format(plugin_version)) results = {self.DATABASE_VERSION: plugin_version} # Return if nothing to do if db_version == plugin_version: feedback.pushInfo( tr("The database version and the plugin version are the same, version {}. There isn't any " "upgrade to do.").format(plugin_version)) return results db_version_integer = format_version_integer(db_version) sql_files = available_migrations(db_version_integer) # Loop sql files and run SQL code for sf in sql_files: sql_file = os.path.join(plugin_path(), "install/sql/upgrade/{}".format(sf)) with open(sql_file, "r") as f: sql = f.read() if len(sql.strip()) == 0: feedback.pushInfo("* " + sf + " -- " + tr("SKIPPING, EMPTY FILE")) continue try: connection.executeSql(sql) except QgsProviderConnectionException as e: raise QgsProcessingException(str(e)) new_db_version = (sf.replace("upgrade_to_", "").replace(".sql", "").strip()) self.update_database_version(connection, new_db_version) feedback.pushInfo( "Database version {} -- OK !".format(new_db_version)) self.vacuum_all_tables(connection, feedback) self.update_database_version(connection, plugin_version) feedback.pushInfo( "Database upgraded to the current plugin version {}!".format( plugin_version)) add_connection(connection_name) return results
def processAlgorithm(self, parameters, context, feedback): metadata = QgsProviderRegistry.instance().providerMetadata('postgres') if Qgis.QGIS_VERSION_INT >= 31400: connection_name = self.parameterAsConnectionName( parameters, self.CONNECTION_NAME, context) else: connection_name = self.parameterAsString( parameters, self.CONNECTION_NAME, context) connection = metadata.findConnection(connection_name) if not connection: raise QgsProcessingException(tr("The connection {} does not exist.").format(connection_name)) # Drop schema if needed override = self.parameterAsBool(parameters, self.OVERRIDE, context) if override and SCHEMA in connection.schemas(): feedback.pushInfo(tr("Removing the schema {}…").format(SCHEMA)) try: connection.dropSchema(SCHEMA, True) except QgsProviderConnectionException as e: raise QgsProcessingException(str(e)) # Create full structure sql_files = [ "00_initialize_database.sql", "{}/10_FUNCTION.sql".format(SCHEMA), "{}/20_TABLE_SEQUENCE_DEFAULT.sql".format(SCHEMA), "{}/30_VIEW.sql".format(SCHEMA), "{}/40_INDEX.sql".format(SCHEMA), "{}/50_TRIGGER.sql".format(SCHEMA), "{}/60_CONSTRAINT.sql".format(SCHEMA), "{}/70_COMMENT.sql".format(SCHEMA), "{}/90_GLOSSARY.sql".format(SCHEMA), "99_finalize_database.sql", ] plugin_dir = plugin_path() plugin_version = version() dev_version = False run_migration = os.environ.get( "TEST_DATABASE_INSTALL_{}".format(SCHEMA.upper()) ) if plugin_version in ["master", "dev"] and not run_migration: feedback.reportError( "Be careful, running the install on a development branch!" ) dev_version = True if run_migration: plugin_dir = plugin_test_data_path() feedback.reportError( "Be careful, running migrations on an empty database using {} " "instead of {}".format(run_migration, plugin_version) ) plugin_version = run_migration # Loop sql files and run SQL code for sql_file in sql_files: feedback.pushInfo(sql_file) sql_file = os.path.join(plugin_dir, "install/sql/{}".format(sql_file)) with open(sql_file, "r") as f: sql = f.read() if len(sql.strip()) == 0: feedback.pushInfo(" Skipped (empty file)") continue try: connection.executeSql(sql) except QgsProviderConnectionException as e: raise QgsProcessingException(str(e)) feedback.pushInfo(" Success !") # Add version if run_migration or not dev_version: metadata_version = plugin_version else: migrations = available_migrations(000000) last_migration = migrations[-1] metadata_version = ( last_migration.replace("upgrade_to_", "").replace(".sql", "").strip() ) feedback.reportError("Latest migration is {}".format(metadata_version)) self.vacuum_all_tables(connection, feedback) sql = """ INSERT INTO {}.qgis_plugin (id, version, version_date, status) VALUES (0, '{}', now()::timestamp(0), 1)""".format(SCHEMA, metadata_version) try: connection.executeSql(sql) except QgsProviderConnectionException as e: raise QgsProcessingException(str(e)) feedback.pushInfo("Database version '{}'.".format(metadata_version)) if not run_migration: self.install_html_templates(feedback, connection_name, context) else: feedback.reportError( 'As you are running an old version of the database, HTML templates are not installed.') add_connection(connection_name) results = { self.DATABASE_VERSION: metadata_version, } return results