// Applies all inactive migrations.
18331
func (m *Migrator) Migrate() error {
	for _, migration := range m.Migrations(Inactive) {
		if err := m.ApplyMigration(migration, upMigration); err != nil {
			return err
		}
	}
	return nil
}
224740
func (wr *Wrangler) replicaMigrateServedFrom(ctx context.Context, ki *topo.KeyspaceInfo, sourceShard *topo.ShardInfo, destinationShard *topo.ShardInfo, servedType topodatapb.TabletType, cells []string, reverse bool, tables []string, ev *events.MigrateServedFrom) error {
	// Save the destination keyspace (its ServedFrom has been changed)
	event.DispatchUpdate(ev, "updating keyspace")
	if err := wr.ts.UpdateKeyspace(ctx, ki); err != nil {
		return err
	}

	// Save the source shard (its blacklisted tables field has changed)
	event.DispatchUpdate(ev, "updating source shard")
	if _, err := wr.ts.UpdateShardFields(ctx, sourceShard.Keyspace(), sourceShard.ShardName(), func(si *topo.ShardInfo) error {
		return si.UpdateSourceBlacklistedTables(ctx, servedType, cells, reverse, tables)
	}); err != nil {
		return err
	}

	// Now refresh the source servers so they reload their
	// blacklisted table list
	event.DispatchUpdate(ev, "refreshing sources tablets state so they update their blacklisted tables")
	return wr.RefreshTabletsByShard(ctx, sourceShard, []topodatapb.TabletType{servedType}, cells)
}
1038005
func migrations(path string) ([]string, error) {
	matches, err := filepath.Glob(path + "/*.sql")
	sort.Strings(matches)
	return matches, err
}
583823
func DeleteInactivateUsers() (err error) {
	users := make([]*User, 0, 10)
	if err = x.Where("is_active = ?", false).Find(&users); err != nil {
		return fmt.Errorf("get all inactive users: %v", err)
	}
	// FIXME: should only update authorized_keys file once after all deletions.
	for _, u := range users {
		if err = DeleteUser(u); err != nil {
			// Ignore users that were set inactive by admin.
			if IsErrUserOwnRepos(err) || IsErrUserHasOrgs(err) {
				continue
			}
			return err
		}
	}

	_, err = x.Where("is_activated = ?", false).Delete(new(EmailAddress))
	return err
}
505933
func (t *TypesMigrator) Migrate() {
	if t.ActivityTypeMigrator == nil {
		t.ActivityTypeMigrator = new(ActivityTypeMigrator)
	}
	if t.DomainMigrator == nil {
		t.DomainMigrator = new(DomainMigrator)
	}
	if t.WorkflowTypeMigrator == nil {
		t.WorkflowTypeMigrator = new(WorkflowTypeMigrator)
	}
	if t.StreamMigrator == nil {
		t.StreamMigrator = new(StreamMigrator)
	}

	t.DomainMigrator.Migrate()
	ParallelMigrate(
		t.WorkflowTypeMigrator.Migrate,
		t.ActivityTypeMigrator.Migrate,
		t.StreamMigrator.Migrate,
	)
}
1024449
func MigrateAvailable(db *mgo.Database, fromCategory string, toCategory string) (err error) {
	Available := db.C(availableCollection)

	change, err := Available.UpdateAll(
		bson.M{
			"app_name": fromCategory,
		},
		bson.M{
			"$set": bson.M{
				"app_name": toCategory,
			},
		},
	)
	handle_errors(err)
	fmt.Println("Updated", change.Updated, "documents in `", availableCollection, "` collection")
	return
}
364271
func (m *Migrate) RollbackMigration(mig *Migration) error {
	if mig.Rollback == nil {
		return ErrRollbackImpossible
	}

	if err := mig.Rollback(m.db); err != nil {
		return err
	}

	sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName)
	if _, err := m.db.Exec(sql, mig.ID); err != nil {
		return err
	}
	return nil
}
1121830
func (s *DB) AutoMigrate(values ...interface{}) *DB {
	db := s.Unscoped()
	for _, value := range values {
		db = db.NewScope(value).autoMigrate().db
	}
	return db
}
1279839
func MigrateRefresh(currpath, driver, connStr, dir string) {
	migrate("refresh", currpath, driver, connStr, dir)
}
882719
func (c *configuration) apply(t transition) {
	logf("Applying %v", t)
	w1 := int(c.stackValue(1))
	w2 := int(c.stackValue(0))

	if t.Move == Left {
		c.AddArc(w2, w1, t.DependencyType)
		c.removeSecondTopStack()
	} else if t.Move == Right {
		c.AddArc(w1, w2, t.DependencyType)
		c.removeTopStack()
	} else {
		c.shift()
	}
}
439183
func (wr *Wrangler) MigrateServedTypes(ctx context.Context, keyspace, shard string, cells []string, servedType topodatapb.TabletType, reverse, skipReFreshState bool, filteredReplicationWaitTime time.Duration, reverseReplication bool) (err error) {
	// check input parameters
	if servedType == topodatapb.TabletType_MASTER {
		// we cannot migrate a master back, since when master migration
		// is done, the source shards are dead
		if reverse {
			return fmt.Errorf("cannot migrate master back to %v/%v", keyspace, shard)
		}
		// we cannot skip refresh state for a master
		if skipReFreshState {
			return fmt.Errorf("cannot skip refresh state for master migration on %v/%v", keyspace, shard)
		}
		if cells != nil {
			return fmt.Errorf("cannot specify cells for master migration on %v/%v", keyspace, shard)
		}
	}

	// lock the keyspace
	ctx, unlock, lockErr := wr.ts.LockKeyspace(ctx, keyspace, fmt.Sprintf("MigrateServedTypes(%v)", servedType))
	if lockErr != nil {
		return lockErr
	}
	defer unlock(&err)

	// find overlapping shards in this keyspace
	wr.Logger().Infof("Finding the overlapping shards in keyspace %v", keyspace)
	osList, err := topotools.FindOverlappingShards(ctx, wr.ts, keyspace)
	if err != nil {
		return fmt.Errorf("FindOverlappingShards failed: %v", err)
	}

	// find our shard in there
	os := topotools.OverlappingShardsForShard(osList, shard)
	if os == nil {
		return fmt.Errorf("shard %v is not involved in any overlapping shards", shard)
	}

	sourceShards, destinationShards, err := wr.findSourceDest(ctx, os)
	if err != nil {
		return err
	}

	// execute the migration
	if servedType == topodatapb.TabletType_MASTER {
		if err = wr.masterMigrateServedType(ctx, keyspace, sourceShards, destinationShards, filteredReplicationWaitTime, reverseReplication); err != nil {
			return err
		}
	} else {
		if err = wr.replicaMigrateServedType(ctx, keyspace, sourceShards, destinationShards, cells, servedType, reverse); err != nil {
			return err
		}
	}

	// Master migrate performs its own refresh.
	// Otherwise, honor skipRefreshState if requested.
	if servedType == topodatapb.TabletType_MASTER || skipReFreshState {
		return nil
	}

	// refresh
	// TODO(b/26388813): Integrate vtctl WaitForDrain here instead of just sleeping.
	// Anything that's not a replica will use the RDONLY sleep time.
	waitForDrainSleep := *waitForDrainSleepRdonly
	if servedType == topodatapb.TabletType_REPLICA {
		waitForDrainSleep = *waitForDrainSleepReplica
	}
	wr.Logger().Infof("WaitForDrain: Sleeping for %.0f seconds before shutting down query service on old tablets...", waitForDrainSleep.Seconds())
	time.Sleep(waitForDrainSleep)
	wr.Logger().Infof("WaitForDrain: Sleeping finished. Shutting down queryservice on old tablets now.")

	rec := concurrency.AllErrorRecorder{}
	refreshShards := sourceShards
	if reverse {
		// For a backwards migration, we should refresh (disable) destination shards instead.
		refreshShards = destinationShards
	}
	for _, si := range refreshShards {
		rec.RecordError(wr.RefreshTabletsByShard(ctx, si, []topodatapb.TabletType{servedType}, cells))
	}
	return rec.Error()
}
1181850
func Apply(db Database, change Change) error {
	db, closer := db.Copy()
	defer closer()

	buildTxn := func(int) ([]txn.Op, error) {
		ops, err := change.Prepare(db)
		if errors.Cause(err) == ErrChangeComplete {
			return nil, jujutxn.ErrNoOperations
		}
		if err != nil {
			return nil, errors.Trace(err)
		}
		return ops, nil
	}

	runner, closer := db.TransactionRunner()
	defer closer()
	if err := runner.Run(buildTxn); err != nil {
		return errors.Trace(err)
	}
	return nil
}
1223579
func DeviceMigrations(prefix string) map[string]storage.MigrateFunction {
	funcs := make(map[string]storage.MigrateFunction)
	for v, f := range deviceMigrations {
		funcs[v] = f(prefix)
	}
	return funcs
}
663924
func (p *Paths) Migrate(newPaths Paths) {
	if newPaths.DataDir != "" {
		p.DataDir = newPaths.DataDir
	}
	if newPaths.LogDir != "" {
		p.LogDir = newPaths.LogDir
	}
	if newPaths.MetricsSpoolDir != "" {
		p.MetricsSpoolDir = newPaths.MetricsSpoolDir
	}
	if newPaths.ConfDir != "" {
		p.ConfDir = newPaths.ConfDir
	}
}
104579
func GetMigrations(configuration MigrationConfiguration) Migrations {
	m := Migrations{}

	// Version 0
	m = append(m, steps{ExecuteSQLFile("000-bootstrap.sql")})

	// Version 1
	m = append(m, steps{ExecuteSQLFile("001-identities-users.sql")})

	// Version 2
	m = append(m, steps{ExecuteSQLFile("002-oauth-states.sql")})

	// Version 3
	m = append(m, steps{ExecuteSQLFile("003-space-resources.sql")})

	// Version 4
	m = append(m, steps{ExecuteSQLFile("004-unique-resource-space.sql")})

	// Version 5
	m = append(m, steps{ExecuteSQLFile("005-authorization.sql")})

	// Version 6
	m = append(m, steps{ExecuteSQLFile("006-external-provider.sql")})

	// Version 7
	m = append(m, steps{ExecuteSQLFile("007-external-provider-id-index.sql")})

	// Version 8
	m = append(m, steps{ExecuteSQLFile("008-rename-token-table.sql")})

	// Version 9
	m = append(m, steps{ExecuteSQLFile("009-external-token-hard-delete.sql")})

	// Version 10
	defaultCluster := configuration.GetOpenShiftClientApiUrl()
	m = append(m, steps{ExecuteSQLFile("010-add-cluster-to-user.sql", defaultCluster)})

	// Version 11
	m = append(m, steps{ExecuteSQLFile("011-add-username-to-external-token.sql")})

	// Version 12
	m = append(m, steps{ExecuteSQLFile("012-hide-email.sql")})

	// Version 13
	m = append(m, steps{ExecuteSQLFile("013-add-email-verified.sql")})

	// Version 14
	m = append(m, steps{ExecuteSQLFile("014-add-user-feature-level.sql")})

	// Version 15
	m = append(m, steps{ExecuteSQLFile("015-clear-resources-create-resource-types.sql")})

	// Version 16
	m = append(m, steps{ExecuteSQLFile("016-add-state-to-auth-state-reference.sql")})

	// Version 17
	m = append(m, steps{ExecuteSQLFile("017-feature-level-not-null.sql")})

	// Version 18
	m = append(m, steps{ExecuteSQLFile("018-convert-user-feature-level.sql")})

	// Version 19
	m = append(m, steps{ExecuteSQLFile("019-authorization-part-2.sql")})

	// Version 20
	m = append(m, steps{ExecuteSQLFile("020-add-response-mode-to-auth-state-reference.sql")})

	// Version 21
	m = append(m, steps{ExecuteSQLFile("021-organizations-list-create.sql")})

	// Version 22
	m = append(m, steps{ExecuteSQLFile("022-add-deprovisioned-to-user.sql")})

	// Version 23
	m = append(m, steps{ExecuteSQLFile("023-resource-type-index.sql")})

	// Version 24
	m = append(m, steps{ExecuteSQLFile("024-role-mapping-and-team-and-group-identities.sql")})

	// Version 25
	m = append(m, steps{ExecuteSQLFile("025-fix-feature-level.sql")})

	// Version 26
	m = append(m, steps{ExecuteSQLFile("026-identities-users-indexes.sql")})

	// Version 27
	m = append(m, steps{ExecuteSQLFile("027-invitations.sql")})

	// Version 28
	m = append(m, steps{ExecuteSQLFile("028-make-organization-names-unique.sql")})

	// Version 29
	m = append(m, steps{ExecuteSQLFile("029-add-space-resourcetype.sql")})

	// Version 30
	m = append(m, steps{ExecuteSQLFile("030-add-team-admin-role.sql")})

	// Version 31
	m = append(m, steps{ExecuteSQLFile("031-clean-up-roles-scopes.sql")})

	// Version 32
	m = append(m, steps{ExecuteSQLFile("032-invitation-code.sql")})

	// Version 33
	m = append(m, steps{ExecuteSQLFile("033-drop-space-resources.sql")})

	// Version 34
	m = append(m, steps{ExecuteSQLFile("034-rename-token-table.sql")})

	// Version 35
	m = append(m, steps{ExecuteSQLFile("035-unique_constraint_default_role_mapping.sql")})

	// Version 36
	m = append(m, steps{ExecuteSQLFile("036-token-privileges.sql")})

	// Version 37
	m = append(m, steps{ExecuteSQLFile("037-invitation-redirect-url.sql")})

	// Version 38
	m = append(m, steps{ExecuteSQLFile("038-admin-console-resource.sql")})

	// Version 39
	m = append(m, steps{ExecuteSQLFile("039-resource-type-alter.sql")})

	// Version 40
	m = append(m, steps{ExecuteSQLFile("040-deferrable-constraints.sql")})

	// Version 41
	m = append(m, steps{ExecuteSQLFile("041-identity-role-index.sql")})

	// Version 42
	m = append(m, steps{ExecuteSQLFile("042-token-index.sql")})

	// Version 43
	m = append(m, steps{ExecuteSQLFile("043-add-admin-console-resource.sql")})

	// Version 44
	m = append(m, steps{ExecuteSQLFile("044-user-active.sql")})

	// Version 45
	m = append(m, steps{ExecuteSQLFile("045-identity-last-active.sql")})

	// Version 46
	m = append(m, steps{ExecuteSQLFile("046-identity-last-active-default.sql")})

	// Version 47
	m = append(m, steps{ExecuteSQLFile("047-add-user-banned-column.sql")})

	// Version 48
	m = append(m, steps{ExecuteSQLFile("048-identity-deactivation-notification.sql")})

	// Version 49
	m = append(m, steps{ExecuteSQLFile("049-user-banned-index.sql")})

	// Version 50
	m = append(m, steps{ExecuteSQLFile("050-worker-lock.sql")})

	// Version 51
	m = append(m, steps{ExecuteSQLFile("051-identity-deactivation-scheduled.sql")})

	// Version N
	//
	// In order to add an upgrade, simply append an array of MigrationFunc to the
	// the end of the "migrations" slice. The version numbers are determined by
	// the index in the array. The following code in comments show how you can
	// do a migration in 3 steps. If one of the steps fails, the others are not
	// executed.
	// If something goes wrong during the migration, all you need to do is return
	// an error that is not nil.

	/*
		m = append(m, steps{
			func(db *sql.Tx) error {
				// Execute random go code
				return nil
			},
			ExecuteSQLFile("YOUR_OWN_FILE.sql"),
			func(db *sql.Tx) error {
				// Execute random go code
				return nil
			},
		})
	*/

	return m
}
1249557
func (wr *Wrangler) MigrateServedFrom(ctx context.Context, keyspace, shard string, servedType topodatapb.TabletType, cells []string, reverse bool, filteredReplicationWaitTime time.Duration) (err error) {
	// read the destination keyspace, check it
	ki, err := wr.ts.GetKeyspace(ctx, keyspace)
	if err != nil {
		return err
	}
	if len(ki.ServedFroms) == 0 {
		return fmt.Errorf("destination keyspace %v is not a vertical split target", keyspace)
	}

	// read the destination shard, check it
	si, err := wr.ts.GetShard(ctx, keyspace, shard)
	if err != nil {
		return err
	}
	if len(si.SourceShards) != 1 || len(si.SourceShards[0].Tables) == 0 {
		return fmt.Errorf("destination shard %v/%v is not a vertical split target", keyspace, shard)
	}

	// check the migration is valid before locking (will also be checked
	// after locking to be sure)
	sourceKeyspace := si.SourceShards[0].Keyspace
	if err := ki.CheckServedFromMigration(servedType, cells, sourceKeyspace, !reverse); err != nil {
		return err
	}

	// lock the keyspaces, source first.
	ctx, unlock, lockErr := wr.ts.LockKeyspace(ctx, sourceKeyspace, fmt.Sprintf("MigrateServedFrom(%v)", servedType))
	if lockErr != nil {
		return lockErr
	}
	defer unlock(&err)
	ctx, unlock, lockErr = wr.ts.LockKeyspace(ctx, keyspace, fmt.Sprintf("MigrateServedFrom(%v)", servedType))
	if lockErr != nil {
		return lockErr
	}
	defer unlock(&err)

	// execute the migration
	err = wr.migrateServedFromLocked(ctx, ki, si, servedType, cells, reverse, filteredReplicationWaitTime)

	// rebuild the keyspace serving graph if there was no error
	if err == nil {
		err = topotools.RebuildKeyspaceLocked(ctx, wr.logger, wr.ts, keyspace, cells)
	}

	return err
}
1190568
func (s *Settings) applyChanges(changes settings.ItemChanges) {
	for _, ch := range changes {
		switch {
		case ch.IsAddition(), ch.IsModification():
			s.Set(ch.Key, ch.NewValue)
		case ch.IsDeletion():
			s.Delete(ch.Key)
		}
	}
}