github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/migration/open_helper_test.go (about)

     1  package migration_test
     2  
     3  import (
     4  	"database/sql"
     5  
     6  	"code.cloudfoundry.org/lager"
     7  
     8  	"github.com/pf-qiu/concourse/v6/atc/db/lock"
     9  	"github.com/pf-qiu/concourse/v6/atc/db/migration"
    10  	"github.com/pf-qiu/concourse/v6/atc/db/migration/migrationfakes"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("OpenHelper", func() {
    16  	var (
    17  		err         error
    18  		db          *sql.DB
    19  		lockDB      *sql.DB
    20  		lockFactory lock.LockFactory
    21  		bindata     *migrationfakes.FakeBindata
    22  		openHelper  *migration.OpenHelper
    23  		fakeLogFunc = func(logger lager.Logger, id lock.LockID) {}
    24  	)
    25  
    26  	JustBeforeEach(func() {
    27  		db, err = sql.Open("postgres", postgresRunner.DataSourceName())
    28  		Expect(err).NotTo(HaveOccurred())
    29  
    30  		lockDB, err = sql.Open("postgres", postgresRunner.DataSourceName())
    31  		Expect(err).NotTo(HaveOccurred())
    32  
    33  		lockFactory = lock.NewLockFactory(lockDB, fakeLogFunc, fakeLogFunc)
    34  		openHelper = migration.NewOpenHelper("postgres", postgresRunner.DataSourceName(), lockFactory, nil, nil)
    35  
    36  		bindata = new(migrationfakes.FakeBindata)
    37  		bindata.AssetStub = asset
    38  	})
    39  
    40  	AfterEach(func() {
    41  		_ = db.Close()
    42  		_ = lockDB.Close()
    43  	})
    44  
    45  	Context("legacy migration_version table exists", func() {
    46  		It("Fails if trying to upgrade from a migration_version < 189", func() {
    47  			SetupMigrationVersionTableToExistAtVersion(db, 188)
    48  
    49  			err = openHelper.MigrateToVersion(5000)
    50  
    51  			Expect(err.Error()).To(Equal("Must upgrade from db version 189 (concourse 3.6.0), current db version: 188"))
    52  
    53  			_, err = db.Exec("SELECT version FROM migration_version")
    54  			Expect(err).NotTo(HaveOccurred())
    55  		})
    56  
    57  		It("Fails if trying to upgrade from a migration_version > 189", func() {
    58  			SetupMigrationVersionTableToExistAtVersion(db, 190)
    59  
    60  			err = openHelper.MigrateToVersion(5000)
    61  
    62  			Expect(err.Error()).To(Equal("Must upgrade from db version 189 (concourse 3.6.0), current db version: 190"))
    63  
    64  			_, err = db.Exec("SELECT version FROM migration_version")
    65  			Expect(err).NotTo(HaveOccurred())
    66  		})
    67  
    68  		It("Forces schema migration version to a known first version if migration_version is 189", func() {
    69  			var initialSchemaVersion = 1510262030
    70  			SetupMigrationVersionTableToExistAtVersion(db, 189)
    71  
    72  			SetupSchemaFromFile(db, "migrations/1510262030_initial_schema.up.sql")
    73  
    74  			err = openHelper.MigrateToVersion(initialSchemaVersion)
    75  			Expect(err).NotTo(HaveOccurred())
    76  
    77  			ExpectDatabaseVersionToEqual(db, initialSchemaVersion, "schema_migrations")
    78  
    79  			ExpectMigrationVersionTableNotToExist(db)
    80  
    81  			ExpectToBeAbleToInsertData(db)
    82  		})
    83  
    84  		It("Runs migrator if migration_version table does not exist", func() {
    85  
    86  			bindata.AssetNamesReturns([]string{
    87  				"1510262030_initial_schema.up.sql",
    88  			})
    89  			err = openHelper.MigrateToVersion(initialSchemaVersion)
    90  			Expect(err).NotTo(HaveOccurred())
    91  
    92  			ExpectDatabaseVersionToEqual(db, initialSchemaVersion, "migrations_history")
    93  
    94  			ExpectMigrationVersionTableNotToExist(db)
    95  
    96  			ExpectToBeAbleToInsertData(db)
    97  		})
    98  
    99  	})
   100  })
   101  
   102  func SetupMigrationVersionTableToExistAtVersion(db *sql.DB, version int) {
   103  	_, err := db.Exec(`CREATE TABLE migration_version(version int)`)
   104  	Expect(err).NotTo(HaveOccurred())
   105  
   106  	_, err = db.Exec(`INSERT INTO migration_version(version) VALUES($1)`, version)
   107  	Expect(err).NotTo(HaveOccurred())
   108  }
   109  
   110  func ExpectMigrationVersionTableNotToExist(dbConn *sql.DB) {
   111  	var exists string
   112  	err := dbConn.QueryRow("SELECT EXISTS(SELECT 1 FROM information_schema.tables where table_name = 'migration_version')").Scan(&exists)
   113  	Expect(err).NotTo(HaveOccurred())
   114  	Expect(exists).To(Equal("false"))
   115  }
   116  
   117  func ExpectDatabaseVersionToEqual(db *sql.DB, version int, table string) {
   118  	var dbVersion int
   119  	query := "SELECT version from " + table + " LIMIT 1"
   120  	err := db.QueryRow(query).Scan(&dbVersion)
   121  	Expect(err).NotTo(HaveOccurred())
   122  	Expect(dbVersion).To(Equal(version))
   123  }