-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PostgreSQL metadata repository (#9)
Signed-off-by: Igor Shishkin <me@teran.dev>
- Loading branch information
Showing
15 changed files
with
1,155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package postgresql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func (r *repository) CreateBLOB(ctx context.Context, checksum string, size uint64, mimeType string) error { | ||
_, err := psql. | ||
Insert("blobs"). | ||
Columns( | ||
"checksum", | ||
"size", | ||
"mime_type", | ||
). | ||
Values( | ||
checksum, | ||
size, | ||
mimeType, | ||
). | ||
RunWith(r.db). | ||
ExecContext(ctx) | ||
|
||
return errors.Wrap(err, "error executing SQL query") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package postgresql | ||
|
||
func (s *postgreSQLRepositoryTestSuite) TestBlobs() { | ||
err := s.repo.CreateBLOB(s.ctx, "deadbeef", 15, "text/plain") | ||
s.Require().NoError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package postgresql | ||
|
||
import ( | ||
"context" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (r *repository) CreateContainer(ctx context.Context, name string) error { | ||
_, err := psql. | ||
Insert("containers"). | ||
Columns( | ||
"name", | ||
). | ||
Values( | ||
name, | ||
). | ||
RunWith(r.db). | ||
ExecContext(ctx) | ||
|
||
return errors.Wrap(err, "error executing SQL query") | ||
} | ||
|
||
func (r *repository) ListContainers(ctx context.Context) ([]string, error) { | ||
rows, err := psql. | ||
Select("name"). | ||
From("containers"). | ||
OrderBy("id"). | ||
RunWith(r.db). | ||
QueryContext(ctx) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error executing SQL query") | ||
} | ||
defer rows.Close() | ||
|
||
result := []string{} | ||
for rows.Next() { | ||
var r string | ||
if err := rows.Scan(&r); err != nil { | ||
return nil, errors.Wrap(err, "error decoding database result") | ||
} | ||
|
||
result = append(result, r) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (r *repository) DeleteContainer(ctx context.Context, name string) error { | ||
_, err := psql. | ||
Delete("containers"). | ||
Where(sq.Eq{"name": name}). | ||
RunWith(r.db). | ||
ExecContext(ctx) | ||
|
||
return errors.Wrap(err, "error executing SQL query") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package postgresql | ||
|
||
func (s *postgreSQLRepositoryTestSuite) TestContainerOperations() { | ||
list, err := s.repo.ListContainers(s.ctx) | ||
s.Require().NoError(err) | ||
s.Require().Equal([]string{}, list) | ||
|
||
err = s.repo.CreateContainer(s.ctx, "test-container") | ||
s.Require().NoError(err) | ||
|
||
err = s.repo.CreateContainer(s.ctx, "test-container2") | ||
s.Require().NoError(err) | ||
|
||
err = s.repo.CreateContainer(s.ctx, "test-container") | ||
s.Require().Error(err) | ||
s.Require().Equal( | ||
`error executing SQL query: pq: duplicate key value violates unique constraint "containers_name_key"`, | ||
err.Error(), | ||
) | ||
|
||
list, err = s.repo.ListContainers(s.ctx) | ||
s.Require().NoError(err) | ||
s.Require().Equal([]string{ | ||
"test-container", | ||
"test-container2", | ||
}, list) | ||
|
||
err = s.repo.DeleteContainer(s.ctx, "test-container") | ||
s.Require().NoError(err) | ||
|
||
list, err = s.repo.ListContainers(s.ctx) | ||
s.Require().NoError(err) | ||
s.Require().Equal([]string{ | ||
"test-container2", | ||
}, list) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
|
||
migrate "github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/postgres" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
_ "github.com/lib/pq" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func MigrateUp(dsn string) error { | ||
db, err := sql.Open("postgres", dsn) | ||
if err != nil { | ||
return errors.Wrap(err, "error opening database connection") | ||
} | ||
defer db.Close() | ||
|
||
if err := db.Ping(); err != nil { | ||
return errors.Wrap(err, "error pinging database") | ||
} | ||
|
||
driver, err := postgres.WithInstance(db, &postgres.Config{}) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating database instance") | ||
} | ||
|
||
m, err := migrate.NewWithDatabaseInstance( | ||
"file://migrations/sql", | ||
"postgres", driver) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating migrator instance") | ||
} | ||
|
||
return errors.Wrap(m.Up(), "error migrating database") | ||
} |
42 changes: 42 additions & 0 deletions
42
repositories/metadata/postgresql/migrations/sql/0000_initial.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
BEGIN; | ||
|
||
CREATE TABLE containers ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
CREATE TABLE versions ( | ||
id SERIAL PRIMARY KEY, | ||
container_id INT NOT NULL, | ||
name VARCHAR(64) NOT NULL, | ||
is_published BOOLEAN NOT NULL | ||
); | ||
|
||
CREATE TABLE blobs ( | ||
id SERIAL PRIMARY KEY, | ||
checksum CHAR(64) NOT NULL, | ||
size INT NOT NULL, | ||
mime_type VARCHAR(64) NOT NULL | ||
); | ||
|
||
CREATE TABLE objects ( | ||
id SERIAL PRIMARY KEY, | ||
version_id INT NOT NULL, | ||
key VARCHAR(255) NOT NULL, | ||
blob_id INT NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX containers_name_key ON containers (name); | ||
|
||
ALTER TABLE versions ADD FOREIGN KEY (container_id) REFERENCES containers (id); | ||
CREATE UNIQUE INDEX versions_container_id_name_key ON versions (container_id, name); | ||
CREATE INDEX versions_is_published_idx ON versions (is_published); | ||
|
||
ALTER TABLE objects ADD FOREIGN KEY (version_id) REFERENCES versions (id); | ||
CREATE UNIQUE INDEX objects_version_id_key_key ON objects (version_id, key); | ||
CREATE INDEX objects_key_idx ON objects (key); | ||
ALTER TABLE objects ADD FOREIGN KEY (blob_id) REFERENCES blobs (id); | ||
|
||
CREATE UNIQUE INDEX blobs_checksum_key ON blobs (checksum); | ||
|
||
COMMIT; |
Oops, something went wrong.