Repository: packit/packit-service
Issue: postgres copr build stored late?

We get multiple `WARNING  Build id: xxx not in CoprBuildDB`, but the build is stored in the end.

For example:
```
[2020-02-24 08:06:03,136: INFO/ForkPoolWorker-1] Copr event; build start: user:packit copr:user-cont-colin-237 pkg:None build:1250387 ip:3.93.12.51 pid:1865067
WARNING  Build id: 1250387 not in CoprBuildDB
[2020-02-24 08:06:03,154: WARNING/ForkPoolWorker-1] Build id: 1250387 not in CoprBuildDB
08:06:03.155 parser.py         DEBUG  We don't process this event
[2020-02-24 08:06:03,155: DEBUG/ForkPoolWorker-1] We don't process this event
[2020-02-24 08:06:03,156: INFO/ForkPoolWorker-1] Task task.steve_jobs.process_message[b7345072-ff46-42d7-ad9e-3d8571a30a5c] succeeded in 0.019566629081964493s: None
```
But after few minutes:
```
[2020-02-24 08:06:23,465: INFO/MainProcess] Received task: task.steve_jobs.process_message[c946c853-8cfa-41fa-a815-f369e29f64a9]  
Copr event; build start: user:packit copr:user-cont-colin-237 pkg:colin build:1250387 ip:34.239.135.1 pid:1865402
[2020-02-24 08:06:23,467: INFO/ForkPoolWorker-1] Copr event; build start: user:packit copr:user-cont-colin-237 pkg:colin build:1250387 ip:34.239.135.1 pid:1865402
08:06:24.347 handler.py        DEBUG  removing contents of the PV
[2020-02-24 08:06:24,347: DEBUG/ForkPoolWorker-1] removing contents of the PV
[2020-02-24 08:06:25,444: DEBUG/ForkPoolWorker-1] Found a config file '.packit.yaml' on ref 'bb69764ea4decc634fab1804b889b67c4532ebba' of the user-cont/colin repository.
[2020-02-24 08:06:25,666: DEBUG/ForkPoolWorker-1] Package config:

 ...
 
[2020-02-24 08:06:29,624: DEBUG/ForkPoolWorker-1] Reporting state of copr build ID=None state=pending, commit=bb69764ea4decc634fab1804b889b67c4532ebba
08:06:29.627 handler.py        DEBUG  Setting status for check 'packit/rpm-build-fedora-30-x86_64': RPM build has started...
```

TODO:
- [ ] flush the database after the save

Changed files:
tests_requre/test_db.py, packit_service/models.py

diff --git a/packit_service/models.py b/packit_service/models.py
index 5d01f5ab..1c08fe8a 100644
--- a/packit_service/models.py
+++ b/packit_service/models.py
@@ -105,7 +105,11 @@ class PullRequest(Base):
     def get_or_create(cls, pr_id: int, namespace: str, repo_name: str) -> "PullRequest":
         session = get_sa_session()
         project = GitProject.get_or_create(namespace=namespace, repo_name=repo_name)
-        pr = session.query(PullRequest).filter_by(pr_id=pr_id).first()
+        pr = (
+            session.query(PullRequest)
+            .filter_by(pr_id=pr_id, project_id=project.id)
+            .first()
+        )
         if not pr:
             pr = PullRequest()
             pr.pr_id = pr_id
diff --git a/tests_requre/test_db.py b/tests_requre/test_db.py
index d3ae06e4..8653402b 100644
--- a/tests_requre/test_db.py
+++ b/tests_requre/test_db.py
@@ -32,18 +32,32 @@ $ alembic upgrade head
 """
 import pytest
 
-from packit_service.models import CoprBuild, get_sa_session, SRPMBuild
+from packit_service.models import (
+    CoprBuild,
+    get_sa_session,
+    SRPMBuild,
+    PullRequest,
+    GitProject,
+)
 
 TARGET = "fedora-42-x86_64"
 
 
+def clean_db():
+    s = get_sa_session()
+    s.query(CoprBuild).delete()
+    s.query(PullRequest).delete()
+    s.query(GitProject).delete()
+    s.commit()
+
+
 @pytest.fixture()
 def a_copr_build():
     s = get_sa_session()
     s.query(CoprBuild).delete()
     s.commit()
     srpm_build = SRPMBuild.create("asd\\nqwe\\n")
-    return CoprBuild.get_or_create(
+    yield CoprBuild.get_or_create(
         pr_id=1,
         build_id="123456",
         commit_sha="687abc76d67d",
@@ -54,6 +68,7 @@ def a_copr_build():
         status="pending",
         srpm_build=srpm_build,
     )
+    clean_db()
 
 
 def test_create_copr_build(a_copr_build):
@@ -94,3 +109,31 @@ def test_copr_build_set_build_logs_url(a_copr_build):
     assert a_copr_build.build_logs_url == url
     b = CoprBuild.get_by_build_id(a_copr_build.build_id, TARGET)
     assert b.build_logs_url == url
+
+
+def test_get_or_create_pr():
+    clean_db()
+    s = get_sa_session()
+
+    try:
+        expected_pr = PullRequest.get_or_create(
+            pr_id=42, namespace="clapton", repo_name="layla"
+        )
+        actual_pr = PullRequest.get_or_create(
+            pr_id=42, namespace="clapton", repo_name="layla"
+        )
+
+        assert s.query(PullRequest).count() == 1
+        assert expected_pr.project_id == actual_pr.project_id
+
+        expected_pr = PullRequest.get_or_create(
+            pr_id=42, namespace="clapton", repo_name="cocaine"
+        )
+        actual_pr = PullRequest.get_or_create(
+            pr_id=42, namespace="clapton", repo_name="cocaine"
+        )
+
+        assert s.query(PullRequest).count() == 2
+        assert expected_pr.project_id == actual_pr.project_id
+    finally:
+        clean_db()