由于表

时间:2015-09-23 14:23:54

标签: ruby-on-rails ruby postgresql rspec database-cleaner

我有一个简单规格的Rails 4应用程序。当我运行规范时,需要26秒。如果我查看日志,我可以看到:

   (38.5ms)  ALTER TABLE "schema_migrations" DISABLE TRIGGER ALL;ALTER TABLE .............# tons of tables
   (10.3ms)            SELECT schemaname || '.' || tablename
          FROM pg_tables
          WHERE
            tablename !~ '_prt_' AND
            tablename <> 'schema_migrations' AND
            schemaname = ANY (current_schemas(false))

   (2.3ms)  select table_name from information_schema.views where table_schema = 'lap-test'
   (10708.2ms)  TRUNCATE TABLE "public"."bounded_facets_service_boutiques", "public"."versions", ..... # Tons of tables
   (10823.0ms)  TRUNCATE TABLE "public"."bounded_facets_service_boutiques", "public"."versions", .... #Tons of tables
   (26.2ms)  ALTER TABLE "schema_migrations" ENABLE TRIGGER ALL;ALTER TABLE "bounded_facets_service_boutiques" ENABLE TRIGGER ALL;ALTER TABLE "versions" .... #Tons of tables
   (0.6ms)  BEGIN
   (0.3ms)  COMMIT
   (0.2ms)  BEGIN
   (0.2ms)  SAVEPOINT active_record_1
   (0.2ms)  RELEASE SAVEPOINT active_record_1
   (0.2ms)  SAVEPOINT active_record_1
   (0.5ms)  SAVEPOINT active_record_2
  Administrator Exists (1.9ms)  SELECT  1 AS one FROM "administrators" WHERE "administrators"."email" = 'person1@labandprocess.com' LIMIT 1
  SQL (2.3ms)  INSERT INTO "administrators" .....
   (0.6ms)  RELEASE SAVEPOINT active_record_2
   (0.4ms)  ROLLBACK TO SAVEPOINT active_record_1
   (0.6ms)  ROLLBACK

如您所见,截断需要10秒2次。

我使用数据库清理器:

RSpec.configure do |config|
  config.include Devise::TestHelpers, type: :controller

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

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

我测试了其他没有结果的策略。

开发模式中有很多种子,但它在另一个数据库中。这个项目有107个模型。

有解决方案吗?

0 个答案:

没有答案