Пример #1
0
func BuildDatabase(
	schemaPath *string,
	migrationsPath *string,
) (*gorm.DB, error) {
	f, _ := ioutil.TempFile("/tmp", "sqlite")
	db, err := gorm.Open("sqlite3", f.Name())

	if err != nil {
		return nil, err
	}

	if schemaPath != nil {
		blob, err := ioutil.ReadFile(*schemaPath)

		if err != nil {
			return nil, err
		}

		if _, err := db.DB().Exec(string(blob)); err != nil {
			return nil, err
		}
	}

	if migrationsPath != nil {
		dbPath := fmt.Sprintf("sqlite3://%s", f.Name())
		errs, ok := migrate.UpSync(dbPath, *migrationsPath)

		if !ok {
			strErrs := []string{}
			for _, migrationError := range errs {
				strErrs = append(strErrs, migrationError.Error())
			}
			return nil, errors.New(strings.Join(strErrs, ","))
		}
	}

	return &db, nil
}
Пример #2
0
func BuildDatabase(
	schemaPath *string,
	migrationsPath *string,
) (*gorm.DB, error) {
	postgresURL := os.Getenv("POSTGRES_URL")

	if postgresURL == "" {
		postgresURL = defaultPostgresURL
	}

	postgresURLSlipped := strings.Split(postgresURL, "/")

	database := strings.Split(
		postgresURLSlipped[len(postgresURLSlipped)-1],
		"?",
	)[0]

	plainURL := postgresURLSlipped[0 : len(postgresURLSlipped)-1]
	plainDB, err := sql.Open(
		"postgres",
		fmt.Sprintf("%s?sslmode=disable", strings.Join(plainURL, "/")),
	)

	if err != nil {
		return nil, err
	}

	plainDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", database))
	plainDB.Exec(fmt.Sprintf("CREATE DATABASE %s", database))

	plainDB.Close()

	db, err := gorm.Open("postgres", postgresURL)

	if err != nil {
		db.Close()
		return nil, err
	}

	if schemaPath != nil {
		blob, err := ioutil.ReadFile(*schemaPath)

		if err != nil {
			db.Close()
			return nil, err
		}

		if _, err := db.DB().Exec(string(blob)); err != nil {
			db.Close()
			return nil, err
		}
	}

	if migrationsPath != nil {
		errs, ok := migrate.UpSync(postgresURL, *migrationsPath)

		if !ok {
			strErrs := []string{}
			for _, migrationError := range errs {
				strErrs = append(strErrs, migrationError.Error())
			}

			db.Close()
			return nil, errors.New(strings.Join(strErrs, ","))
		}
	}

	return &db, nil
}