Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

PostgreSQL adapter method .tables return table names without schema

未关闭
#6 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
postgresql, ruby
领域
databases

调研方向

先从 database_cleaner/sequel/deletion.rb 中的 delete_tables 方法和 database_cleaner/sequel/truncation.rb 中的 tables_to_truncate 方法开始,然后检查 adapters/shared/postgres.rb 中 tables 方法附近的代码。验证来自多个 schema 的表是否使用带 schema 限定的标识符表示,并验证删除或截断是否会从列出的每个 schema 中移除记录。

由索引模型根据 Issue 内容生成。

描述

Take a look at #1154 issue of Sequel gem https://github.com/jeremyevans/sequel/issues/1154.

My database consists of multiple schemas with some number of tables with the same name, like this:

       Schema    |          Name           | Type    |  Owner   
-----------------+-------------------------+---------+----------
 type1           | tasks                   | table   | user
 type1           | updates                 | table   | user
 type2           | tasks                   | table   | user
 type2           | updates                 | table   | user
 type3           | tasks                   | table   | user
 type3           | updates                 | table   | user
  ...

I am using database_cleaner (1.5.1) and rspec (3.4.0) for my tests. Database config contains search_path for all the schemas. When I try to clear DB with :deletion method (or :truncate) - it didn't remove any records.

DatabaseCleaner[:sequel, { connection: DB }]

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:deletion)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

Database contains all the data that was inserted during the tests. I open log files and see this:

I, INFO -- : (0.000349s) ALTER TABLE "updates" DISABLE TRIGGER ALL
I, INFO -- : (0.000444s) ALTER TABLE "tasks" DISABLE TRIGGER ALL
I, INFO -- : (0.000236s) ALTER TABLE "updates" DISABLE TRIGGER ALL
I, INFO -- : (0.000232s) ALTER TABLE "tasks" DISABLE TRIGGER ALL
I, INFO -- : (0.000202s) ALTER TABLE "updates" DISABLE TRIGGER ALL
I, INFO -- : (0.000200s) ALTER TABLE "tasks" DISABLE TRIGGER ALL
....
I, INFO -- : (0.000349s) DELETE FROM "tasks"
I, INFO -- : (0.000444s) DELETE FROM "updates"
I, INFO -- : (0.000249s) DELETE FROM "tasks"
I, INFO -- : (0.000314s) DELETE FROM "updates"
...

As you can see - no schema provided to PSQL, just raw table names. Ok, I open Sequel adapter of database_cleaner and learned how it tries to delete the data from the tables:

    # database_cleaner/sequel/deleteion.rb : 30-46
    def delete_tables(db, tables)  # tables - connection.tables method of Sequel
      tables.each do |table|
        db[table.to_sym].delete
      end
    end

   # database_cleaner/sequel/truncation.rb : 64-67
    def tables_to_truncate(db)
       (@only || db.tables.map(&:to_s)) - @tables_to_exclude
    end

As we see, db_cleaner gem get tables name with sequel .tables method. Ok, go to Sequel code (adapters/shared/postgres.rb - 525 line of code) and see that tables method return table names WITHOUT schemas.

2.1.7 :004 > DB.tables # DB - sequel connection
 => [:updates, :tasks, :updates, :tasks, :updates, :schema_info, :tasks, :tasks, :tasks, :updates, :tasks, :updates]

Thats why no one record is removed and that is why database_cleaner doesn't remove any data.

For correct removing of the data from the tables with schema database_cleaner gem needs something like this:

[:type1__updates, :type1__tasks, :type2__updates, :type2__tasks, :type3__updates, :type3__tasks ]

Thanks!

主要语言
Ruby
星标
19
派生
11
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DatabaseCleaner/database_cleaner-sequel 的其他 Issue

查看 DatabaseCleaner/database_cleaner-sequel 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。