コード例 #1
0
ファイル: gocql.go プロジェクト: upfluence/goutils
func BuildKeySpace(migrationsPath *string) (*gocql.Session, error) {
	var (
		cassandraIP = os.Getenv("CASSANDRA_IP")
		keySpace    = os.Getenv("CASSANDRA_KEY_SPACE")
	)

	if cassandraIP == "" {
		cassandraIP = defaultCassandraIP
	}

	if keySpace == "" {
		keySpace = fmt.Sprintf("%s%d", defaultKeyspace, rand.Int31())
	}

	cluster := gocql.NewCluster(cassandraIP)
	cluster.Consistency = gocql.All
	cluster.ProtoVersion = protocolVersion

	session, err := cluster.CreateSession()

	if err != nil {
		return nil, err
	}

	defer session.Close()

	session.Query(`DROP KEYSPACE IF EXISTS ` + keySpace).Exec()
	session.Query(
		`CREATE KEYSPACE ` + keySpace + ` WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }`,
	).Exec()

	if migrationsPath != nil {
		errs, ok := migrate.UpSync(
			fmt.Sprintf(
				"cassandra://%s:%d/%s?protocol=%d",
				cassandraIP,
				cassandraPort,
				keySpace,
				protocolVersion,
			),
			*migrationsPath,
		)

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

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

	cluster.Keyspace = keySpace
	return cluster.CreateSession()
}
コード例 #2
0
ファイル: gorm.go プロジェクト: upfluence/goutils
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
}
コード例 #3
0
ファイル: gorm.go プロジェクト: upfluence/goutils
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
}