From 32fb872bc1b322b8ededc31857a18f0d9db4f150 Mon Sep 17 00:00:00 2001
From: colettebas <colettebas@aol.com>
Date: Mon, 14 Mar 2022 20:27:24 -0400
Subject: [PATCH 01/30] Created db structure in models and schemas

---
 app/models.py  | 50 ++++++++++++++++++++++++++++++++++-
 app/schemas.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletion(-)

diff --git a/app/models.py b/app/models.py
index b99aee4..8db099c 100644
--- a/app/models.py
+++ b/app/models.py
@@ -53,4 +53,52 @@ class RecognizedPlate(Base):
     time = Column(TIMESTAMP(timezone=False))
     footage_id = Column(Integer, ForeignKey("license_footage.id"))
 
-    footage = relationship("LicenseFootage", back_populates="recognized_plates")
\ No newline at end of file
+    footage = relationship("LicenseFootage", back_populates="recognized_plates")
+
+class Interview(Base):
+    __tablename__ = "interview"
+
+    id = Column(Integer, primary_key=True, index=True)
+    full_name = Column(String, index=True)
+    created_at = Column(TIMESTAMP(timezone=False))
+    address = Column(String, index=True)
+    case = Column(Integer, ForeignKey("license_footage.id"))
+    full_text = Column(String, index=True)
+
+    interview = relationship("Interview", back_populates="interview")
+
+class Question_Answer_Pair(Base):
+    __tablename__ = "question_answer_pair"
+
+    id = Column(Integer, primary_key=True, index=True)
+    interview_id = Column(Integer, ForeignKey("interview.id")) ##Please check if this is correct
+    question = Column(String, index=True)
+    answer = Column(String, index=True)
+
+    question_answer_pair = relationship("Question_Answer_Pair", back_populates="question_answer_pair")
+
+class Question(Base):
+    __tablename__ = "question"
+
+    case = Column(Integer, index=True)
+    question = Column(String, index=True)
+
+question = relationship("Question", back_populates="question")
+
+class Additional_Question(Base):
+    __tablename__ = "additional_question"
+
+    interview_id = Column(Integer, ForeignKey("interview.id")) ##Please check if this is correct
+    question = Column(String, index=True)
+
+    additional_question = relationship("Additional_Question", back_populates="additional_question")
+
+class Answer_NER(Base):
+    __tablename__ = "answer_ner"
+
+    question_answer_pair_id = Column(Integer, ForeignKey("question_answer_pair.id")) ##Please check if this is correct
+    ner_label = Column(String, index=True)
+    start_index = Column(Integer, index=True)
+    end_index = Column(Integer, index=True)
+
+    answer_ner = relationship("Answer_NER", back_populates="answer_ner")
\ No newline at end of file
diff --git a/app/schemas.py b/app/schemas.py
index f95b215..f74ea21 100644
--- a/app/schemas.py
+++ b/app/schemas.py
@@ -85,5 +85,76 @@ class LicenseFootage(LicenseFootageBase):
     date_uploaded = datetime.now()
     recognized_plates: List[RecognizedPlate] = []
 
+    class Config:
+        orm_mode = True
+
+
+############ Transcribed Interview Data #############
+
+class InterviewCreate(BaseModel):
+    full_name: str
+    created_at: datetime = datetime
+    address: str
+    case: int
+
+class Interview(BaseModel):
+    id: int
+    full_name: str
+    created_at: datetime = datetime
+    address: str
+    case: int
+    full_text: str
+
+    class Config:
+        orm_mode = True
+
+class Question_Answer_PairCreate(BaseModel):
+    interview_id: int ##I don't know how to link this to another table
+    question: str
+    answer: str
+
+class Question_Answer_Pair(BaseModel):
+    id: int
+    interview_id: int ##I don't know how to link this to another table
+    question: str
+    answer: str
+
+    class Config:
+        orm_mode = True
+
+class QuestionCreate(BaseModel):
+    case: int
+    question: str
+
+class Question(BaseModel):
+    case: int
+    question: str
+
+    class Config:
+        orm_mode = True
+
+class Additional_QuestionCreate(BaseModel):
+    interview_id: int ##I don't know how to link this to another table
+    question: str
+
+class Additional_Question(BaseModel):
+    interview_id: int ##I don't know how to link this to another table
+    question: str
+
+    class Config:
+        orm_mode = True
+
+class Answer_NERCreate(BaseModel):
+    question_answer_pair_id: int ##I don't know how to link this to another table
+    ner_label: str
+    start_index: int
+    end_index: int
+
+class Answer_NER(BaseModel):
+    question_answer_pair_id: int ##I don't know how to link this to another table
+    ner_label: str
+    start_index: int
+    end_index: int
+
     class Config:
         orm_mode = True
