Ejemplo n.º 1
0
func (s *MigrateSuite) TestNewMigrator(c *C) {
	var m *migrate.Migrator
	var err error

	// Initial run
	m, err = migrate.NewMigrator(s.conn, versionTable)
	c.Assert(err, IsNil)

	// Creates version table
	schemaVersionExists := s.tableExists(c, versionTable)
	c.Assert(schemaVersionExists, Equals, true)

	// Succeeds when version table is already created
	m, err = migrate.NewMigrator(s.conn, versionTable)
	c.Assert(err, IsNil)

	initialVersion, err := m.GetCurrentVersion()
	c.Assert(err, IsNil)
	c.Assert(initialVersion, Equals, int32(0))
}
Ejemplo n.º 2
0
func Example_OnStartMigrationProgressLogging() {
	conn, err := pgx.Connect(*defaultConnectionParameters)
	if err != nil {
		fmt.Printf("Unable to establish connection: %v", err)
		return
	}

	// Clear any previous runs
	if _, err = conn.Exec("drop table if exists schema_version"); err != nil {
		fmt.Printf("Unable to drop schema_version table: %v", err)
		return
	}

	var m *migrate.Migrator
	m, err = migrate.NewMigrator(conn, "schema_version")
	if err != nil {
		fmt.Printf("Unable to create migrator: %v", err)
		return
	}

	m.OnStart = func(_ int32, name, direction, _ string) {
		fmt.Printf("Migrating %s: %s", direction, name)
	}

	m.AppendMigration("create a table", "create temporary table foo(id serial primary key)", "")

	if err = m.Migrate(); err != nil {
		fmt.Printf("Unexpected failure migrating: %v", err)
		return
	}
	// Output:
	// Migrating up: create a table
}