How do I use TOML fixtures to seed my database in a development environment using the Go Buffalo framework?

王林
Release: 2024-02-11 13:33:08
forward
549 people have browsed it

如何使用 TOML 装置在使用 Go Buffalo 框架的开发环境中为我的数据库播种?

When developing with the Go Buffalo framework, we often need to seed the database to populate the test and development environments with some initial data. TOML is a simple and easy-to-read configuration file format that can be used to define data. So how do we use TOML files to seed our database? In this article, PHP editor Xiaoxin will introduce you to a simple method to seed a database using TOML devices in the development environment of the Go Buffalo framework. Let’s take a look!

Question content

I am trying to use TOML fixtures to seed my development database in the Go Buffalo framework. However, I can't find a clear example or documentation on how to do this.

Workaround

To seed the database you can use grifts. When creating a new application, a dummy placeholder grift should be generated at grifts/db.go like this:

package grifts

import "github.com/gobuffalo/grift/grift"

var _ = grift.namespace("db", func() {

    grift.desc("seed", "seeds a database")
    grift.add("seed", func(c *grift.context) error {
        // add db seeding stuff here
        return nil
    })

})
Copy after login

I tried using model.loadfixture("seed widgets") in grift, but had some panic because it's not in the test environment. I don't think there is direct support for database seeding from toml installations, but it would be a useful feature. However, looking at the code in loadfixture we can build our own fixture loading:

Suppose you have a fixture for a scene named name = "seed widgets":

package grifts

import (
    "fmt"
    "os"
    "strings"

    "github.com/gobuffalo/grift/grift"
    "github.com/gobuffalo/suite/v4"
    "github.com/gobuffalo/suite/v4/fix"
)

var _ = grift.Namespace("db", func() {

    grift.Desc("seed", "Seeds a database")
    grift.Add("seed", func(c *grift.Context) error {
        // The DB connection will connect to the environment set in `GO_ENV` (defaults to `test`)
        // Set this environment variable in your `.env` file to `development`

        // NOTE: it may be better to put seed fixtures in a different directory
        //       to seperate concerns
        model, err := suite.NewModelWithFixtures(os.DirFS("./fixtures"))
        if err != nil {
            return err
        }

        sc, err := fix.Find("seed widgets")
        if err != nil {
            return err
        }

        for _, table := range sc.Tables {
            for _, row := range table.Row {
                q := "insert into " + table.Name
                keys := []string{}
                skeys := []string{}
                for k := range row {
                    keys = append(keys, k)
                    skeys = append(skeys, ":"+k)
                }

                q = q + fmt.Sprintf(" (%s) values (%s)", strings.Join(keys, ","), strings.Join(skeys, ","))
                if _, err = model.DB.Store.NamedExec(q, row); err != nil {
                    return err
                }
            }
        }

        return nil
    })

})
Copy after login

The above is the detailed content of How do I use TOML fixtures to seed my database in a development environment using the Go Buffalo framework?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!