func TruncateTables(database *db.DB) { env := application.NewEnvironment() dbMigrator := v1models.DatabaseMigrator{} dbMigrator.Migrate(database.RawConnection(), env.ModelMigrationsPath) v1models.Setup(database) v2models.Setup(database) connection := database.Connection().(*db.Connection) err := connection.TruncateTables() if err != nil { panic(err) } }
func Setup(database *db.DB) { database.TableMap().AddTableWithName(Sender{}, "senders").SetKeys(false, "ID").SetUniqueTogether("name", "client_id") database.TableMap().AddTableWithName(CampaignType{}, "campaign_types").SetKeys(false, "ID").SetUniqueTogether("name", "sender_id") database.TableMap().AddTableWithName(Template{}, "v2_templates").SetKeys(false, "ID").SetUniqueTogether("name", "client_id") database.TableMap().AddTableWithName(Campaign{}, "campaigns").SetKeys(false, "ID") database.TableMap().AddTableWithName(Message{}, "messages").SetKeys(false, "ID") }
func Setup(database *db.DB) { database.TableMap().AddTableWithName(Client{}, "clients").SetKeys(true, "Primary").ColMap("ID").SetUnique(true) database.TableMap().AddTableWithName(Kind{}, "kinds").SetKeys(true, "Primary").SetUniqueTogether("id", "client_id") database.TableMap().AddTableWithName(Receipt{}, "receipts").SetKeys(true, "Primary").SetUniqueTogether("user_guid", "client_id", "kind_id") database.TableMap().AddTableWithName(Unsubscribe{}, "unsubscribes").SetKeys(true, "Primary").SetUniqueTogether("user_id", "client_id", "kind_id") database.TableMap().AddTableWithName(GlobalUnsubscribe{}, "global_unsubscribes").SetKeys(true, "Primary").ColMap("UserID").SetUnique(true) database.TableMap().AddTableWithName(Template{}, "templates").SetKeys(true, "Primary").ColMap("Name").SetUnique(true) database.TableMap().AddTableWithName(Message{}, "messages").SetKeys(false, "ID") }
package models_test import ( "github.com/cloudfoundry-incubator/notifications/application" "github.com/cloudfoundry-incubator/notifications/db" "github.com/cloudfoundry-incubator/notifications/testing/helpers" "github.com/cloudfoundry-incubator/notifications/v1/models" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("DatabaseMigrator", func() { var ( database *db.DB connection *db.Connection dbMigrator models.DatabaseMigrator defaultTemplatePath string ) BeforeEach(func() { env := application.NewEnvironment() defaultTemplatePath = env.RootPath + "/templates/default.json" database = db.NewDatabase(sqlDB, db.Config{ DefaultTemplatePath: defaultTemplatePath, }) helpers.TruncateTables(database) connection = database.Connection().(*db.Connection) dbMigrator = models.DatabaseMigrator{} }) Describe("migrating the database", func() {
package db_test import ( "github.com/cloudfoundry-incubator/notifications/db" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Database", func() { var database *db.DB BeforeEach(func() { database = db.NewDatabase(sqlDB, db.Config{}) }) Describe("Connection", func() { It("returns a Connection", func() { connection := database.Connection() Expect(connection).To(BeAssignableToTypeOf(&db.Connection{})) }) }) })