Skip to content

Commit c049e76

Browse files
committed
[mssql] When moving tables, update geometry_columns table
Ensure that the geometry_columns metadata table is also updated when moving SQL server tables via browser
1 parent b0a21eb commit c049e76

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/providers/mssql/qgsmssqlproviderconnection.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,15 @@ QString QgsMssqlProviderConnection::defaultPrimaryKeyColumnName() const
880880

881881
void QgsMssqlProviderConnection::moveTableToSchema( const QString &sourceSchema, const QString &tableName, const QString &targetSchema ) const
882882
{
883-
executeSqlPrivate( u"ALTER SCHEMA %1 TRANSFER %2.%3"_s
884-
.arg( QgsMssqlUtils::quotedIdentifier( targetSchema ), QgsMssqlUtils::quotedIdentifier( sourceSchema ), QgsMssqlUtils::quotedIdentifier( tableName ) ) );
883+
QString sql = u"ALTER SCHEMA %1 TRANSFER %2.%3;\n"_s
884+
.arg( QgsMssqlUtils::quotedIdentifier( targetSchema ), QgsMssqlUtils::quotedIdentifier( sourceSchema ), QgsMssqlUtils::quotedIdentifier( tableName ) );
885+
sql += u"if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'geometry_columns' )\n"
886+
"UPDATE geometry_columns SET f_table_schema = %1 WHERE f_table_schema = %2 AND f_table_name = %3;"_s.arg(
887+
QgsMssqlUtils::quotedValue( targetSchema ),
888+
QgsMssqlUtils::quotedValue( sourceSchema ),
889+
QgsMssqlUtils::quotedValue( tableName )
890+
);
891+
executeSqlPrivate( sql );
885892
}
886893

887894
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions QgsMssqlProviderConnection::sqlOptions( const QString &layerSource )

tests/src/python/test_qgsproviderconnection_mssql.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)