mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 09:22:43 +03:00
a398d3b190
GITHUB_PR_NUMBER: 6111 GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/6111 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 1f6517acfacb58c566bb5e48f74ea0dfa5c6f063
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package seed
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
"github.com/hasura/graphql-engine/cli/migrate/database/hasuradb"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
// ApplySeedsToDatabase will read all .sql files in the given
|
|
// directory and apply it to hasura
|
|
func ApplySeedsToDatabase(ec *cli.ExecutionContext, fs afero.Fs, m *migrate.Migrate, filenames []string, datasource string) error {
|
|
seedQuery := hasuradb.HasuraInterfaceBulk{
|
|
Type: "bulk",
|
|
Args: make([]interface{}, 0),
|
|
}
|
|
|
|
if len(filenames) > 0 {
|
|
for _, filename := range filenames {
|
|
absFilename := filepath.Join(ec.SeedsDirectory, filename)
|
|
b, err := afero.ReadFile(fs, absFilename)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error opening file")
|
|
}
|
|
q := hasuradb.HasuraInterfaceQuery{
|
|
Type: "run_sql",
|
|
Args: hasuradb.RunSQLInput{
|
|
Source: datasource,
|
|
SQL: string(b),
|
|
},
|
|
}
|
|
seedQuery.Args = append(seedQuery.Args, q)
|
|
}
|
|
} else {
|
|
err := afero.Walk(fs, ec.SeedsDirectory, func(path string, file os.FileInfo, err error) error {
|
|
if file == nil || err != nil {
|
|
return err
|
|
}
|
|
if !file.IsDir() && filepath.Ext(file.Name()) == ".sql" {
|
|
b, err := afero.ReadFile(fs, path)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error opening file")
|
|
}
|
|
q := hasuradb.HasuraInterfaceQuery{
|
|
Type: "run_sql",
|
|
Args: hasuradb.RunSQLInput{
|
|
SQL: string(b),
|
|
Source: datasource,
|
|
},
|
|
}
|
|
seedQuery.Args = append(seedQuery.Args, q)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return errors.Wrap(err, "error walking the directory path")
|
|
}
|
|
}
|
|
if len(seedQuery.Args) == 0 {
|
|
return fmt.Errorf("no SQL files found in %s", ec.SeedsDirectory)
|
|
}
|
|
return m.ApplySeed(seedQuery)
|
|
}
|