# 检查数据库完整性的最佳方法是什么？

**URL:** <https://meta.discourse.org/t/what-is-the-best-way-to-check-the-integrity-of-the-database/140670>\
**Category:** Migration\
**Created:** [2020年二月5日 02:40 UTC](https://meta.discourse.org/t/what-is-the-best-way-to-check-the-integrity-of-the-database/140670 "2020-02-05T02:40:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)\
**Post date:** [2020年二月5日 02:40 UTC](https://meta.discourse.org/t/what-is-the-best-way-to-check-the-integrity-of-the-database/140670/1 "2020-02-05T02:40:07Z")

</div>

我想将一个论坛的用户、主题和帖子合并到另一个论坛中。虽然论坛在开发环境中恢复时没有问题，但在尝试合并时（目前是在全新安装中）却遇到了 `PG::ForeignKeyViolation` 错误。因此，我想知道是否有 Discourse 工具可以检查数据库的完整性？如果没有，是否有任何其他提示或建议？

_（关于数据库问题也有过一些讨论，因此我在想，如果目前还没有这样的工具，DC 团队是否认为有必要开发这样一个工具？）_

---

<div class="post-metadata">

**Author:** ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)\
**Post date:** [2020年二月5日 04:26 UTC](https://meta.discourse.org/t/what-is-the-best-way-to-check-the-integrity-of-the-database/140670/2 "2020-02-05T04:26:52Z")

</div>

我手动对比了一个运营超过 5 年的 Discourse 论坛的架构和一个全新安装的架构，差异如下：

[https://www.diffchecker.com/lZrp2jxJ](https://www.diffchecker.com/lZrp2jxJ)

主要是将 `bigint` 改为 `integer`，将 `character varying(255)` 改为 `character varying`，以及缺少了一些 `not null` 约束，但也缺少了一些索引。

以下是指出这些差异的行：

**第 1646 行 - 缺失：**

```sql
  Index "public.index_badges_on_badge_type_id"
 Column | Type | Key? | Definition Column | Type | Key? | Definition   
--------+------------------------+------+------------ ---------------+---------+------+---------------
 name | character varying(255) | yes | name badge_type_id | integer | yes | badge_type_id
btree, for table "public.badges"

```

**第 3301 行 - 缺失：**

```sql
	Index "public.index_user_badges_on_user_id"
 Column | Type | Key? | Definition 
---------+---------+------+------------
 user_id | integer | yes | user_id
btree, for table "public.user_badges"

```

**第 4639 行 - 缺失：**

```plaintext
	      Index "public.schema_migrations_pkey"
 Column | Type | Key? | Definition 
---------+-------------------+------+------------
 version | character varying | yes | version
primary key, btree, for table "public.schema_migrations"

```

**第 5487 行 - 部分缺失：**

```plaintext
                                       Table "public.topic_search_data"
   Column | Type | Collation | Nullable | Default Column | Type | Collation | Nullable | Default                       
-------------+------------------------+-----------+----------+--------- -------------+-------------------+-----------+----------+-----------------------------------------------------
 topic_id | integer | | not null | topic_id | integer | | not null | nextval('topic_search_data_topic_id_seq'::regclass)
 raw_data | text | | | raw_data | text | | | 
 locale | character varying(255) | | not null | locale | character varying | | not null | 
 search_data | tsvector | | | search_data | tsvector | | | 
 version | integer | | | 0 version | integer | | | 0

```

**第 5506 行 - 缺失：**

```plaintext
           Sequence "public.topic_search_data_topic_id_seq"
  Type | Start | Minimum | Maximum | Increment | Cycles? | Cache 
---------+-------+---------+------------+-----------+---------+-------
 integer | 1 | 1 | 2147483647 | 1 | no | 1
Owned by: public.topic_search_data.topic_id

```

**第 6044 行 - 缺失：**

```plaintext
    "index_user_badges_on_user_id" btree (user_id)

```

我需要对这些问题担心吗？我应该手动添加这些索引吗？还有其他想法或有什么特别需要注意的地方吗（这里快凌晨 5 点了，我快撑不住眼皮了 🙈)

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2023年十二月31日 07:13 UTC](https://meta.discourse.org/t/what-is-the-best-way-to-check-the-integrity-of-the-database/140670/4 "2023-12-31T07:13:25Z")

</div>


