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

     1  package migration_test
     2  
     3  import (
     4  	"database/sql"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Fix Build Private Plan", func() {
    11  	const preMigrationVersion = 1551384519
    12  	const postMigrationVersion = 1551384520
    13  
    14  	var (
    15  		db *sql.DB
    16  	)
    17  
    18  	Context("Up", func() {
    19  		It("ignores NULL plans", func() {
    20  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    21  			SetupTeam(db, "some-team", "{}")
    22  			SetupBuild(db, "some-build")
    23  			db.Close()
    24  
    25  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    26  			ExpectBuildWithNullPlan(db, "some-build")
    27  			db.Close()
    28  		})
    29  
    30  		It("removes the 'plan' key", func() {
    31  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    32  			SetupTeam(db, "some-team", "{}")
    33  			SetupBuildWithPlan(db, "some-build", `{"plan":{"some":"plan"}}`)
    34  			db.Close()
    35  
    36  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    37  			ExpectBuildWithPlan(db, "some-build", `{"some":"plan"}`)
    38  			db.Close()
    39  		})
    40  
    41  		It("updates multiple plans", func() {
    42  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    43  			SetupTeam(db, "some-team", "{}")
    44  			SetupBuildWithPlan(db, "some-build", `{"plan":{"some":"plan"}}`)
    45  			SetupBuildWithPlan(db, "some-other-build", `{"plan":{"some":"other-plan"}}`)
    46  			db.Close()
    47  
    48  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    49  			ExpectBuildWithPlan(db, "some-build", `{"some":"plan"}`)
    50  			ExpectBuildWithPlan(db, "some-other-build", `{"some":"other-plan"}`)
    51  			db.Close()
    52  		})
    53  	})
    54  
    55  	Context("Down", func() {
    56  		It("ignores NULL plans", func() {
    57  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    58  			SetupTeam(db, "some-team", "{}")
    59  			SetupBuild(db, "some-build")
    60  			db.Close()
    61  
    62  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    63  			ExpectBuildWithNullPlan(db, "some-build")
    64  			db.Close()
    65  		})
    66  
    67  		It("nests the plan under a 'plan' key", func() {
    68  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    69  			SetupTeam(db, "some-team", "{}")
    70  			SetupBuildWithPlan(db, "some-build", `{"some":"plan"}`)
    71  			db.Close()
    72  
    73  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    74  			ExpectBuildWithPlan(db, "some-build", `{"plan":{"some":"plan"}}`)
    75  			db.Close()
    76  		})
    77  
    78  		It("updates multiple plans", func() {
    79  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    80  			SetupTeam(db, "some-team", "{}")
    81  			SetupBuildWithPlan(db, "some-build", `{"some":"plan"}`)
    82  			SetupBuildWithPlan(db, "some-other-build", `{"some":"other-plan"}`)
    83  			db.Close()
    84  
    85  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    86  			ExpectBuildWithPlan(db, "some-build", `{"plan":{"some":"plan"}}`)
    87  			ExpectBuildWithPlan(db, "some-other-build", `{"plan":{"some":"other-plan"}}`)
    88  			db.Close()
    89  		})
    90  	})
    91  })
    92  
    93  func SetupBuild(dbConn *sql.DB, name string) {
    94  	_, err := dbConn.Exec("INSERT INTO builds(name, status, team_id) VALUES($1, 'started', 1)", name)
    95  	Expect(err).NotTo(HaveOccurred())
    96  }
    97  
    98  func SetupBuildWithPlan(dbConn *sql.DB, name, plan string) {
    99  	_, err := dbConn.Exec("INSERT INTO builds(name, status, team_id, private_plan) VALUES($1, 'started', 1, $2)", name, plan)
   100  	Expect(err).NotTo(HaveOccurred())
   101  }
   102  
   103  func ExpectBuildWithNullPlan(dbConn *sql.DB, name string) {
   104  
   105  	plan := fetchBuildPlan(dbConn, name)
   106  
   107  	Expect(plan.Valid).To(BeFalse())
   108  }
   109  
   110  func ExpectBuildWithPlan(dbConn *sql.DB, name, expectedPlan string) {
   111  
   112  	plan := fetchBuildPlan(dbConn, name)
   113  
   114  	Expect(plan.Valid).To(BeTrue())
   115  	Expect(plan.String).To(Equal(expectedPlan))
   116  }
   117  
   118  func fetchBuildPlan(dbConn *sql.DB, name string) sql.NullString {
   119  	var plan sql.NullString
   120  	err := dbConn.QueryRow("SELECT private_plan FROM builds WHERE name = $1", name).Scan(&plan)
   121  	Expect(err).NotTo(HaveOccurred())
   122  	return plan
   123  }