Skip to content

Commit fcb25a1

Browse files
committed
Drop MD tables when disabled
1 parent 991416a commit fcb25a1

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/pgduckdb_background_worker.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern "C" {
4242
#include "catalog/namespace.h"
4343
#include "catalog/pg_namespace.h"
4444
#include "catalog/pg_extension.h"
45+
#include "catalog/pg_foreign_server.h"
4546
#include "utils/acl.h"
4647
#include "utils/guc.h"
4748
#include "utils/memutils.h"
@@ -918,7 +919,7 @@ CreateSchemaIfNotExists(const char *postgres_schema_name, bool is_default_db) {
918919
if (!is_default_db) {
919920
/*
920921
* For ddb$ schemas we need to record a dependency between the schema
921-
* and the extension, so that DROP EXTENSION also drops these schemas.
922+
* and the FDW, so that DROP SERVER motherduck also drops these schemas.
922923
*/
923924
schema_oid = get_namespace_oid(postgres_schema_name, true);
924925
if (schema_oid == InvalidOid) {
@@ -932,12 +933,12 @@ CreateSchemaIfNotExists(const char *postgres_schema_name, bool is_default_db) {
932933
.objectId = schema_oid,
933934
.objectSubId = 0,
934935
};
935-
ObjectAddress extension_address = {
936-
.classId = ExtensionRelationId,
937-
.objectId = pgduckdb::ExtensionOid(),
936+
ObjectAddress server_address = {
937+
.classId = ForeignServerRelationId,
938+
.objectId = GetMotherduckForeignServerOid(), // pgduckdb::ExtensionOid(),
938939
.objectSubId = 0,
939940
};
940-
recordDependencyOn(&schema_address, &extension_address, DEPENDENCY_NORMAL);
941+
recordDependencyOn(&schema_address, &server_address, DEPENDENCY_NORMAL);
941942
}
942943

943944
/* Success, so we commit the subtransaction */

test/pycheck/motherduck_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ def test_md_create_table(md_cur: Cursor, ddb):
5959
):
6060
md_cur.sql("CREATE TABLE t4(a int) USING heap")
6161

62+
def test_md_table_dropped_when_disabled(md_cur: Cursor, ddb):
63+
ddb.sql("CREATE TABLE t1(a int, b varchar)")
64+
ddb.sql("INSERT INTO t1 VALUES (1, 'abc')")
65+
md_cur.wait_until_table_exists("t1")
66+
67+
assert md_cur.sql("SELECT * FROM t1") == (1, "abc")
68+
69+
# Disable MD, should CASCADE drop the table
70+
md_cur.sql("DROP SERVER motherduck CASCADE;")
71+
72+
with pytest.raises(
73+
psycopg.errors.UndefinedTable,
74+
match=r'relation "t1" does not exist',
75+
):
76+
md_cur.sql("SELECT * FROM t1")
77+
6278

6379
def test_md_default_db_escape(pg: Postgres, ddb, default_db_name, md_test_user):
6480
# Make sure MD is not enabled

0 commit comments

Comments
 (0)