generated from GalvinGao/gofiber-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from penguin-statistics/dev
Release v0.0.5
- Loading branch information
Showing
31 changed files
with
804 additions
and
68 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
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,24 @@ | ||
package syncschema | ||
|
||
import ( | ||
appcli "exusiai.dev/roguestats-backend/cmd/app/cli" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func Command() *cli.Command { | ||
return &cli.Command{ | ||
Name: "sync-schema", | ||
Usage: "Sync JSON Schemas under DIR with the database.", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "dir", | ||
Aliases: []string{"d"}, | ||
Usage: "Directory containing the JSON Schemas.", | ||
Value: "./schema", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
return appcli.RunFunc(c, Run) | ||
}, | ||
} | ||
} |
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,95 @@ | ||
package syncschema | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"exusiai.dev/roguestats-backend/internal/ent" | ||
"exusiai.dev/roguestats-backend/internal/ent/research" | ||
"github.com/rs/zerolog/log" | ||
"github.com/urfave/cli/v2" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type SyncSchemaCommandDeps struct { | ||
fx.In | ||
|
||
Ent *ent.Client | ||
} | ||
|
||
func Run(c *cli.Context, d SyncSchemaCommandDeps) error { | ||
tx, err := d.Ent.BeginTx(c.Context, nil) | ||
if err != nil { | ||
return err | ||
} | ||
defer tx.Rollback() | ||
|
||
dir := c.String("dir") | ||
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() || filepath.Ext(path) != ".json" { | ||
return nil | ||
} | ||
|
||
// id is the beginning of the filename until the first dot. | ||
segments := strings.Split(info.Name(), ".") | ||
id := segments[0] | ||
name := segments[1] | ||
log.Info().Str("path", path).Str("id", id).Msg("processing research") | ||
|
||
// Check if the research exists. | ||
research, err := tx.Research.Query().Where(research.ID(id)).Only(c.Context) | ||
if err != nil { | ||
if ent.IsNotFound(err) { | ||
log.Info().Str("id", id).Msg("research does not exist, creating") | ||
// Create the research. | ||
research, err = tx.Research.Create(). | ||
SetID(id). | ||
SetName(name). | ||
Save(c.Context) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
jsonBytes, err := minifiedJsonFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Update the research schema. | ||
log.Info().Str("id", id).Msg("updating research schema") | ||
_, err = research.Update(). | ||
SetSchema(jsonBytes). | ||
Save(c.Context) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
log.Info().Msg("committing transaction") | ||
|
||
return tx.Commit() | ||
} | ||
|
||
func minifiedJsonFile(file string) ([]byte, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
return io.ReadAll(f) | ||
} |
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
Oops, something went wrong.