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 }
func Status(cmd *cobra.Command, args []string) { config, err := LoadConfig() if err != nil { fmt.Fprintf(os.Stderr, "Error loading config:\n %v\n", err) os.Exit(1) } err = config.Validate() if err != nil { fmt.Fprintf(os.Stderr, "Invalid config:\n %v\n", err) os.Exit(1) } conn, err := config.Connect() if err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to PostgreSQL:\n %v\n", err) os.Exit(1) } defer conn.Close() migrator, err := migrate.NewMigrator(conn, config.VersionTable) if err != nil { fmt.Fprintf(os.Stderr, "Error initializing migrator:\n %v\n", err) os.Exit(1) } migrator.Data = config.Data migrationsPath := cliOptions.migrationsPath err = migrator.LoadMigrations(migrationsPath) if err != nil { fmt.Fprintf(os.Stderr, "Error loading migrations:\n %v\n", err) os.Exit(1) } if len(migrator.Migrations) == 0 { fmt.Fprintln(os.Stderr, "No migrations found") os.Exit(1) } migrationVersion, err := migrator.GetCurrentVersion() if err != nil { fmt.Fprintf(os.Stderr, "Error retrieving migration version:\n %v\n", err) os.Exit(1) } var status string behindCount := len(migrator.Migrations) - int(migrationVersion) if behindCount == 0 { status = "up to date" } else { status = "migration(s) pending" } fmt.Println("status: ", status) fmt.Printf("version: %d of %d\n", migrationVersion, len(migrator.Migrations)) fmt.Println("host: ", config.ConnConfig.Host) fmt.Println("database:", config.ConnConfig.Database) }
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)) }
func Migrate(cmd *cobra.Command, args []string) { config, err := LoadConfig() if err != nil { fmt.Fprintf(os.Stderr, "Error loading config:\n %v\n", err) os.Exit(1) } err = config.Validate() if err != nil { fmt.Fprintf(os.Stderr, "Invalid config:\n %v\n", err) os.Exit(1) } conn, err := config.Connect() if err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to PostgreSQL:\n %v\n", err) os.Exit(1) } defer conn.Close() migrator, err := migrate.NewMigrator(conn, config.VersionTable) if err != nil { fmt.Fprintf(os.Stderr, "Error initializing migrator:\n %v\n", err) os.Exit(1) } migrator.Data = config.Data migrationsPath := cliOptions.migrationsPath err = migrator.LoadMigrations(migrationsPath) if err != nil { fmt.Fprintf(os.Stderr, "Error loading migrations:\n %v\n", err) os.Exit(1) } if len(migrator.Migrations) == 0 { fmt.Fprintln(os.Stderr, "No migrations found") os.Exit(1) } migrator.OnStart = func(sequence int32, name, direction, sql string) { fmt.Printf("%s executing %s %s\n%s\n\n", time.Now().Format("2006-01-02 15:04:05"), name, direction, sql) } var currentVersion int32 currentVersion, err = migrator.GetCurrentVersion() if err != nil { fmt.Fprintf(os.Stderr, "Unable to get current version:\n %v\n", err) os.Exit(1) } destination := cliOptions.destinationVersion mustParseDestination := func(d string) int32 { var n int64 n, err = strconv.ParseInt(d, 10, 32) if err != nil { fmt.Fprintf(os.Stderr, "Bad destination:\n %v\n", err) os.Exit(1) } return int32(n) } if destination == "last" { err = migrator.Migrate() } else if len(destination) >= 3 && destination[0:2] == "-+" { err = migrator.MigrateTo(currentVersion - mustParseDestination(destination[2:])) if err == nil { err = migrator.MigrateTo(currentVersion) } } else if len(destination) >= 2 && destination[0] == '-' { err = migrator.MigrateTo(currentVersion - mustParseDestination(destination[1:])) } else if len(destination) >= 2 && destination[0] == '+' { err = migrator.MigrateTo(currentVersion + mustParseDestination(destination[1:])) } else { err = migrator.MigrateTo(mustParseDestination(destination)) } if err != nil { fmt.Fprintln(os.Stderr, err) if err, ok := err.(migrate.MigrationSyntaxError); ok { ele, err := ExtractErrorLine(err.Sql, int(err.Position)) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } prefix := fmt.Sprintf("LINE %d: ", ele.LineNum) fmt.Printf("%s%s\n", prefix, ele.Text) padding := strings.Repeat(" ", len(prefix)+ele.ColumnNum-1) fmt.Printf("%s^\n", padding) } os.Exit(1) } }
func (s *MigrateSuite) createEmptyMigrator(c *C) *migrate.Migrator { var err error m, err := migrate.NewMigrator(s.conn, versionTable) c.Assert(err, IsNil) return m }