本文整理汇总了Golang中github.com/cloudwan/gohan/db.InitDBWithSchemas函数的典型用法代码示例。如果您正苦于以下问题:Golang InitDBWithSchemas函数的具体用法?Golang InitDBWithSchemas怎么用?Golang InitDBWithSchemas使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了InitDBWithSchemas函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: InitializeEnvironment
// InitializeEnvironment creates new transaction for the test
func (env *Environment) InitializeEnvironment() error {
var err error
_, file := filepath.Split(env.testFileName)
env.dbFile, err = ioutil.TempFile(os.TempDir(), file)
if err != nil {
return fmt.Errorf("Failed to create a temporary file in %s: %s", os.TempDir(), err.Error())
}
env.dbConnection, err = newDBConnection(env.dbFile.Name())
if err != nil {
return fmt.Errorf("Failed to connect to database: %s", err.Error())
}
env.Environment = gohan_otto.NewEnvironment(env.dbConnection, &middleware.FakeIdentity{}, 30*time.Second)
env.SetUp()
env.addTestingAPI()
script, err := env.VM.Compile(env.testFileName, env.testSource)
if err != nil {
return fmt.Errorf("Failed to compile the file '%s': %s", env.testFileName, err.Error())
}
env.VM.Run(script)
err = env.loadSchemas()
if err != nil {
schema.ClearManager()
return fmt.Errorf("Failed to load schema for '%s': %s", env.testFileName, err.Error())
}
err = db.InitDBWithSchemas("sqlite3", env.dbFile.Name(), true, false)
if err != nil {
schema.ClearManager()
return fmt.Errorf("Failed to init DB: %s", err.Error())
}
return nil
}
示例2: getInitDbCommand
func getInitDbCommand() cli.Command {
return cli.Command{
Name: "init-db",
ShortName: "idb",
Usage: "Initialize DB backend with given schema file",
Description: `
Initialize empty database with given schema.
Setting meta-schema option will additionaly populate meta-schema table with schema resources.
Useful for development purposes.`,
Flags: []cli.Flag{
cli.StringFlag{Name: "database-type, t", Value: "sqlite3", Usage: "Backend datebase type"},
cli.StringFlag{Name: "database, d", Value: "gohan.db", Usage: "DB connection string"},
cli.StringFlag{Name: "schema, s", Value: "etc/schema/gohan.json", Usage: "Schema definition"},
cli.BoolFlag{Name: "drop-on-create", Usage: "If true, old database will be dropped"},
cli.BoolFlag{Name: "cascade", Usage: "If true, FOREIGN KEYS in database will be created with ON DELETE CASCADE"},
cli.StringFlag{Name: "meta-schema, m", Value: "", Usage: "Meta-schema file (optional)"},
},
Action: func(c *cli.Context) {
dbType := c.String("database-type")
dbConnection := c.String("database")
schemaFile := c.String("schema")
metaSchemaFile := c.String("meta-schema")
dropOnCreate := c.Bool("drop-on-create")
cascade := c.Bool("cascade")
manager := schema.GetManager()
manager.LoadSchemasFromFiles(schemaFile, metaSchemaFile)
err := db.InitDBWithSchemas(dbType, dbConnection, dropOnCreate, cascade)
if err != nil {
util.ExitFatal(err)
}
fmt.Println("DB is initialized")
},
}
}
示例3: initBenchmarkDatabase
func initBenchmarkDatabase() error {
schema.ClearManager()
manager := schema.GetManager()
manager.LoadSchemasFromFiles("../etc/apps/example.yaml", "../etc/schema/gohan.json")
err := db.InitDBWithSchemas("mysql", "[email protected](localhost:3306)/gohan_test", false, false)
if err != nil {
return err
}
return nil
}
示例4: TestOttoExtension
}
}
return nil
}
func TestOttoExtension(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Otto Extension Suite")
}
var _ = Describe("Suite set up and tear down", func() {
var _ = BeforeSuite(func() {
var err error
Expect(os.Chdir(configDir)).To(Succeed())
testDB, err = db.ConnectDB(dbType, dbFile, db.DefaultMaxOpenConn)
Expect(err).ToNot(HaveOccurred(), "Failed to connect database.")
manager := schema.GetManager()
config := util.GetConfig()
Expect(config.ReadConfig(configFile)).To(Succeed())
schemaFiles := config.GetStringList("schemas", nil)
Expect(schemaFiles).NotTo(BeNil())
Expect(manager.LoadSchemasFromFiles(schemaFiles...)).To(Succeed())
Expect(db.InitDBWithSchemas(dbType, dbFile, false, false)).To(Succeed())
})
var _ = AfterSuite(func() {
schema.ClearManager()
os.Remove(dbFile)
})
})
示例5:
var dbType string
if os.Getenv("MYSQL_TEST") == "true" {
conn = "gohan:[email protected]/gohan_test"
dbType = "mysql"
} else {
conn = "./test.db"
dbType = "sqlite3"
}
manager := schema.GetManager()
dbc, err := db.ConnectDB(dbType, conn, db.DefaultMaxOpenConn)
sqlConn = dbc.(*DB)
Expect(err).ToNot(HaveOccurred())
Expect(manager.LoadSchemasFromFiles(
"../../etc/schema/gohan.json", "../../tests/test_abstract_schema.yaml", "../../tests/test_schema.yaml")).To(Succeed())
db.InitDBWithSchemas(dbType, conn, true, false)
// Insert fixture data
fixtureDB, err := db.ConnectDB("json", "test_fixture.json", db.DefaultMaxOpenConn)
Expect(err).ToNot(HaveOccurred())
db.CopyDBResources(fixtureDB, dbc, true)
tx, err = dbc.Begin()
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
schema.ClearManager()
if os.Getenv("MYSQL_TEST") != "true" {
os.Remove(conn)
}
示例6:
})
})
})
})
})
Context("Initialization", func() {
BeforeEach(func() {
conn = "test.db"
dbType = "sqlite3"
Expect(manager.LoadSchemaFromFile("../tests/test_abstract_schema.yaml")).To(Succeed())
Expect(manager.LoadSchemaFromFile("../tests/test_schema.yaml")).To(Succeed())
})
It("Should initialize the database without error", func() {
Expect(db.InitDBWithSchemas(dbType, conn, false, false)).To(Succeed())
})
})
Context("Converting", func() {
BeforeEach(func() {
Expect(manager.LoadSchemaFromFile("test_data/conv_in.yaml")).To(Succeed())
})
It("Should do it properly", func() {
inDB, err := db.ConnectDB("yaml", "test_data/conv_in.yaml", db.DefaultMaxOpenConn)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("test_data/conv_in.db")
db.InitDBWithSchemas("sqlite3", "test_data/conv_out.db", false, false)
outDB, err := db.ConnectDB("sqlite3", "test_data/conv_out.db", db.DefaultMaxOpenConn)
示例7: initDB
func (server *Server) initDB() error {
return db.InitDBWithSchemas(server.getDatabaseConfig())
}
示例8: InitDB
//InitDB initializes database using schema.
func InitDB(dbType string, connection string, dropOnCreate bool, cascade bool) error {
err := db.InitDBWithSchemas(dbType, connection, dropOnCreate, cascade)
return err
}