\ No newline at end of file

From 382c815a85e705cba1b41483f4e273d809105e2f Mon Sep 17 00:00:00 2001
From: colettebas <colettebas@aol.com>
Date: Mon, 14 Mar 2022 21:26:39 -0400
Subject: [PATCH 02/30] created crug and main methods, no testing

---
 app/crud.py    | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++
 app/main.py    | 25 ++++++++++++++++-
 app/models.py  |  1 +
 app/schemas.py |  2 ++
 4 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/app/crud.py b/app/crud.py
index eb79cff..e7a8ce6 100644
--- a/app/crud.py
+++ b/app/crud.py
@@ -106,3 +106,77 @@ def create_license_footage_with_obj(license_footage: schemas.LicenseFootage, db:
 # Get all license plates for that license_footage id
 def get_license_plates_for_filename(footage_id: int, db: Session, skip: int = 0, limit: int = 100):
     return db.query(models.RecognizedPlate).filter(models.RecognizedPlate.footage_id == footage_id).offset(skip).limit(limit).all()
+
+# Create Interview object with audio file name
+##THIS IS ASSUMING AUDIO FILES HAS ALREADY BEEN SAVED TO DATABASE
+def create_interview(audio_filename: str, db: Session):
+
+    # Insert code to retrieve audio file from db
+
+    ####################
+    ## YOUR CODE HERE ##
+    ####################
+
+    # return type - audio file of interview
+    # NEED TO FIGURE OUT IF AUDIO FILE OR PATH OF FILE SHOULD BE RETURNED
+
+    #following this post: https://stackoverflow.com/questions/64558200/python-requests-in-docker-compose-containers
+    path = "the path to the server" + "/sendTranscription"
+    response = session.post(path, json={"audio_filename": audio_filename})
+
+    #get the full text transcription from the response
+    reponse_json = json.loads(response.text)
+    full_text = reponse_json['transcription']
+
+    # Add Interview object
+    db_message = models.Interview(filename=filename, full_name = interview.full_name,
+                                  created_at = interview.created_at,
+                                  address = interview.address,
+                                  case = interview.case, full_text = full_text)
+    db.add(db_message)
+    db.commit()
+    db.refresh(db_message)
+
+    return db_message
+
+# Create Interview object with audio file name
+##THIS IS ASSUMING AUDIO FILES HAS ALREADY BEEN SAVED TO DATABASE
+def analyze_interview(case: int, audio_filename: str, db: Session):
+
+    #get full text for this interview
+    full_text = db.query(models.Interview).filter(models.Interview.filename == audio_filename).offset(skip).limit(limit).all()
+
+    #get list of questions for this interview
+    questions = db.query(models.Question).filter(models.Question.case == case).offset(skip).limit(limit).all()
+    add_questions = db.query(models.Additional_Question).filter(models.Additional_Question.interview_id == question_answer_pair.interview_id).offset(skip).limit(limit).all()
+    questions = questions + add_questions
+
+    #following this post: https://stackoverflow.com/questions/64558200/python-requests-in-docker-compose-containers
+    path = "the path to the server" + "/analyzeText"
+    response = session.post(path, json={"full_text": full_text, "questions": questions})
+
+    #get the json from the response
+    reponse_json = json.loads(response.text)
+    all_pairs = reponse_json['transcription']
+
+    # Add each pair and NER
+    # CAN I ADD MULTIPLE OBJECTS TO THE DB IN ONE COMMIT?
+    for pair in all_pairs:
+        db_pair_message = models.Question_Answer_Pair(interview_id=interview.id, question=pair['question'], answer=pair['answer'])
+        db.add(db_pair_message)
+        db.flush()  #allows for the primary key of the pair to be generated
+        for ner in pair['ner']:
+            db_ner_message = models.Answer_NER(question_answer_pair_id=db_pair_message.id, label=ner['label'], answer=pair['answer'])
+            db.add(db_ner_message)
+        db.commit()
+        db.refresh(db_pair_message)
+
+    return all_pairs
+
+# Get all question and answer pairs for an interview
+def get_question_answer_pairs(interview_id: int, db: Session, skip: int = 0, limit: int = 100):
+    return db.query(models.Question_Answer_Pair).filter(models.Question_Answer_Pair.interview_id == interview_id).offset(skip).limit(limit).all()
+
+# Get all ner objects for an answer
+def get_ners_for_answer(question_answer_pair_id: int, db: Session, skip: int = 0, limit: int = 100):
+    return db.query(models.Answer_NER).filter(models.Answer_NER.question_answer_pair_id == question_answer_pair_id).offset(skip).limit(limit).all()
diff --git a/app/main.py b/app/main.py
index bb31948..2dfb0b9 100644
--- a/app/main.py
+++ b/app/main.py
@@ -86,4 +86,27 @@ def create_license_footage(license_footage: schemas.CreateLicenseFootageObj, db:
 @app.get("/licenses/{footage_id}/plates", response_model=List[schemas.RecognizedPlate])
 def get_plates_for_footage_id(footage_id: int, skip: int = 0, limit: int= 100, db: Session = Depends(get_db)):
     plates = crud.get_license_plates_for_filename(footage_id=footage_id, skip=skip, limit=limit, db=db)
-    return plates
\ No newline at end of file
+    return plates
+
+# Route - POST - Create a interview object and add transcribed full text
+@app.post("/transciber/{audio_filename}", response_model=schemas.LicenseFootage)
+def create_interview(license_footage: schemas.CreateLicenseFootageObj, db:Session = Depends(get_db)):
+    return crud.create_interview(audio_filename=audio_filename, db=db)
+
+# Route - POST - Create question_answer_pairs and ners from analyzed text
+#HOW ARE WE GOING TO DEAL WITH CASE NUMBERS? INCLUDE IN POST REQUEST?
+@app.post("/transciber/{audio_filename}/analyze", response_model=schemas.LicenseFootage)
+def create_interview(license_footage: schemas.CreateLicenseFootageObj, db:Session = Depends(get_db)):
+    return crud.analyze_interview(case=case, audio_filename=audio_filename, db=db)
+
+# Route - GET - get all question answer pairs for an interview_id
+@app.get("/transcriber/{interview_id}/question_answer_pairs", response_model=List[schemas.Question_Answer_Pair])
+def get_question_answer_pair_for_interview_id(interview_id: int, skip: int = 0, limit: int= 100, db: Session = Depends(get_db)):
+    pairs = crud.get_question_answer_pairs(interview_id=interview_id, skip=skip, limit=limit, db=db)
+    return pairs
+
+# Route - GET - get all ners for an question_answer_pair_id
+@app.get("/transcriber/{question_answer_pair_id}/ners", response_model=List[schemas.Question_Answer_Pair])
+def get_ner_for_question_answer_pair_id(question_answer_pair_id: int, skip: int = 0, limit: int= 100, db: Session = Depends(get_db)):
+    ners = crud.get_ners_for_answer(question_answer_pair_id=question_answer_pair_id, skip=skip, limit=limit, db=db)
+    return ners
\ No newline at end of file
diff --git a/app/models.py b/app/models.py
index 8db099c..a94bbd0 100644
--- a/app/models.py
+++ b/app/models.py
@@ -59,6 +59,7 @@ class Interview(Base):
     __tablename__ = "interview"
 
     id = Column(Integer, primary_key=True, index=True)
+    filename = Column(String, index=True)
     full_name = Column(String, index=True)
     created_at = Column(TIMESTAMP(timezone=False))
     address = Column(String, index=True)
diff --git a/app/schemas.py b/app/schemas.py
index f74ea21..2f66c8a 100644
--- a/app/schemas.py
+++ b/app/schemas.py
@@ -92,6 +92,7 @@ class Config:
 ############ Transcribed Interview Data #############
 
 class InterviewCreate(BaseModel):
+    filename: str
     full_name: str
     created_at: datetime = datetime
     address: str
@@ -99,6 +100,7 @@ class InterviewCreate(BaseModel):
 
 class Interview(BaseModel):
     id: int
+    filename: str
     full_name: str
     created_at: datetime = datetime
     address: str

From bb4c9813d5b951b03b6805758fd3d27709c83db6 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 12:19:01 -0400
Subject: [PATCH 03/30] New workflow file created

---
 .github/workflows/cd-workflow.yml | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .github/workflows/cd-workflow.yml

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
new file mode 100644
index 0000000..e69de29

From 445d1cde489f3ad76a91478b56b8743ca1b5b502 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 12:24:50 -0400
Subject: [PATCH 04/30] Added docker config files

---
 .github/workflows/cd-workflow.yml | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
index e69de29..ccd7153 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/cd-workflow.yml
@@ -0,0 +1,27 @@
+
+name: Docker CD
+
+on: [push]
+
+jobs:
+  Build-and-Push-Image:
+    runs-on: ubuntu-latest
+    needs: test
+    name: Docker Build, Tag, Push
+
+  steps:
+    - name: Checkout
+      uses: actions/checkout@v1
+    - name: Download built artifact
+      uses: actions/download-artifact@main
+      with:
+        name: webpack artifacts
+        path: public
+    - name: Build container image
+      uses: docker/build-push-action@v1
+      with:
+        username: ${{github.actor}}
+        password: ${{secrets.GITHUB_TOKEN}}
+        registry: docker.pkg.github.com
+        repository: UMass-Rescue/596-S22-Backend
+        tag_with_sha: true
\ No newline at end of file

From 3abc82892299a210e38c8772c62d7e392afe67f3 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 12:27:20 -0400
Subject: [PATCH 05/30] Resolved error with the format

---
 .github/workflows/cd-workflow.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
index ccd7153..499495b 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/cd-workflow.yml
@@ -1,7 +1,7 @@
 
 name: Docker CD
 
-on: [push]
+on: push
 
 jobs:
   Build-and-Push-Image:

From bcad4a4cf9c365d5e804df63a9b36a6e13fbbc6b Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 12:29:31 -0400
Subject: [PATCH 06/30] removed testing line

---
 .github/workflows/cd-workflow.yml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
index 499495b..0d8e5cc 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/cd-workflow.yml
@@ -6,7 +6,6 @@ on: push
 jobs:
   Build-and-Push-Image:
     runs-on: ubuntu-latest
-    needs: test
     name: Docker Build, Tag, Push
 
   steps:

From 7f562c8ecc941f2e413fdfcd10c3479a88062418 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 12:52:53 -0400
Subject: [PATCH 07/30] fixed syntax error

---
 .github/workflows/cd-workflow.yml | 32 +++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
index 0d8e5cc..5da96d0 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/cd-workflow.yml
@@ -8,19 +8,19 @@ jobs:
     runs-on: ubuntu-latest
     name: Docker Build, Tag, Push
 
-  steps:
-    - name: Checkout
-      uses: actions/checkout@v1
-    - name: Download built artifact
-      uses: actions/download-artifact@main
-      with:
-        name: webpack artifacts
-        path: public
-    - name: Build container image
-      uses: docker/build-push-action@v1
-      with:
-        username: ${{github.actor}}
-        password: ${{secrets.GITHUB_TOKEN}}
-        registry: docker.pkg.github.com
-        repository: UMass-Rescue/596-S22-Backend
-        tag_with_sha: true
\ No newline at end of file
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v1
+      - name: Download built artifact
+        uses: actions/download-artifact@main
+        with:
+          name: webpack artifacts
+          path: public
+      - name: Build container image
+        uses: docker/build-push-action@v1
+        with:
+          username: ${{github.actor}}
+          password: ${{secrets.GITHUB_TOKEN}}
+          registry: docker.pkg.github.com
+          repository: UMass-Rescue/596-S22-Backend
+          tag_with_sha: true
\ No newline at end of file

From 14bf558466fa4433aa27ca830f5a4d6951ae5d1c Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 13:22:31 -0400
Subject: [PATCH 08/30] remove artifict code . not sure what it does

---
 .github/workflows/cd-workflow.yml | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
index 5da96d0..2366015 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/cd-workflow.yml
@@ -11,11 +11,6 @@ jobs:
     steps:
       - name: Checkout
         uses: actions/checkout@v1
-      - name: Download built artifact
-        uses: actions/download-artifact@main
-        with:
-          name: webpack artifacts
-          path: public
       - name: Build container image
         uses: docker/build-push-action@v1
         with:

From 3b212948d2e7fba420509f4f12803d54a5d89e9f Mon Sep 17 00:00:00 2001
From: Sam DuBois <30542196+sdub18@users.noreply.github.com>
Date: Wed, 23 Mar 2022 13:24:38 -0400
Subject: [PATCH 09/30] Create docker-image.yml

---
 .github/workflows/docker-image.yml | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 .github/workflows/docker-image.yml

diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml
new file mode 100644
index 0000000..8529334
--- /dev/null
+++ b/.github/workflows/docker-image.yml
@@ -0,0 +1,18 @@
+name: Docker Image CI
+
+on:
+  push:
+    branches: [ main ]
+  pull_request:
+    branches: [ main ]
+
+jobs:
+
+  build:
+
+    runs-on: ubuntu-latest
+
+    steps:
+    - uses: actions/checkout@v2
+    - name: Build the Docker image
+      run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)

From 8fe0ad30b483fcb7f9ccd906085bdb12bdc9225b Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:00:26 -0400
Subject: [PATCH 10/30] made repo name all lowercase

---
 .github/workflows/cd-workflow.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/cd-workflow.yml
index 2366015..8ff3ee8 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/cd-workflow.yml
@@ -12,10 +12,10 @@ jobs:
       - name: Checkout
         uses: actions/checkout@v1
       - name: Build container image
-        uses: docker/build-push-action@v1
+        uses: docker/build-push-action@v2
         with:
           username: ${{github.actor}}
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
-          repository: UMass-Rescue/596-S22-Backend
+          repository: umass-rescue/596-S22-backend
           tag_with_sha: true
\ No newline at end of file

From 5a458c13713d60ec1ccca69da04fd3c1996061c3 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:07:53 -0400
Subject: [PATCH 11/30] Some small upgrades to the docker deploy workflow

---
 .../{cd-workflow.yml => docker-image-deploy.yml}          | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
 rename .github/workflows/{cd-workflow.yml => docker-image-deploy.yml} (82%)

diff --git a/.github/workflows/cd-workflow.yml b/.github/workflows/docker-image-deploy.yml
similarity index 82%
rename from .github/workflows/cd-workflow.yml
rename to .github/workflows/docker-image-deploy.yml
index 8ff3ee8..2f61b02 100644
--- a/.github/workflows/cd-workflow.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -1,7 +1,11 @@
 
-name: Docker CD
+name: Docker Image CD
 
-on: push
+on:
+  push:
+    branches: [ main ]
+  pull_request:
+    branches: [ main ]
 
 jobs:
   Build-and-Push-Image:

From 0d06c7daf402b3cb959fdcbb6375d4f825c9302c Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:13:32 -0400
Subject: [PATCH 12/30] renamed repo to try and create package

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index 2f61b02..c8a0d3d 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -21,5 +21,5 @@ jobs:
           username: ${{github.actor}}
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
-          repository: umass-rescue/596-S22-backend
+          repository: umass-rescue/596-S22-backend/backend
           tag_with_sha: true
\ No newline at end of file

From 705f87640e8a9b1063d0efa15a6099ece7f66790 Mon Sep 17 00:00:00 2001
From: Sam DuBois <30542196+sdub18@users.noreply.github.com>
Date: Wed, 23 Mar 2022 14:15:11 -0400
Subject: [PATCH 13/30] Update README.md

---
 README.md | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/README.md b/README.md
index 7e11ab2..2cf5fa3 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,13 @@ Backend Repo for 596RL Spring 2022
 To successfully connect to the database, we use a `.env` file, which you will need to generate using:
 `touch .env` in the backend directory. This is an untracked file in our repo. Here you will need to paste in the secrets of the backend. DM Sam DuBois and he will send you the file.
 
+```
+# PostgreSQL Container Secrets
+POSTGRES_USER=
+POSTGRES_PASSWORD=
+POSTGRES_DB=
+```
+
 
 ### ๐Ÿš€ How to Run the Container
 

From 336a50bafa94d230771bfb6d6d17d0c6994f9755 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:35:56 -0400
Subject: [PATCH 14/30] Think i might have fixed it

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index c8a0d3d..aa87956 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -16,7 +16,7 @@ jobs:
       - name: Checkout
         uses: actions/checkout@v1
       - name: Build container image
-        uses: docker/build-push-action@v2
+        uses: docker/build-push-action@v1
         with:
           username: ${{github.actor}}
           password: ${{secrets.GITHUB_TOKEN}}

From 711bf40ff95f269709ba215ad5bdadb58b2564cf Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:38:01 -0400
Subject: [PATCH 15/30] repo name change

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index aa87956..51bfb27 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -21,5 +21,5 @@ jobs:
           username: ${{github.actor}}
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
-          repository: umass-rescue/596-S22-backend/backend
+          repository: umass-rescue/596-S22-backend
           tag_with_sha: true
\ No newline at end of file

From 5b81ec0a4f334352e7073d08162d32beead78303 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:39:25 -0400
Subject: [PATCH 16/30] This one should definitely work

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index 51bfb27..fa84193 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -21,5 +21,5 @@ jobs:
           username: ${{github.actor}}
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
-          repository: umass-rescue/596-S22-backend
+          repository: umass-rescue/596-s22-backend
           tag_with_sha: true
\ No newline at end of file

From 05a3df6b96a3a607909c677f1f4558fbdc93e255 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Wed, 23 Mar 2022 14:42:44 -0400
Subject: [PATCH 17/30] Added package name to the repo

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index fa84193..bc8e118 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -21,5 +21,5 @@ jobs:
           username: ${{github.actor}}
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
-          repository: umass-rescue/596-s22-backend
+          repository: umass-rescue/596-s22-backend/backend
           tag_with_sha: true
\ No newline at end of file

From 2885ed465af5e33c2c538082efc4e9969e6abcc2 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 16:16:49 -0400
Subject: [PATCH 18/30] added to the docker file

---
 Dockerfile | 3 +++
 startup.sh | 7 +++++++
 2 files changed, 10 insertions(+)
 create mode 100644 startup.sh

diff --git a/Dockerfile b/Dockerfile
index ab315e7..9f0336e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,4 +6,7 @@ COPY requirements.txt requirements.txt
 
 RUN pip install --no-cache-dir --upgrade -r /rescue/requirements.txt
 
+# Run alembic configuration
+CMD ["./startup.sh"]
+
 COPY ./app /rescue/app
\ No newline at end of file
diff --git a/startup.sh b/startup.sh
new file mode 100644
index 0000000..ad31029
--- /dev/null
+++ b/startup.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+while ! mysqladmin ping -h"database" --silent; do
+    sleep 1
+done
+
+alembic upgrade head
\ No newline at end of file

From 26db7a3985844fece3816253a00822c533d7b104 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 16:20:33 -0400
Subject: [PATCH 19/30] Added depends on clause to the docker compose

---
 docker-compose.yml | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docker-compose.yml b/docker-compose.yml
index 6b7569f..f841985 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -16,6 +16,8 @@ services:
     build:
       context: ./
       dockerfile: Dockerfile
+    depends_on:
+      - db
     volumes:
       - ./be-data/:/backend/
     command: uvicorn app.main:app --host 0.0.0.0 --port 8000

From 5e37e76d649aaf25c67bc491803bfef9d2b69cd3 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 16:37:36 -0400
Subject: [PATCH 20/30] added alembic file to Dockerfile

---
 Dockerfile | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Dockerfile b/Dockerfile
index 9f0336e..0520893 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,6 +6,9 @@ COPY requirements.txt requirements.txt
 
 RUN pip install --no-cache-dir --upgrade -r /rescue/requirements.txt
 
+COPY alembic/ ./alembic/
+COPY alembic.ini .
+
 # Run alembic configuration
 CMD ["./startup.sh"]
 

From 5111c48bf12696f657c2b885354e76ca31c46140 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 16:52:51 -0400
Subject: [PATCH 21/30] Added alembic files to the requirements.txt and also
 changed location of db from localhost to db

---
 alembic.ini      | 2 +-
 requirements.txt | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/alembic.ini b/alembic.ini
index d5d217a..9c8a307 100644
--- a/alembic.ini
+++ b/alembic.ini
@@ -52,7 +52,7 @@ version_path_separator = os  # Use os.pathsep. Default configuration used for ne
 # are written from script.py.mako
 # output_encoding = utf-8
 
-sqlalchemy.url = postgresql://username:password@localhost:5432/default_database
+sqlalchemy.url = postgresql://username:password@db:5432/default_database
 
 
 [post_write_hooks]
diff --git a/requirements.txt b/requirements.txt
index 854abed..6ccb111 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,4 +4,6 @@ pydantic==1.9.0
 SQLAlchemy==1.4.31
 mysql-connector-python==8.0.28
 psycopg2==2.9.3
-python-dotenv==0.19.2
\ No newline at end of file
+python-dotenv==0.19.2
+alembic==1.7.7
+graphene==3.0
\ No newline at end of file

From e7bf66308f4f4227b97f3f78e3fd26dacd439f9b Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 17:06:52 -0400
Subject: [PATCH 22/30] Trying to get this startup file to run and check
 alembic. Not really working though

---
 Dockerfile         |  7 ++++---
 docker-compose.yml | 20 ++++++++++----------
 requirements.txt   |  3 ++-
 startup.sh         |  5 ++++-
 4 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 0520893..9177312 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -8,8 +8,9 @@ RUN pip install --no-cache-dir --upgrade -r /rescue/requirements.txt
 
 COPY alembic/ ./alembic/
 COPY alembic.ini .
+COPY startup.sh .
 
-# Run alembic configuration
-CMD ["./startup.sh"]
+COPY ./app /rescue/app
 
-COPY ./app /rescue/app
\ No newline at end of file
+# Run alembic configuration
+CMD ["./startup.sh"]
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index f841985..f151121 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -2,16 +2,6 @@ version: '3.8'
 
 # Setting up the PostgreSQL DB Container
 services:
-  db:
-    image: 'postgres:latest'
-    restart: always
-    env_file: # The location we use to share all of our secrets
-      - .env
-    volumes:
-      - ./db-data/:/var/lib/postgresql/data/
-    ports:
-      - 5432:5432
-  
   server:
     build:
       context: ./
@@ -25,3 +15,13 @@ services:
       - .env
     ports:
       - 8000:8000
+      
+  db:
+    image: 'postgres:latest'
+    restart: always
+    env_file: # The location we use to share all of our secrets
+      - .env
+    volumes:
+      - ./db-data/:/var/lib/postgresql/data/
+    ports:
+      - 5432:5432
diff --git a/requirements.txt b/requirements.txt
index 6ccb111..55dc373 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,4 +6,5 @@ mysql-connector-python==8.0.28
 psycopg2==2.9.3
 python-dotenv==0.19.2
 alembic==1.7.7
-graphene==3.0
\ No newline at end of file
+graphene==3.0
+mysqlclient==2.0.2
\ No newline at end of file
diff --git a/startup.sh b/startup.sh
index ad31029..9660ce1 100644
--- a/startup.sh
+++ b/startup.sh
@@ -1,7 +1,10 @@
 #!/bin/sh
 
+# Is the database read to accept connections?
+# If the database is not ready to accept connections and this script proceeds regardless, the Docker container will crash.
 while ! mysqladmin ping -h"database" --silent; do
-    sleep 1
+  sleep 1
 done
 
+# Run all migrations on the database.
 alembic upgrade head
\ No newline at end of file

From c03f644922cd252ac269e4ad0a4833015afb445f Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 17:24:50 -0400
Subject: [PATCH 23/30] Trying to change tag, may break

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index bc8e118..00c5bf0 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -22,4 +22,4 @@ jobs:
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
           repository: umass-rescue/596-s22-backend/backend
-          tag_with_sha: true
\ No newline at end of file
+          tag: latest
\ No newline at end of file

From bf9262257205aafb6c1f325626bb323c3b030068 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 17:30:17 -0400
Subject: [PATCH 24/30] changed tag to tagfs

---
 .github/workflows/docker-image-deploy.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/docker-image-deploy.yml b/.github/workflows/docker-image-deploy.yml
index 00c5bf0..9c26c03 100644
--- a/.github/workflows/docker-image-deploy.yml
+++ b/.github/workflows/docker-image-deploy.yml
@@ -22,4 +22,4 @@ jobs:
           password: ${{secrets.GITHUB_TOKEN}}
           registry: docker.pkg.github.com
           repository: umass-rescue/596-s22-backend/backend
-          tag: latest
\ No newline at end of file
+          tags: latest
\ No newline at end of file

From a13eddf663b0a5ee37042204bf2bf15106d37dce Mon Sep 17 00:00:00 2001
From: Sam DuBois <30542196+sdub18@users.noreply.github.com>
Date: Thu, 24 Mar 2022 22:46:33 -0400
Subject: [PATCH 25/30] Update README.md

---
 README.md | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 2cf5fa3..be5216c 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,10 @@ Backend Repo for 596RL Spring 2022
 - SQLAlchemy  (Locally for database manipulation)
 ```
 
+### ๐Ÿค How to Run the Package in Another Repo
+To learn how to run the backend in YOUR repo, follow this [guide](https://www.notion.so/Setting-Up-Backend-Docker-Container-dd2ce1e805e84b44a245991c96d46591) here.
+
+
 ### ๐Ÿ‘€ Configuring Secrets
 To successfully connect to the database, we use a `.env` file, which you will need to generate using:
 `touch .env` in the backend directory. This is an untracked file in our repo. Here you will need to paste in the secrets of the backend. DM Sam DuBois and he will send you the file.
@@ -32,7 +36,6 @@ POSTGRES_PASSWORD=
 POSTGRES_DB=
 ```
 
-
 ### ๐Ÿš€ How to Run the Container
 
 This repo uses Docker to containerize any processes used. In order to the run the repo, it is advised that you install docker.
@@ -41,8 +44,8 @@ Make sure the Docker and the postgresSQL library are working.
 
 From there, all you need to do to run the container is:
 ```
-docker-compose up -d --build
-docker-compose up
+docker compose build
+docker compose up
 ```
 
 ### ๐Ÿงช Useful Tools for Debugging and Testing

From e47ef69ebe2f8929bb2801a4d37644d71f48e939 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 23:09:58 -0400
Subject: [PATCH 26/30] nearly got it working but startup.sh is failing on the
 mysqladmin part

---
 docker-compose.yml | 1 -
 startup.sh         | 5 ++++-
 2 files changed, 4 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 startup.sh

diff --git a/docker-compose.yml b/docker-compose.yml
index f151121..6041d16 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -10,7 +10,6 @@ services:
       - db
     volumes:
       - ./be-data/:/backend/
-    command: uvicorn app.main:app --host 0.0.0.0 --port 8000
     env_file:
       - .env
     ports:
diff --git a/startup.sh b/startup.sh
old mode 100644
new mode 100755
index 9660ce1..30958dd
--- a/startup.sh
+++ b/startup.sh
@@ -7,4 +7,7 @@ while ! mysqladmin ping -h"database" --silent; do
 done
 
 # Run all migrations on the database.
-alembic upgrade head
\ No newline at end of file
+alembic upgrade head
+
+# start the server
+uvicorn app.main:app --host 0.0.0.0 --port 8000
\ No newline at end of file

From 37eaf5b1f1e1b0efb5a5b83a0f248218812dca5b Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 23:23:23 -0400
Subject: [PATCH 27/30] commented out the alembic stuff for right now. realized
 it was using MySQL but we use PostgreSQL

---
 startup.sh | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/startup.sh b/startup.sh
index 30958dd..7d5d5b9 100755
--- a/startup.sh
+++ b/startup.sh
@@ -2,12 +2,12 @@
 
 # Is the database read to accept connections?
 # If the database is not ready to accept connections and this script proceeds regardless, the Docker container will crash.
-while ! mysqladmin ping -h"database" --silent; do
-  sleep 1
-done
+# while [pg_isready -h db -p 5432 == ] do
+#   sleep 1
+# done
 
-# Run all migrations on the database.
-alembic upgrade head
+# # Run all migrations on the database.
+# alembic upgrade head
 
 # start the server
 uvicorn app.main:app --host 0.0.0.0 --port 8000
\ No newline at end of file

From fbce755f993980104e00ed67c6cb1e7babddf6b2 Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 23:43:01 -0400
Subject: [PATCH 28/30] trying to use pg_isready to check the server

---
 startup.sh | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/startup.sh b/startup.sh
index 7d5d5b9..60ea34a 100755
--- a/startup.sh
+++ b/startup.sh
@@ -1,13 +1,15 @@
 #!/bin/sh
 
-# Is the database read to accept connections?
-# If the database is not ready to accept connections and this script proceeds regardless, the Docker container will crash.
-# while [pg_isready -h db -p 5432 == ] do
-#   sleep 1
-# done
+while ! pg_isready 
+do
+    echo "$(date) - waiting for database to start"
+    sleep 10
+done
 
 # # Run all migrations on the database.
-# alembic upgrade head
+alembic upgrade head
+
+# until alembic upgrade head; do sleep 10; done
 
 # start the server
 uvicorn app.main:app --host 0.0.0.0 --port 8000
\ No newline at end of file

From eef82ef7fa7aa7b202c33280071602cbb9042aeb Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 23:57:51 -0400
Subject: [PATCH 29/30] Got it workinggit add .

---
 Dockerfile | 2 ++
 startup.sh | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 9177312..7b17651 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -4,6 +4,8 @@ WORKDIR /rescue
 
 COPY requirements.txt requirements.txt
 
+RUN apt-get update && apt-get install -f -y postgresql-client
+
 RUN pip install --no-cache-dir --upgrade -r /rescue/requirements.txt
 
 COPY alembic/ ./alembic/
diff --git a/startup.sh b/startup.sh
index 60ea34a..c47145a 100755
--- a/startup.sh
+++ b/startup.sh
@@ -1,9 +1,9 @@
 #!/bin/sh
 
-while ! pg_isready 
+while ! pg_isready -h "db" -p 5432
 do
     echo "$(date) - waiting for database to start"
-    sleep 10
+    sleep 5
 done
 
 # # Run all migrations on the database.

From 6e0385067bdb2fb3f6ae51614f4ae05dca6c4e0f Mon Sep 17 00:00:00 2001
From: sdub18 <sdubois@umass.edu>
Date: Thu, 24 Mar 2022 23:58:48 -0400
Subject: [PATCH 30/30] removed unwanted code

---
 startup.sh | 2 --
 1 file changed, 2 deletions(-)

diff --git a/startup.sh b/startup.sh
index c47145a..71766c6 100755
--- a/startup.sh
+++ b/startup.sh
@@ -9,7 +9,5 @@ done
 # # Run all migrations on the database.
 alembic upgrade head
 
-# until alembic upgrade head; do sleep 10; done
-
 # start the server
 uvicorn app.main:app --host 0.0.0.0 --port 8000
\ No newline at end of file