-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for using Spring Beans as Java based migrations. (#…
- Loading branch information
1 parent
a131d86
commit 4dce778
Showing
34 changed files
with
316 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ns-examples-sb/src/main/java/ac/simons/neo4j/migrations/examples/sb/PersonRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.neo4j.migrations.examples.sb; | ||
|
||
import org.springframework.data.neo4j.repository.Neo4jRepository; | ||
|
||
/** | ||
* Just for demo purposes. | ||
*/ | ||
public interface PersonRepository extends Neo4jRepository<Person, String> { | ||
} |
40 changes: 40 additions & 0 deletions
40
...-sb/src/main/java/ac/simons/neo4j/migrations/examples/sb/V040__AFreshSpringMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.neo4j.migrations.examples.sb; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import ac.simons.neo4j.migrations.core.JavaBasedMigration; | ||
import ac.simons.neo4j.migrations.core.MigrationContext; | ||
|
||
/** | ||
* An example that migrations might also be Spring components. | ||
* @author Michael J. Simons | ||
*/ | ||
@Component | ||
public class V040__AFreshSpringMigration implements JavaBasedMigration { | ||
|
||
private final PersonRepository personRepository; | ||
|
||
public V040__AFreshSpringMigration(PersonRepository personRepository) { | ||
this.personRepository = personRepository; | ||
} | ||
|
||
@Override | ||
public void apply(MigrationContext context) { | ||
personRepository.save(new Person("Kasper was here")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...eo4j/migrations/springframework/boot/autoconfigure/ApplicationContextAwareDiscoverer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.neo4j.migrations.springframework.boot.autoconfigure; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
|
||
import ac.simons.neo4j.migrations.core.Discoverer; | ||
import ac.simons.neo4j.migrations.core.JavaBasedMigration; | ||
import ac.simons.neo4j.migrations.core.MigrationContext; | ||
import ac.simons.neo4j.migrations.core.MigrationsException; | ||
import io.github.classgraph.ClassGraph; | ||
import io.github.classgraph.ScanResult; | ||
|
||
/** | ||
* A discoverer for {@link JavaBasedMigration Java based migrations} using the Spring context, thus enable components | ||
* being used as migrations. | ||
* | ||
* @author Michael J. Simons | ||
* @since 2.11.0 | ||
*/ | ||
final class ApplicationContextAwareDiscoverer implements Discoverer<JavaBasedMigration> { | ||
|
||
private final ObjectProvider<JavaBasedMigration> javaBasedMigrations; | ||
|
||
ApplicationContextAwareDiscoverer(ObjectProvider<JavaBasedMigration> javaBasedMigrations) { | ||
this.javaBasedMigrations = javaBasedMigrations; | ||
} | ||
|
||
@Override | ||
public Collection<JavaBasedMigration> discover(MigrationContext context) { | ||
|
||
var config = context.getConfig(); | ||
var result = new ArrayList<JavaBasedMigration>(); | ||
|
||
// First use all beans from the context | ||
javaBasedMigrations.forEach(result::add); | ||
// And keep a set of the already discovered types | ||
var loadedClasses = result.stream().map(JavaBasedMigration::getClass) | ||
.collect(Collectors.toSet()); | ||
|
||
// If configured, use the same algorithm as the default scanner | ||
if (config.getPackagesToScan().length != 0) { | ||
try (ScanResult scanResult = new ClassGraph() | ||
.enableAllInfo() | ||
.acceptPackages(config.getPackagesToScan()) | ||
.enableExternalClasses() | ||
.scan()) { | ||
|
||
scanResult | ||
.getClassesImplementing(JavaBasedMigration.class.getName()).loadClasses(JavaBasedMigration.class) | ||
.stream() | ||
.filter(c -> !(Modifier.isAbstract(c.getModifiers()) || loadedClasses.contains(c))) | ||
.map(c -> { | ||
try { | ||
return JavaBasedMigration.getDefaultConstructorFor(c).newInstance(); | ||
} catch (Exception e) { | ||
throw new MigrationsException("Could not instantiate migration " + c.getName(), e); | ||
} | ||
}).forEach(result::add); | ||
|
||
} | ||
} | ||
return List.copyOf(result); | ||
} | ||
} |
Oops, something went wrong.