@Test void migrationFrom0_23_0() { final ProjectManager pm = manager.projectManager(); final CommandExecutor executor = manager.executor(); pm.get(INTERNAL_PROJ).repos().create("main", Author.SYSTEM); final UserAndTimestamp userAndTimestamp = UserAndTimestamp.of(author); final Tokens tokens = new Tokens( ImmutableMap.of("app1", new Token("app1", "secret1", false, userAndTimestamp, null)), ImmutableMap.of("secret1", "app1")); executor.execute( Command.push(author, INTERNAL_PROJ, "main", Revision.HEAD, "", "", Markup.PLAINTEXT, Change.ofJsonUpsert(TOKEN_JSON, Jackson.valueToTree(tokens)))).join(); final Project project = createProjectWithoutMetadata("project1"); final ProjectMetadata metadata = new ProjectMetadata(project.name(), ImmutableMap.of(REPO_FOO, new RepositoryMetadata(REPO_FOO, userAndTimestamp, PerRolePermissions.ofPublic())), ImmutableMap.of(), ImmutableMap.of(), userAndTimestamp, null); executor.execute( Command.push(author, project.name(), REPO_META, Revision.HEAD, "", "", Markup.PLAINTEXT, Change.ofJsonUpsert(METADATA_JSON, Jackson.valueToTree(metadata)))).join(); MigrationUtil.migrate(pm, executor); final MetadataService mds = new MetadataService(pm, executor); final ProjectMetadata meta = mds.getProject("project1").join(); assertThat(meta).isNotNull(); assertThat(meta.repo(REPO_FOO)).isNotNull(); assertThat(meta.repo(REPO_META)).isNotNull(); assertThat(meta.creation()).isEqualToComparingFieldByField(userAndTimestamp); assertThat(project.repos().get(REPO_DOGMA) .getOrNull(Revision.HEAD, METADATA_JSON).join()).isNotNull(); assertThat(project.repos().get(REPO_META) .getOrNull(Revision.HEAD, METADATA_JSON).join()).isNull(); final Token token = mds.findTokenByAppId("app1").join(); assertThat(token).isNotNull(); assertThat(token.secret()).isEqualTo("secret1"); assertThat(token.isAdmin()).isFalse(); assertThat(token.isActive()).isTrue(); assertThat(token.creation()).isEqualToComparingFieldByField(userAndTimestamp); assertThat(pm.get(INTERNAL_PROJ).repos() .get(REPO_DOGMA) .getOrNull(Revision.HEAD, TOKEN_JSON).join()).isNotNull(); assertThat(pm.get(INTERNAL_PROJ).repos() .get("main") .getOrNull(Revision.HEAD, TOKEN_JSON).join()).isNull(); }
public static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor) { requireNonNull(projectManager, "projectManager"); requireNonNull(executor, "executor"); final RepositorySupport<ProjectMetadata> metadataRepo = new RepositorySupport<>(projectManager, executor, entry -> convertWithJackson(entry, ProjectMetadata.class)); final UserAndTimestamp userAndTimestamp = UserAndTimestamp.of(author); final Entry<?> tokenEntry = projectManager.get(INTERNAL_PROJ).repos() .get(Project.REPO_DOGMA) .getOrNull(Revision.HEAD, TOKEN_JSON).join(); final Collection<Token> migratedTokens = tokenEntry == null ? migrateTokens(projectManager, executor) : ImmutableSet.of(); migratedTokens.forEach(token -> logger.info("Token '{}' has been migrated", token.id())); final SafeProjectManager safeProjectManager = new SafeProjectManager(projectManager); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } if (!p.repos().exists(Project.REPO_META)) { return; } try { final ProjectMetadata metadata = metadataRepo.fetch(p.name(), Project.REPO_META, METADATA_JSON).join().object(); metadata.repos().put(Project.REPO_META, new RepositoryMetadata(Project.REPO_META, userAndTimestamp, PerRolePermissions.ofPublic())); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); metadataRepo.push(p.name(), Project.REPO_META, author, "Remove the old metadata file", Change.ofRemoval(METADATA_JSON)).join(); } catch (Throwable cause) { cause = Exceptions.peel(cause); if (!(cause instanceof EntryNotFoundException)) { Exceptions.throwUnsafely(cause); } } }); final UserAndTimestamp creationTime = UserAndTimestamp.of(author); final Map<String, TokenRegistration> registrations = migratedTokens.stream() .map(t -> new TokenRegistration(t.id(), ProjectRole.MEMBER, creationTime)) .collect(toMap(TokenRegistration::id, Function.identity())); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } final Map<String, RepositoryMetadata> repos = p.repos().list().values().stream() .filter(r -> !r.name().equals(Project.REPO_DOGMA)) .map(r -> new RepositoryMetadata(r.name(), userAndTimestamp, PerRolePermissions.ofPublic())) .collect(toMap(RepositoryMetadata::name, Function.identity())); final ProjectMetadata metadata = new ProjectMetadata(p.name(), repos, ImmutableMap.of(), registrations, userAndTimestamp, null); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); }); }
MigrationUtil { public static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor) { requireNonNull(projectManager, "projectManager"); requireNonNull(executor, "executor"); final RepositorySupport<ProjectMetadata> metadataRepo = new RepositorySupport<>(projectManager, executor, entry -> convertWithJackson(entry, ProjectMetadata.class)); final UserAndTimestamp userAndTimestamp = UserAndTimestamp.of(author); final Entry<?> tokenEntry = projectManager.get(INTERNAL_PROJ).repos() .get(Project.REPO_DOGMA) .getOrNull(Revision.HEAD, TOKEN_JSON).join(); final Collection<Token> migratedTokens = tokenEntry == null ? migrateTokens(projectManager, executor) : ImmutableSet.of(); migratedTokens.forEach(token -> logger.info("Token '{}' has been migrated", token.id())); final SafeProjectManager safeProjectManager = new SafeProjectManager(projectManager); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } if (!p.repos().exists(Project.REPO_META)) { return; } try { final ProjectMetadata metadata = metadataRepo.fetch(p.name(), Project.REPO_META, METADATA_JSON).join().object(); metadata.repos().put(Project.REPO_META, new RepositoryMetadata(Project.REPO_META, userAndTimestamp, PerRolePermissions.ofPublic())); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); metadataRepo.push(p.name(), Project.REPO_META, author, "Remove the old metadata file", Change.ofRemoval(METADATA_JSON)).join(); } catch (Throwable cause) { cause = Exceptions.peel(cause); if (!(cause instanceof EntryNotFoundException)) { Exceptions.throwUnsafely(cause); } } }); final UserAndTimestamp creationTime = UserAndTimestamp.of(author); final Map<String, TokenRegistration> registrations = migratedTokens.stream() .map(t -> new TokenRegistration(t.id(), ProjectRole.MEMBER, creationTime)) .collect(toMap(TokenRegistration::id, Function.identity())); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } final Map<String, RepositoryMetadata> repos = p.repos().list().values().stream() .filter(r -> !r.name().equals(Project.REPO_DOGMA)) .map(r -> new RepositoryMetadata(r.name(), userAndTimestamp, PerRolePermissions.ofPublic())) .collect(toMap(RepositoryMetadata::name, Function.identity())); final ProjectMetadata metadata = new ProjectMetadata(p.name(), repos, ImmutableMap.of(), registrations, userAndTimestamp, null); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); }); } }
MigrationUtil { public static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor) { requireNonNull(projectManager, "projectManager"); requireNonNull(executor, "executor"); final RepositorySupport<ProjectMetadata> metadataRepo = new RepositorySupport<>(projectManager, executor, entry -> convertWithJackson(entry, ProjectMetadata.class)); final UserAndTimestamp userAndTimestamp = UserAndTimestamp.of(author); final Entry<?> tokenEntry = projectManager.get(INTERNAL_PROJ).repos() .get(Project.REPO_DOGMA) .getOrNull(Revision.HEAD, TOKEN_JSON).join(); final Collection<Token> migratedTokens = tokenEntry == null ? migrateTokens(projectManager, executor) : ImmutableSet.of(); migratedTokens.forEach(token -> logger.info("Token '{}' has been migrated", token.id())); final SafeProjectManager safeProjectManager = new SafeProjectManager(projectManager); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } if (!p.repos().exists(Project.REPO_META)) { return; } try { final ProjectMetadata metadata = metadataRepo.fetch(p.name(), Project.REPO_META, METADATA_JSON).join().object(); metadata.repos().put(Project.REPO_META, new RepositoryMetadata(Project.REPO_META, userAndTimestamp, PerRolePermissions.ofPublic())); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); metadataRepo.push(p.name(), Project.REPO_META, author, "Remove the old metadata file", Change.ofRemoval(METADATA_JSON)).join(); } catch (Throwable cause) { cause = Exceptions.peel(cause); if (!(cause instanceof EntryNotFoundException)) { Exceptions.throwUnsafely(cause); } } }); final UserAndTimestamp creationTime = UserAndTimestamp.of(author); final Map<String, TokenRegistration> registrations = migratedTokens.stream() .map(t -> new TokenRegistration(t.id(), ProjectRole.MEMBER, creationTime)) .collect(toMap(TokenRegistration::id, Function.identity())); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } final Map<String, RepositoryMetadata> repos = p.repos().list().values().stream() .filter(r -> !r.name().equals(Project.REPO_DOGMA)) .map(r -> new RepositoryMetadata(r.name(), userAndTimestamp, PerRolePermissions.ofPublic())) .collect(toMap(RepositoryMetadata::name, Function.identity())); final ProjectMetadata metadata = new ProjectMetadata(p.name(), repos, ImmutableMap.of(), registrations, userAndTimestamp, null); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); }); } private  MigrationUtil(); }
MigrationUtil { public static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor) { requireNonNull(projectManager, "projectManager"); requireNonNull(executor, "executor"); final RepositorySupport<ProjectMetadata> metadataRepo = new RepositorySupport<>(projectManager, executor, entry -> convertWithJackson(entry, ProjectMetadata.class)); final UserAndTimestamp userAndTimestamp = UserAndTimestamp.of(author); final Entry<?> tokenEntry = projectManager.get(INTERNAL_PROJ).repos() .get(Project.REPO_DOGMA) .getOrNull(Revision.HEAD, TOKEN_JSON).join(); final Collection<Token> migratedTokens = tokenEntry == null ? migrateTokens(projectManager, executor) : ImmutableSet.of(); migratedTokens.forEach(token -> logger.info("Token '{}' has been migrated", token.id())); final SafeProjectManager safeProjectManager = new SafeProjectManager(projectManager); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } if (!p.repos().exists(Project.REPO_META)) { return; } try { final ProjectMetadata metadata = metadataRepo.fetch(p.name(), Project.REPO_META, METADATA_JSON).join().object(); metadata.repos().put(Project.REPO_META, new RepositoryMetadata(Project.REPO_META, userAndTimestamp, PerRolePermissions.ofPublic())); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); metadataRepo.push(p.name(), Project.REPO_META, author, "Remove the old metadata file", Change.ofRemoval(METADATA_JSON)).join(); } catch (Throwable cause) { cause = Exceptions.peel(cause); if (!(cause instanceof EntryNotFoundException)) { Exceptions.throwUnsafely(cause); } } }); final UserAndTimestamp creationTime = UserAndTimestamp.of(author); final Map<String, TokenRegistration> registrations = migratedTokens.stream() .map(t -> new TokenRegistration(t.id(), ProjectRole.MEMBER, creationTime)) .collect(toMap(TokenRegistration::id, Function.identity())); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } final Map<String, RepositoryMetadata> repos = p.repos().list().values().stream() .filter(r -> !r.name().equals(Project.REPO_DOGMA)) .map(r -> new RepositoryMetadata(r.name(), userAndTimestamp, PerRolePermissions.ofPublic())) .collect(toMap(RepositoryMetadata::name, Function.identity())); final ProjectMetadata metadata = new ProjectMetadata(p.name(), repos, ImmutableMap.of(), registrations, userAndTimestamp, null); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); }); } private  MigrationUtil(); static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor); }
MigrationUtil { public static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor) { requireNonNull(projectManager, "projectManager"); requireNonNull(executor, "executor"); final RepositorySupport<ProjectMetadata> metadataRepo = new RepositorySupport<>(projectManager, executor, entry -> convertWithJackson(entry, ProjectMetadata.class)); final UserAndTimestamp userAndTimestamp = UserAndTimestamp.of(author); final Entry<?> tokenEntry = projectManager.get(INTERNAL_PROJ).repos() .get(Project.REPO_DOGMA) .getOrNull(Revision.HEAD, TOKEN_JSON).join(); final Collection<Token> migratedTokens = tokenEntry == null ? migrateTokens(projectManager, executor) : ImmutableSet.of(); migratedTokens.forEach(token -> logger.info("Token '{}' has been migrated", token.id())); final SafeProjectManager safeProjectManager = new SafeProjectManager(projectManager); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } if (!p.repos().exists(Project.REPO_META)) { return; } try { final ProjectMetadata metadata = metadataRepo.fetch(p.name(), Project.REPO_META, METADATA_JSON).join().object(); metadata.repos().put(Project.REPO_META, new RepositoryMetadata(Project.REPO_META, userAndTimestamp, PerRolePermissions.ofPublic())); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); metadataRepo.push(p.name(), Project.REPO_META, author, "Remove the old metadata file", Change.ofRemoval(METADATA_JSON)).join(); } catch (Throwable cause) { cause = Exceptions.peel(cause); if (!(cause instanceof EntryNotFoundException)) { Exceptions.throwUnsafely(cause); } } }); final UserAndTimestamp creationTime = UserAndTimestamp.of(author); final Map<String, TokenRegistration> registrations = migratedTokens.stream() .map(t -> new TokenRegistration(t.id(), ProjectRole.MEMBER, creationTime)) .collect(toMap(TokenRegistration::id, Function.identity())); safeProjectManager.list().values().forEach(p -> { if (alreadyMigrated(p)) { return; } final Map<String, RepositoryMetadata> repos = p.repos().list().values().stream() .filter(r -> !r.name().equals(Project.REPO_DOGMA)) .map(r -> new RepositoryMetadata(r.name(), userAndTimestamp, PerRolePermissions.ofPublic())) .collect(toMap(RepositoryMetadata::name, Function.identity())); final ProjectMetadata metadata = new ProjectMetadata(p.name(), repos, ImmutableMap.of(), registrations, userAndTimestamp, null); commitProjectMetadata(metadataRepo, executor, p.name(), metadata); }); } private  MigrationUtil(); static synchronized void migrate(ProjectManager projectManager, CommandExecutor executor);  }