Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create first DB query #100

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ Create a new migration by running:
dev/gen-migration
```

If you are unfamiliar with migrations, you may follow [this guide](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md). The database is PostgreSQL and the driver is PGX.
Fill in the migrations in the generated files. If you are unfamiliar with migrations, you may follow [this guide](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md). The database is PostgreSQL and the driver is PGX.

## Modifying database queries

We use [sqlc](https://docs.sqlc.dev/en/latest/index.html) to generate the code for our DB queries. Modify the `queries.sql` file, and then run:

```sh
sqlc generate
```
2 changes: 1 addition & 1 deletion dev/docker/env
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
set -e

function docker_compose() {
docker-compose -f dev/docker/docker-compose.yml -p xmtpd "$@"
docker compose -f dev/docker/docker-compose.yml -p xmtpd "$@"
}
1 change: 1 addition & 0 deletions dev/up
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if ! which migrate &>/dev/null; then brew install golang-migrate; fi
if ! which golangci-lint &>/dev/null; then brew install golangci-lint; fi
if ! which shellcheck &>/dev/null; then brew install shellcheck; fi
if ! which mockery &>/dev/null; then brew install mockery; fi
if ! which sqlc &> /dev/null; then brew install sqlc; fi

dev/generate
dev/docker/up
5 changes: 5 additions & 0 deletions pkg/db/queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- name: InsertStagedOriginatorEnvelope :one
INSERT INTO staged_originator_envelopes(payer_envelope)
VALUES (@payer_envelope)
RETURNING
*;
31 changes: 31 additions & 0 deletions pkg/db/queries/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions pkg/db/queries/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions pkg/db/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/migrations/00001_init-schema.down.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP TABLE node_info;
DROP TABLE all_envelopes;
DROP TABLE staged_originated_envelopes;
DROP TABLE gateway_envelopes;
DROP TABLE staged_originator_envelopes;
DROP TABLE address_log;
11 changes: 6 additions & 5 deletions pkg/migrations/00001_init-schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ CREATE TABLE node_info(
CONSTRAINT is_singleton CHECK (singleton_id = 1)
);

CREATE TABLE all_envelopes(
-- Includes all envelopes, whether they were originated locally or not
CREATE TABLE gateway_envelopes(
-- used to construct gateway_sid
id BIGSERIAL PRIMARY KEY,
originator_sid BIGINT NOT NULL,
topic BYTEA NOT NULL,
originator_envelope BYTEA NOT NULL
);
-- Client queries
CREATE INDEX idx_all_envelopes_topic ON all_envelopes(topic);
CREATE INDEX idx_gateway_envelopes_topic ON gateway_envelopes(topic);
-- Node queries
CREATE UNIQUE INDEX idx_all_envelopes_originator_sid ON all_envelopes(originator_sid);
CREATE UNIQUE INDEX idx_gateway_envelopes_originator_sid ON gateway_envelopes(originator_sid);


-- Process for originating envelopes:
Expand All @@ -30,10 +31,10 @@ CREATE UNIQUE INDEX idx_all_envelopes_originator_sid ON all_envelopes(originator
-- 2.2. Atomically insert into all_envelopes and delete from originated_envelopes,
-- ignoring unique index violations on originator_sid
-- This preserves total ordering, while avoiding gaps in sequence ID's.
CREATE TABLE staged_originated_envelopes(
CREATE TABLE staged_originator_envelopes(
-- used to construct originator_sid
id BIGSERIAL PRIMARY KEY,
originator_ns TIMESTAMP NOT NULL DEFAULT now(),
originator_time TIMESTAMP NOT NULL DEFAULT now(),
payer_envelope BYTEA NOT NULL
);

Expand Down
6 changes: 3 additions & 3 deletions pkg/proto/xmtpv4/message_api/message_api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "postgresql"
queries: "pkg/db/queries.sql"
schema: "pkg/migrations"
gen:
go:
package: "queries"
out: "pkg/db/queries"
Loading