-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to clean the DB #15827
Closed
Closed
Add ability to clean the DB #15827
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions
27
extensions/agroal/deployment/src/main/resources/dev-templates/clean.html
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,27 @@ | ||
{#include main} | ||
{#title}Clean Databases{/title} | ||
{#body} | ||
<table class="table table-striped"> | ||
<thead class="thead-dark"> | ||
<tr> | ||
<th scope="col">Datasource</th> | ||
<th scope="col">Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{#for db in info:dbs} | ||
<tr> | ||
<td> | ||
{db} | ||
</td> | ||
<td> | ||
<form method="post" enctype="application/x-www-form-urlencoded"> | ||
<input type="hidden" name="name" value="{db}"> | ||
<input id="invoke" type="submit" value="Invoke" class="btn btn-primary btn-sm"> | ||
</form> | ||
</td> | ||
{/for} | ||
</tbody> | ||
</table> | ||
{/body} | ||
{/include} |
3 changes: 3 additions & 0 deletions
3
extensions/agroal/deployment/src/main/resources/dev-templates/embedded.html
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,3 @@ | ||
<a href="{urlbase}/clean" class="badge badge-light"> | ||
<i class="fa fa-database fa-fw"></i> | ||
Reset Databases</a> |
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 | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,16 @@ | ||||||
package io.quarkus.agroal.runtime; | ||||||
|
||||||
import java.util.ServiceLoader; | ||||||
import java.util.function.Supplier; | ||||||
|
||||||
import io.agroal.api.AgroalDataSource; | ||||||
import io.quarkus.agroal.runtime.schema.DatabaseSchemaProvider; | ||||||
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig; | ||||||
import io.quarkus.devconsole.runtime.spi.DevConsolePostHandler; | ||||||
import io.quarkus.runtime.annotations.Recorder; | ||||||
import io.vertx.core.Handler; | ||||||
import io.vertx.core.MultiMap; | ||||||
import io.vertx.ext.web.RoutingContext; | ||||||
|
||||||
@Recorder | ||||||
public class AgroalRecorder { | ||||||
|
@@ -29,4 +35,20 @@ public AgroalDataSource get() { | |||||
}; | ||||||
} | ||||||
|
||||||
public Handler<RoutingContext> devConsoleCleanDatabaseHandler() { | ||||||
// the usual issue of Vert.x hanging on to the first TCCL and setting it on all its threads | ||||||
final ClassLoader currentCl = Thread.currentThread().getContextClassLoader(); | ||||||
return new DevConsolePostHandler() { | ||||||
@Override | ||||||
protected void handlePost(RoutingContext event, MultiMap form) throws Exception { | ||||||
String name = form.get("name"); | ||||||
ServiceLoader<DatabaseSchemaProvider> dbs = ServiceLoader.load(DatabaseSchemaProvider.class, | ||||||
Thread.currentThread().getContextClassLoader()); | ||||||
for (DatabaseSchemaProvider i : dbs) { | ||||||
i.resetDatabase(name); | ||||||
} | ||||||
flashMessage(event, "Action invoked"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
}; | ||||||
} | ||||||
} |
33 changes: 33 additions & 0 deletions
33
...roal/runtime/src/main/java/io/quarkus/agroal/runtime/schema/CleanDatabaseInterceptor.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,33 @@ | ||
package io.quarkus.agroal.runtime.schema; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.InvocationContext; | ||
|
||
public class CleanDatabaseInterceptor { | ||
|
||
final List<DatabaseSchemaProvider> providers; | ||
|
||
public CleanDatabaseInterceptor() { | ||
this.providers = new ArrayList<>(); | ||
ServiceLoader<DatabaseSchemaProvider> dbs = ServiceLoader.load(DatabaseSchemaProvider.class, | ||
Thread.currentThread().getContextClassLoader()); | ||
for (DatabaseSchemaProvider i : dbs) { | ||
providers.add(i); | ||
} | ||
} | ||
|
||
@AroundInvoke | ||
public Object intercept(InvocationContext context) throws Exception { | ||
try { | ||
return context.proceed(); | ||
} finally { | ||
for (DatabaseSchemaProvider i : providers) { | ||
i.resetAllDatabases(); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...agroal/runtime/src/main/java/io/quarkus/agroal/runtime/schema/DatabaseSchemaProvider.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,11 @@ | ||
package io.quarkus.agroal.runtime.schema; | ||
|
||
/** | ||
* A service interface that can be used to reset the database for dev and test mode. | ||
*/ | ||
public interface DatabaseSchemaProvider { | ||
|
||
void resetDatabase(String dbName); | ||
|
||
void resetAllDatabases(); | ||
} |
23 changes: 23 additions & 0 deletions
23
extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywaySchemaProvider.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,23 @@ | ||
package io.quarkus.flyway.runtime; | ||
|
||
import io.quarkus.agroal.runtime.schema.DatabaseSchemaProvider; | ||
|
||
public class FlywaySchemaProvider implements DatabaseSchemaProvider { | ||
@Override | ||
public void resetDatabase(String dbName) { | ||
for (FlywayContainer i : FlywayRecorder.FLYWAY_CONTAINERS) { | ||
if (i.getDataSourceName().equals(dbName)) { | ||
i.getFlyway().clean(); | ||
i.getFlyway().migrate(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void resetAllDatabases() { | ||
for (FlywayContainer i : FlywayRecorder.FLYWAY_CONTAINERS) { | ||
i.getFlyway().clean(); | ||
i.getFlyway().migrate(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../main/resources/META-INF/services/io.quarkus.agroal.runtime.schema.DatabaseSchemaProvider
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 @@ | ||
io.quarkus.flyway.runtime.FlywaySchemaProvider |
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
37 changes: 37 additions & 0 deletions
37
...ent/src/test/java/io/quarkus/hibernate/orm/HibernateSchemaRecreateDevConsoleTestCase.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,37 @@ | ||
package io.quarkus.hibernate.orm; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusDevModeTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class HibernateSchemaRecreateDevConsoleTestCase { | ||
@RegisterExtension | ||
final static QuarkusDevModeTest TEST = new QuarkusDevModeTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(MyEntity.class, MyEntityTestResource.class) | ||
.addAsResource("application.properties") | ||
.addAsResource("import.sql")); | ||
|
||
@Test | ||
public void testCleanDatabase() { | ||
RestAssured.when().get("/my-entity/count").then().body(is("2")); | ||
RestAssured.when().get("/my-entity/add").then().body(is("MyEntity:added")); | ||
RestAssured.when().get("/my-entity/count").then().body(is("3")); | ||
RestAssured.with() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I never realised that tests could access dev mode actions. |
||
.redirects().follow(false).formParam("name", "<default>").post("q/dev/io.quarkus.quarkus-agroal/clean") | ||
.then() | ||
.statusCode(303); | ||
RestAssured.when().get("/my-entity/count").then().body(is("2")); | ||
|
||
} | ||
|
||
private void assertBodyIs(String expectedBody) { | ||
RestAssured.when().get("/my-entity/2").then().body(is(expectedBody)); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
extensions/hibernate-orm/deployment/src/test/resources/import.sql
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
INSERT INTO MyEntity(id, name) VALUES(1, 'default sql load script entity'); | ||
INSERT INTO MyEntity(id, name) VALUES(2, 'import.sql load script entity'); | ||
alter sequence myEntitySeq restart with 3; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shit we do to deal with modularity :(