Repository: duckdb/duckdb
File: src/catalog/dependency_manager.cpp (C++)

Code before:
		return;
	}

	const auto old_info = GetLookupProperties(old_obj);
	const auto new_info = GetLookupProperties(new_obj);

	vector<DependencyInfo> dependencies;
	// Other entries that depend on us
	ScanDependents(transaction, old_info, [&](DependencyEntry &dep) {
		// It makes no sense to have a schema depend on anything
		D_ASSERT(dep.EntryInfo().type != CatalogType::SCHEMA_ENTRY);

		bool disallow_alter = true;
		switch (alter_info.type) {
		case AlterType::ALTER_TABLE: {
			auto &alter_table = alter_info.Cast<AlterTableInfo>();
			switch (alter_table.alter_table_type) {
			case AlterTableType::FOREIGN_KEY_CONSTRAINT: {
				// These alters are made as part of a CREATE or DROP table statement when a foreign key column is
				// present either adding or removing a reference to the referenced primary key table
				disallow_alter = false;
				break;
			}
			case AlterTableType::RENAME_TABLE: {
				// Renaming of tables shouldnt be blocked by anything
				disallow_alter = false;
				if (dep.EntryInfo().type == CatalogType::INDEX_ENTRY) {
					// FIXME: unless there is an index on the table, because the table name is baked into the index
					disallow_alter = true;
				}
				if (dep.EntryInfo().type == CatalogType::TABLE_ENTRY && old_obj.type == CatalogType::TABLE_ENTRY) {
					auto &table_entry = old_obj.Cast<TableCatalogEntry>();
					for (auto &constraint : table_entry.GetConstraints()) {
						if (constraint->type != ConstraintType::FOREIGN_KEY) {
							continue;
						}
						auto &fk = constraint->Cast<ForeignKeyConstraint>();
						if (fk.info.type != ForeignKeyType::FK_TYPE_PRIMARY_KEY_TABLE) {
							continue;
						}
						if (StringUtil::CIEquals(dep.EntryInfo().name, fk.info.table)) {
							// Renaming tables that are referenced by ForeignKeyConstraints is not supported
							// this currently breaks 'ScanForeignKeyTable' and causes it to infinite loop because
							// 'ReferencedTableIsOrdered' will not get a hit to fix this we should probably use OIDs in
							// places like this, so it survives a rename
							disallow_alter = true;
						}
					}
				}
				break;
			}

Review comment (suggestion):
Let's keep the changeset minimal and add this separately - can we remove the exception for `RENAME_TABLE` for now

Code after:
		return;
	}

	const auto old_info = GetLookupProperties(old_obj);
	const auto new_info = GetLookupProperties(new_obj);

	vector<DependencyInfo> dependencies;
	// Other entries that depend on us
	ScanDependents(transaction, old_info, [&](DependencyEntry &dep) {
		// It makes no sense to have a schema depend on anything
		D_ASSERT(dep.EntryInfo().type != CatalogType::SCHEMA_ENTRY);

		bool disallow_alter = true;
		switch (alter_info.type) {
		case AlterType::ALTER_TABLE: {
			auto &alter_table = alter_info.Cast<AlterTableInfo>();
			switch (alter_table.alter_table_type) {
			case AlterTableType::FOREIGN_KEY_CONSTRAINT: {
				// These alters are made as part of a CREATE or DROP table statement when a foreign key column is
				// present either adding or removing a reference to the referenced primary key table
				disallow_alter = false;
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
		if (disallow_alter) {
			throw DependencyException("Cannot alter entry \"%s\" because there are entries that "
			                          "depend on it.",
			                          old_obj.name);
		}

		auto dep_info = DependencyInfo::FromDependent(dep);
		dep_info.subject.entry = new_info;
		dependencies.emplace_back(dep_info);
	});

	// Keep old dependencies
	dependency_set_t dependents;
	ScanSubjects(transaction, old_info, [&](DependencyEntry &dep) {
		auto entry = LookupEntry(transaction, dep);
		if (!entry) {
			return;
		}

		auto dep_info = DependencyInfo::FromSubject(dep);