sql >> データベース >  >> RDS >> PostgreSQL

zeusを使用したキュウリとRSpecのテスト:Postgresは他のユーザーによってアクセスされています

    この回答 に触発されました 、次のdatabase.rakeを作成しました ファイル。元の回答がPostgreSQL9.1でのみ機能したのに対し、これはPostgreSQL9.2でも機能するように変更されています。このメカニズムは最も美しいものではありません。9.1コマンドが失敗すると、9.2コマンドを実行するだけです。しかし、最も重要なこと:それは機能します!

    #{Rails.root}/lib/tasks/databases.rake
    # monkey patch ActiveRecord to avoid There are n other session(s) using the database.
    def drop_database(config)
      case config['adapter']
      when /mysql/
        ActiveRecord::Base.establish_connection(config)
        ActiveRecord::Base.connection.drop_database config['database']
      when /sqlite/
        require 'pathname'
        path = Pathname.new(config['database'])
        file = path.absolute? ? path.to_s : File.join(Rails.root, path)
    
        FileUtils.rm(file)
      when /postgresql/
        begin
          ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
          ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
            if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/
              ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
            end
          end
          ActiveRecord::Base.connection.drop_database config['database']
        rescue # in PG 9.2 column procpid was renamed pid and the query status is checked not using 'current_query' but using 'state'
          ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
          ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by pid;").each do |x|
            if config['database'] == x['datname'] && x['state'] =~ /idle/
              ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['pid']})")
            end
          end
          ActiveRecord::Base.connection.drop_database config['database']
        end
      end
    end
    


    1. セッション変数:データ量が多すぎますか?

    2. SQLServerのDELETEテーブルとTRUNCATEテーブルの違い

    3. NetBeans 9.0、パート2のJava9でJShellを使用する

    4. テーブル式の基礎、パート9 –ビュー、派生テーブルおよびCTEとの比較