@@ -551,6 +551,62 @@ def test_move_table_to_schema(self):
551551 table = conn .table ("qgis_schema_test" , "table_to_move" )
552552 self .assertEqual (table .tableName (), "table_to_move" )
553553
554+ def test_move_table_to_schema_geometry_columns (self ):
555+ """
556+ Test that table can be moved to another schema when table is
557+ in geometry_columns metadata table.
558+ """
559+
560+ md = QgsProviderRegistry .instance ().providerMetadata ("mssql" )
561+ conn = md .createConnection (self .uri , {})
562+
563+ conn .dropVectorTable ("qgis_test" , "table_to_move_gc" )
564+ conn .createVectorTable (
565+ "qgis_test" ,
566+ "table_to_move_gc" ,
567+ QgsFields (),
568+ Qgis .WkbType .PolygonZ ,
569+ QgsCoordinateReferenceSystem (),
570+ True ,
571+ {},
572+ )
573+ # make sure table is present in geometry_columns
574+ res = conn .executeSql (
575+ """SELECT f_table_schema, f_table_name from geometry_columns WHERE f_table_schema = 'qgis_test' AND f_table_name = 'table_to_move_gc';"""
576+ )
577+ self .assertEqual (res , [["qgis_test" , "table_to_move_gc" ]])
578+
579+ try :
580+ conn .dropSchema ("qgis_schema_test" , True )
581+ except QgsProviderConnectionException :
582+ # likely schema does not exist
583+ pass
584+
585+ conn .executeSql ("CREATE SCHEMA qgis_schema_test;" )
586+
587+ # test table exist
588+ table = conn .table ("qgis_test" , "table_to_move_gc" )
589+ self .assertEqual (table .tableName (), "table_to_move_gc" )
590+
591+ # move table to another schema
592+ conn .moveTableToSchema (
593+ "qgis_test" ,
594+ "table_to_move_gc" ,
595+ "qgis_schema_test" ,
596+ )
597+
598+ vl = QgsVectorLayer (
599+ conn .tableUri ("qgis_schema_test" , "table_to_move_gc" ),
600+ "table_to_move_gc" ,
601+ "mssql" ,
602+ )
603+ self .assertTrue (vl .isValid ())
604+ self .assertEqual (vl .wkbType (), Qgis .WkbType .PolygonZ )
605+ res = conn .executeSql (
606+ """SELECT f_table_schema, f_table_name from geometry_columns WHERE f_table_schema = 'qgis_schema_test' AND f_table_name = 'table_to_move_gc';"""
607+ )
608+ self .assertEqual (res , [["qgis_schema_test" , "table_to_move_gc" ]])
609+
554610 def test_rename_field (self ):
555611 """Test rename fields"""
556612 md = QgsProviderRegistry .instance ().providerMetadata ("mssql" )
0 commit comments