Example #1
0
func LockDBAndMigrate(logger lager.Logger, sqlDriver string, sqlDataSource string) (db.Conn, error) {
	var err error
	var dbLockConn db.Conn
	var dbConn db.Conn

	for {
		dbLockConn, err = db.WrapWithError(sql.Open(sqlDriver, sqlDataSource))
		if err != nil {
			if strings.Contains(err.Error(), " dial ") {
				logger.Error("failed-to-open-db-retrying", err)
				time.Sleep(5 * time.Second)
				continue
			}
			return nil, err
		}

		break
	}

	lockName := crc32.ChecksumIEEE([]byte(sqlDriver + sqlDataSource))

	for {
		_, err = dbLockConn.Exec(`select pg_advisory_lock($1)`, lockName)
		if err != nil {
			logger.Error("failed-to-acquire-lock-retrying", err)
			time.Sleep(5 * time.Second)
			continue
		}

		logger.Info("migration-lock-acquired")

		migrations := Translogrifier(logger, Migrations)
		dbConn, err = db.WrapWithError(migration.OpenWith(sqlDriver, sqlDataSource, migrations, safeGetVersion, safeSetVersion))
		if err != nil {
			logger.Fatal("failed-to-run-migrations", err)
		}

		_, err = dbLockConn.Exec(`select pg_advisory_unlock($1)`, lockName)
		if err != nil {
			logger.Error("failed-to-release-lock", err)
		}

		dbLockConn.Close()
		break
	}

	return dbConn, nil
}
	BeforeEach(func() {
		postgresRunner.Truncate()

		dbConn = db.Wrap(postgresRunner.Open())

		listener = pq.NewListener(postgresRunner.DataSourceName(), time.Second, time.Minute, nil)
		Eventually(listener.Ping, 5*time.Second).ShouldNot(HaveOccurred())
		bus := db.NewNotificationsBus(listener, dbConn)

		pipelinesDB = new(fakes.FakePipelinesDB)

		pipelineDBFactory = db.NewPipelineDBFactory(lagertest.NewTestLogger("test"), dbConn, bus, pipelinesDB)
	})

	AfterEach(func() {
		err := dbConn.Close()
		Expect(err).NotTo(HaveOccurred())

		err = listener.Close()
		Expect(err).NotTo(HaveOccurred())
	})

	Describe("default pipeline", func() {
		It("is the first one returned from the DB", func() {
			savedPipelineOne := db.SavedPipeline{
				ID: 1,
				Pipeline: db.Pipeline{
					Name: "a-pipeline",
				},
			}
Example #3
0
	. "github.com/onsi/gomega"
)

var _ = Describe("Counting Database Queries", func() {
	var (
		underlyingConn *fakes.FakeConn
		countingConn   db.Conn
	)

	BeforeEach(func() {
		underlyingConn = new(fakes.FakeConn)
		countingConn = metric.CountQueries(underlyingConn)
	})

	AfterEach(func() {
		err := countingConn.Close()
		Expect(err).NotTo(HaveOccurred())
	})

	It("passes through calls to the underlying connection", func() {
		countingConn.Ping()

		Expect(underlyingConn.PingCallCount()).To(Equal(1))
	})

	It("returns the return values from the underlying connection", func() {
		underlyingConn.PingReturns(errors.New("disaster"))

		err := countingConn.Ping()
		Expect(err).To(MatchError("disaster"))
	})
Example #4
0
var _ = Describe("Explain", func() {
	var (
		logger         *lagertest.TestLogger
		underlyingConn *fakes.FakeConn
		explainConn    db.Conn
	)

	BeforeEach(func() {
		logger = lagertest.NewTestLogger("explain")
		underlyingConn = new(fakes.FakeConn)
		explainConn = db.Explain(logger, underlyingConn, 100*time.Millisecond)
	})

	AfterEach(func() {
		err := explainConn.Close()
		Ω(err).ShouldNot(HaveOccurred())
	})

	It("passes through calls to the underlying connection", func() {
		explainConn.Ping()

		Ω(underlyingConn.PingCallCount()).To(Equal(1))
	})

	It("returns the return values from the underlying connection", func() {
		underlyingConn.PingReturns(errors.New("disaster"))

		err := explainConn.Ping()
		Ω(err).Should(MatchError("disaster"))
	})