Example #1
0
func TestRunAllMigrationsUp(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		Add(sampleMigrations[0])
		Add(sampleMigrations[1])
		RunAllMigrationsUp(db)

		_, err := db.Query("Select other from tests_table;")
		assert.Nil(t, err)

		rows, _ := db.Query("Select * from " + MigrationsTable + ";")
		count := 0
		defer rows.Close()
		for rows.Next() {
			count++
		}

		assert.Equal(t, count, 2)
	}
}
Example #2
0
func TestDownBadMigration(t *testing.T) {
	for dname, man := range mans {
		utils.ClearTestTables(dname)
		utils.ClearTestMigrations()
		utils.SetupTestingFolders(dname)

		firstID := transporter.MigrationIdentifier()
		lastID := transporter.MigrationIdentifier()

		utils.GenerateMigrationFiles([]utils.TestMigration{
			utils.TestMigration{
				Identifier:  firstID,
				UpCommand:   "CREATE TABLE down_table (a varchar(255))",
				DownCommand: "DROP TABLE down_table",
			},
			utils.TestMigration{
				Identifier:  lastID,
				UpCommand:   "ALTER TABLE down_table ADD COLUMN o varchar(255)",
				DownCommand: "WUHdnns oius (Bad mig!);",
			},
		})

		context := cli.NewContext(nil, &flag.FlagSet{}, nil)
		Up(context)
		Down(context)

		con, _ := utils.BuildTestingConnection(dname)
		transporter.SetManager(man)

		version := transporter.DatabaseVersion(con)
		assert.Equal(t, version, strconv.FormatInt(lastID, 10))
		defer con.Close()
	}
}
Example #3
0
func TestRunMigrationUp(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		m := sampleMigrations[0]

		RunMigrationUp(db, &m)
		_, err := db.Exec("Select * from tests_table;")
		assert.Nil(t, err)
		rows, _ := db.Query("Select * from " + MigrationsTable + ";")

		count := 0
		defer rows.Close()
		for rows.Next() {
			var identifier string
			rows.Scan(&identifier)
			id, _ := strconv.Atoi(identifier)
			assert.Equal(t, int64(id), m.Identifier)
			count++
		}

		assert.Equal(t, 1, count)
	}
}
Example #4
0
func TestDropColumnMigration(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		m := Migration{
			Identifier: MigrationIdentifier(),
			Up: func(tx *Tx) {
				tx.CreateTable("tests_table", managers.Table{
					"a":            "varchar(12)",
					"other_column": "integer",
					"float_column": "float",
				})

				tx.DropColumn("tests_table", "float_column")
			},
			Down: func(tx *Tx) {
				tx.DropTable("tests_table")
			},
		}

		RunMigrationUp(db, &m)
		_, err := db.Exec("SELECT float_column FROM tests_table")
		assert.NotNil(t, err)
	}
}
Example #5
0
func TestRunOneMigrationDown(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		migrations = []Migration{}

		m := sampleMigrations[0]
		Add(m)
		RunMigrationUp(db, &m)
		RunOneMigrationDown(db)

		_, err := db.Exec("Select * from tests_table;")
		assert.NotNil(t, err)

		rows, _ := db.Query("Select * from " + MigrationsTable + ";")
		count := 0
		defer rows.Close()
		for rows.Next() {
			count++
		}

		assert.Equal(t, 0, count)
	}
}
Example #6
0
func TestChangeColumnNameMigration(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		m := Migration{
			Identifier: MigrationIdentifier(),
			Up: func(tx *Tx) {
				tx.CreateTable("tests_table", managers.Table{
					"a":            "varchar(12)",
					"other_column": "integer",
					"float_column": "float",
				})

				tx.RenameColumn("tests_table", "float_column", "point_column")
			},
			Down: func(tx *Tx) {
				tx.DropTable("tests_table")
			},
		}

		RunMigrationUp(db, &m)
		_, err := db.Exec("SELECT point_column FROM tests_table")
		if name == "mysql" {
			assert.NotNil(t, err) //Note: Mysql requires the column type.
		} else {
			assert.Nil(t, err)
		}
	}
}
Example #7
0
func TestDropMigration(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		m := Migration{
			Identifier: MigrationIdentifier(),
			Up: func(tx *Tx) {
				tx.Exec("CREATE TABLE tests_table (a varchar(12))")
			},
			Down: func(tx *Tx) {
				tx.DropTable("tests_table")
			},
		}

		RunMigrationUp(db, &m)
		RunMigrationDown(db, &m)
		_, err := db.Exec("SELECT * FROM tests_table")
		assert.NotNil(t, err)

	}
}
Example #8
0
func TestUpBadMigration(t *testing.T) {
	for dname := range mans {
		utils.ClearTestTables(dname)
		utils.ClearTestMigrations()
		utils.SetupTestingFolders(dname)

		utils.GenerateMigrationFiles([]utils.TestMigration{
			utils.TestMigration{
				Identifier:  transporter.MigrationIdentifier(),
				UpCommand:   "Create table other_table (a varchar(255) );",
				DownCommand: "Drop Table other_table;",
			},
			utils.TestMigration{
				Identifier:  transporter.MigrationIdentifier(),
				UpCommand:   "Alter table shshshshs other_table add column o varchar(12);",
				DownCommand: "Drop column Wihii;",
			},
			utils.TestMigration{
				Identifier:  transporter.MigrationIdentifier(),
				UpCommand:   "Alter table other_table drop column a;",
				DownCommand: "",
			},
		})

		context := cli.NewContext(nil, &flag.FlagSet{}, nil)
		Up(context)

		con, _ := utils.BuildTestingConnection(dname)
		_, err := con.Query("Select a from other_table;")
		assert.Nil(t, err)
	}
}
Example #9
0
func TestMigrationsTableDoesntExists(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)

		db, err := utils.BuildTestingConnection(name)
		defer db.Close()
		assert.Nil(t, err)
		assert.False(t, MigrationsTableExists(db))
	}
}
Example #10
0
func TestRunAllMigrationsOnlyPending(t *testing.T) {
	for name, man := range mans {
		manager = man
		dropTables(name)
		createMigrationsTable(name)

		db, _ := utils.BuildTestingConnection(name)
		defer db.Close()

		migrations = []Migration{}

		Add(sampleMigrations[0])
		Add(sampleMigrations[1])

		db.Query("INSERT INTO " + MigrationsTable + " VALUES (" + sampleMigrations[1].GetID() + ");")
		RunAllMigrationsUp(db)

		_, err := db.Query("Select other from tests_table;")
		assert.NotNil(t, err)
	}

}
Example #11
0
func TestDown(t *testing.T) {
	for dname := range mans {
		utils.ClearTestTables(dname)
		utils.ClearTestMigrations()
		utils.SetupTestingFolders(dname)

		utils.GenerateMigrationFiles([]utils.TestMigration{
			utils.TestMigration{
				Identifier:  transporter.MigrationIdentifier(),
				UpCommand:   "CREATE TABLE down_table (a varchar(255))",
				DownCommand: "DROP TABLE down_table",
			},
			utils.TestMigration{
				Identifier:  transporter.MigrationIdentifier(),
				UpCommand:   "ALTER TABLE down_table ADD COLUMN o varchar(255)",
				DownCommand: "ALTER TABLE down_table DROP COLUMN o",
			},
		})

		context := cli.NewContext(nil, &flag.FlagSet{}, nil)
		Up(context)
		Down(context)

		con, _ := utils.BuildTestingConnection(dname)
		defer con.Close()

		_, err := con.Query("SELECT a FROM down_table")
		assert.Nil(t, err)

		Down(context)

		_, err = con.Query("SELECT a FROM down_table")
		log.Println(err)
		assert.NotNil(t, err)
	}
}
Example #12
0
func dropTables(driver string) {
	db, _ := utils.BuildTestingConnection(driver)
	db.Exec("DROP TABLE IF EXISTS  " + MigrationsTable + ";")
	db.Exec("DROP TABLE IF EXISTS tests_table ;")
}
Example #13
0
func createMigrationsTable(driver string) {
	db, _ := utils.BuildTestingConnection(driver)
	query := manager.CreateMigrationsTableQuery(MigrationsTable)
	db.Exec(query)
}