Skip to content

Commit

Permalink
Database model: removes migrations in favor of OpenSauced API data mo…
Browse files Browse the repository at this point in the history
…del (#30)

- Adjusts SQL queries to conform to the data model used by OpenSauced
  API.
- Adjusts hack/setup.sh to work with migration under hack/pizza.sql
  - Adds a few small other fixes to ensure the hack/setup.sh script
    works as intended

Signed-off-by: John McBride <[email protected]>
  • Loading branch information
jpmcb authored Aug 8, 2023
1 parent e99d988 commit 65f6e18
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 220 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ local:
go build -o build/pizza-oven main.go

build:
docker buildx bake
docker buildx build \
--load \
--tag opensauced-pizza:latest \
.

setup-test-env:
./hack/setup.sh
Expand Down
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# pizza
This is an engine that sources git commits and turns them to insights. Ideally this will be useful for any open source maintainer trying to get insight into their open source.

<img width="1350" alt="Screen Shot 2023-05-11 at 8 46 18 AM" src="https://github.com/open-sauced/pizza/assets/5713670/b91989d8-df6d-4631-8d7d-3089b76ee113">

## Scope
Build a docker container clones a repo and stores the contributors and their contributions (commits) in a relational tables. This data should be store in a postgres database (😃).

## Bonus
- Make this work with orchestration that fetches the latest data on a cron every hour.
- Add a queue to assist in fetch content without rate limiting.
- Add the ability to fetch all repos in an org.
- Visualize this data somehow.

## Gotchas
- Large repos like k8s or linux will trip `git clone` the rate limiter if done multiple times in an hour. How would you account for fetching large repos with lots of data?
<div align="center">
<br>
<img alt="Open Sauced" src="https://i.ibb.co/7jPXt0Z/logo1-92f1a87f.png" width="300px">
<h1>🍕 Pizza Oven Micro-service 🍕</h1>
<strong>A Go micro-service that sources git commits from any arbitrary git repo and indexes them into a postgres database.</strong>
<br>
</div>
<br>
<p align="center">
<img src="https://img.shields.io/github/languages/code-size/open-sauced/pizza" alt="GitHub code size in bytes">
<a href="https://github.com/open-sauced/pizza/issues">
<img src="https://img.shields.io/github/issues/open-sauced/pizza" alt="GitHub issues">
</a>
<a href="https://github.com/open-sauced/api.opensauced.pizza/releases">
<img src="https://img.shields.io/github/v/release/open-sauced/pizza.svg?style=flat" alt="GitHub Release">
</a>
<a href="https://discord.gg/U2peSNf23P">
<img src="https://img.shields.io/discord/714698561081704529.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2" alt="Discord">
</a>
<a href="https://twitter.com/saucedopen">
<img src="https://img.shields.io/twitter/follow/saucedopen?label=Follow&style=social" alt="Twitter">
</a>
</p>

## 🚀 Routes

Expand Down Expand Up @@ -50,8 +57,13 @@ There are a few required dependencies to build and run the pizza-oven service:
### Local postgres database setup

You can use a local postgres database (like with `brew services start postgresql`)
[configured to accept SSL connections](https://www.postgresql.org/docs/current/ssl-tcp.html),
an `.env` file with the database's secrets,
[configured to accept SSL connections](https://www.postgresql.org/docs/current/ssl-tcp.html)
that has been bootstrapped with the [OpenSauced API migrations](https://github.com/open-sauced/api/tree/beta/migrations).
It is highly recommended to follow the instructions in the API repository to get a locally running postgres going
that can be used with the `pizza` oven micro-service.

You'll also need an `.env` file with the database's secrets
(see `.env.example` in this repo for the required env variables),
and a locally running version of the Go application.

To start the pizza oven service:
Expand Down
6 changes: 3 additions & 3 deletions hack/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ spec:
spec:
containers:
- name: pizza-oven
image: pizza-oven:latest
image: opensauced-pizza:latest
imagePullPolicy: IfNotPresent
command: ["/usr/bin/pizza-oven"]
env:
Expand Down Expand Up @@ -221,7 +221,7 @@ echo
echo "########################################################################"
echo "Applying database migrations"
echo
wrapped_psql -f ${ROOTPATH}/migrations/20230622103000_baseline.sql
wrapped_psql -f ${ROOTPATH}/hack/pizza.sql

# Build the pizza-oven container
echo
Expand All @@ -235,7 +235,7 @@ echo
echo "########################################################################"
echo "Uploading pizza oven container to kind cluster"
echo
kind --name opensauced-pizza load docker-image pizza-oven:latest
kind --name opensauced-pizza load docker-image opensauced-pizza:latest

echo
echo "########################################################################"
Expand Down
191 changes: 0 additions & 191 deletions migrations/20230622103000_baseline.sql

This file was deleted.

14 changes: 7 additions & 7 deletions pkg/database/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,48 +47,48 @@ func NewPizzaOvenDbHandler(host, port, user, pwd, dbName string) *PizzaOvenDbHan
// GetRepositoryID queries the id of a repository based on its git URL
func (p PizzaOvenDbHandler) GetRepositoryID(insight insights.CommitInsight) (int, error) {
var id int
err := p.db.QueryRow("SELECT id FROM public.repos WHERE git_url=$1", insight.RepoURLSource).Scan(&id)
err := p.db.QueryRow("SELECT id FROM public.baked_repos WHERE clone_url=$1", insight.RepoURLSource).Scan(&id)
return id, err
}

// InsertRepository inserts a git repository by its git_url
func (p PizzaOvenDbHandler) InsertRepository(insight insights.CommitInsight) (int, error) {
var id int
err := p.db.QueryRow("INSERT INTO public.repos(git_url) VALUES($1) RETURNING id", insight.RepoURLSource).Scan(&id)
err := p.db.QueryRow("INSERT INTO public.baked_repos(clone_url) VALUES($1) RETURNING id", insight.RepoURLSource).Scan(&id)
return id, err
}

// GetAuthorID queries the id of an author by their email
func (p PizzaOvenDbHandler) GetAuthorID(insight insights.CommitInsight) (int, error) {
var id int
err := p.db.QueryRow("SELECT id FROM public.users WHERE login=$1", insight.AuthorEmail).Scan(&id)
err := p.db.QueryRow("SELECT id FROM public.commit_authors WHERE commit_author_email=$1", insight.AuthorEmail).Scan(&id)
return id, err
}

// InsertAuthor inserts an author by their email
func (p PizzaOvenDbHandler) InsertAuthor(insight insights.CommitInsight) (int, error) {
var id int
err := p.db.QueryRow("INSERT INTO public.users(login) VALUES($1) RETURNING id", insight.AuthorEmail).Scan(&id)
err := p.db.QueryRow("INSERT INTO public.commit_authors(commit_author_email) VALUES($1) RETURNING id", insight.AuthorEmail).Scan(&id)
return id, err
}

// GetCommitID queries the id of a given commit based on its hash
func (p PizzaOvenDbHandler) GetCommitID(repoID int, insight insights.CommitInsight) (int, error) {
var id int
err := p.db.QueryRow("SELECT id FROM public.commits WHERE repo_id=$1 AND commit_hash=$2", repoID, insight.Hash).Scan(&id)
err := p.db.QueryRow("SELECT id FROM public.commits WHERE baked_repo_id=$1 AND commit_hash=$2", repoID, insight.Hash).Scan(&id)
return id, err
}

// InsertCommit inserts a commit based on its commit hash
func (p PizzaOvenDbHandler) InsertCommit(insight insights.CommitInsight, authorID int, repoID int) error {
_, err := p.db.Exec("INSERT INTO public.commits(commit_hash, user_id, repo_id, commit_date) VALUES($1, $2, $3, $4)", insight.Hash, authorID, repoID, insight.Date)
_, err := p.db.Exec("INSERT INTO public.commits(commit_hash, commit_author_id, baked_repo_id, commit_date) VALUES($1, $2, $3, $4)", insight.Hash, authorID, repoID, insight.Date)
return err
}

// GetLastCommit returns time.Time of the last git commit for the given repoID
func (p PizzaOvenDbHandler) GetLastCommit(repoID int) (time.Time, error) {
var dateTime sql.NullTime
err := p.db.QueryRow("SELECT commit_date FROM public.commits WHERE commit_date IS NOT NULL AND repo_id=$1 ORDER BY commit_date DESC LIMIT 1", repoID).Scan(&dateTime)
err := p.db.QueryRow("SELECT commit_date FROM public.commits WHERE commit_date IS NOT NULL AND baked_repo_id=$1 ORDER BY commit_date DESC LIMIT 1", repoID).Scan(&dateTime)
if err != nil {
if err == sql.ErrNoRows {
// When no rows are returned, use an empty time.Time struct which
Expand Down

0 comments on commit 65f6e18

Please sign in to comment.