Skip to content

Commit 79e240c

Browse files
authored
feat: database utility scripts (#17)
1 parent cce4660 commit 79e240c

File tree

7 files changed

+154
-4
lines changed

7 files changed

+154
-4
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"log/slog"
7+
"os"
8+
9+
"github.com/GenerateNU/platemate/internal/config"
10+
"github.com/GenerateNU/platemate/internal/storage/mongo"
11+
"github.com/GenerateNU/platemate/internal/xslog"
12+
"github.com/joho/godotenv"
13+
)
14+
15+
/*
16+
Adds two example fields to the collection passed via
17+
the collection flag.
18+
Example usage: go run cmd/db/example/main.go -collection=collectionName
19+
Applies empty strings as default values
20+
*/
21+
func main() {
22+
ctx := context.Background()
23+
name := flag.String("coll", "users", "name of the collection to apply schema to")
24+
25+
flag.Parse()
26+
if *name == "" {
27+
fatal(ctx, "name flag is required", nil)
28+
}
29+
30+
if err := godotenv.Load(); err != nil {
31+
fatal(ctx, "Failed to load .env", err)
32+
}
33+
config, err := config.Load()
34+
if err != nil {
35+
fatal(ctx, "Failed to load config", err)
36+
}
37+
38+
db, err := mongo.New(ctx, config.Atlas)
39+
if err != nil {
40+
fatal(ctx, "Failed to connect to MongoDB in main", err)
41+
}
42+
43+
if err := db.ApplySchema(ctx, *name); err != nil {
44+
fatal(ctx, "Failed to apply schema", err)
45+
} else {
46+
slog.LogAttrs(ctx, slog.LevelInfo, "Schema applied to", slog.String("collection", *name), slog.String("Environment", db.DB.Name()))
47+
}
48+
}
49+
50+
func fatal(ctx context.Context, msg string, err error) {
51+
slog.LogAttrs(
52+
ctx,
53+
slog.LevelError,
54+
msg,
55+
xslog.Error(err),
56+
)
57+
os.Exit(1)
58+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
read -p "Enter Collection Name: " name
2+
go run cmd/db/apply_schema/main.go -coll $name

backend/cmd/db/clone_prod/main.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"log/slog"
7+
"os"
8+
9+
"github.com/GenerateNU/platemate/internal/config"
10+
"github.com/GenerateNU/platemate/internal/storage/mongo"
11+
"github.com/GenerateNU/platemate/internal/xslog"
12+
"github.com/joho/godotenv"
13+
)
14+
15+
/*
16+
Adds two example fields to the collection passed via
17+
the collection flag.
18+
Example usage: go run cmd/db/example/main.go -collection=collectionName
19+
Applies empty strings as default values
20+
*/
21+
func main() {
22+
ctx := context.Background()
23+
name := flag.String("name", "users", "name of the new copy of production")
24+
25+
flag.Parse()
26+
if *name == "" {
27+
fatal(ctx, "name flag is required", nil)
28+
}
29+
30+
if err := godotenv.Load(); err != nil {
31+
fatal(ctx, "Failed to load .env", err)
32+
}
33+
config, err := config.Load()
34+
if err != nil {
35+
fatal(ctx, "Failed to load config", err)
36+
}
37+
38+
config.Atlas.Environment = "Production"
39+
40+
db, err := mongo.New(ctx, config.Atlas)
41+
if err != nil {
42+
fatal(ctx, "Failed to connect to MongoDB in main", err)
43+
}
44+
45+
if err := db.Clone(ctx, db.Collections, *name, 200); err != nil {
46+
fatal(ctx, "Failed to add example fields", err)
47+
}
48+
}
49+
50+
func fatal(ctx context.Context, msg string, err error) {
51+
slog.LogAttrs(
52+
ctx,
53+
slog.LevelError,
54+
msg,
55+
xslog.Error(err),
56+
)
57+
os.Exit(1)
58+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
read -p "Enter New Environment Name: " name
2+
go run cmd/db/clone_prod/main.go -name $name
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ func main() {
3939
fatal(ctx, "Failed to connect to MongoDB", err)
4040
}
4141

42-
if err := db.CreateExampleFields(ctx, *collection); err != nil {
43-
fatal(ctx, "Failed to add example fields", err)
42+
if err := db.CreateCollection(ctx, "users"); err != nil {
43+
fatal(ctx, "Failed to create collection", err)
44+
} else {
45+
slog.LogAttrs(ctx, slog.LevelInfo, "Collection created", slog.String("Collection", "users"), slog.String("Environment", db.DB.Name()))
4446
}
4547
}
4648

backend/internal/storage/mongo/bulk.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mongo
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"slices"
78

89
"go.mongodb.org/mongo-driver/bson"
@@ -13,6 +14,12 @@ Perform a collection operation across all environments
1314
- Do not preform unless given approval from a Tech Lead
1415
*/
1516
func (db *DB) BulkOperation(ctx context.Context, operation CollectionOperation, arg string) error {
17+
slog.LogAttrs(
18+
ctx,
19+
slog.LevelInfo,
20+
"Performing bulk operation",
21+
slog.String("arg", arg),
22+
)
1623
databaseNames, err := db.Client.ListDatabaseNames(ctx, bson.D{})
1724
databaseNames = filterSystemDatabases(databaseNames)
1825

@@ -22,13 +29,20 @@ func (db *DB) BulkOperation(ctx context.Context, operation CollectionOperation,
2229
for _, name := range databaseNames {
2330
if err := operation(ctx, arg); err != nil {
2431
return fmt.Errorf("failed to perform operation on %s: %w", name, err)
32+
} else {
33+
slog.LogAttrs(
34+
ctx,
35+
slog.LevelInfo,
36+
"Successfully performed operation on database",
37+
slog.String("database_name", name),
38+
)
2539
}
2640
}
2741
return nil
2842
}
2943

3044
func filterSystemDatabases(databases []string) []string {
31-
systemNames := []string{"local","admin"}
45+
systemNames := []string{"local","admin","encryption", "config"}
3246
ret := make([]string,0);
3347
for _, v := range databases {
3448
if !slices.Contains(systemNames,v){

nix_modules/devenv.nix

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,21 @@
6464
cd "$DEVENV_ROOT"/backend
6565
${pkgs.gum}/bin/gum spin --spinner dot --title "go mod tidy" -- go mod tidy
6666
${pkgs.rubyPackages.dotenv}/bin/dotenv -i -f ""$DEVENV_ROOT"/.env" -- \
67-
go run cmd/db/example/main.go
67+
go run cmd/db/script/main.go
68+
'';
69+
};
70+
"database-clone" = {
71+
description = "Clone the production database for testing";
72+
exec = ''
73+
cd "$DEVENV_ROOT"/backend
74+
sh ./cmd/db/clone_prod/script.sh
75+
'';
76+
};
77+
"database-apply-schema" = {
78+
description = "Apply a schema to a given collection";
79+
exec = ''
80+
cd "$DEVENV_ROOT"/backend
81+
sh ./cmd/db/apply_schema/script.sh
6882
'';
6983
};
7084
"frontend-lint" = {

0 commit comments

Comments
 (0)