-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
20 deletions.
There are no files selected for viewing
43 changes: 26 additions & 17 deletions
43
backend/src/test/java/mouda/backend/config/DatabaseCleaner.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 |
---|---|---|
@@ -1,26 +1,35 @@ | ||
package mouda.backend.config; | ||
|
||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import jakarta.persistence.EntityManager; | ||
|
||
@Component | ||
public class DatabaseCleaner { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public DatabaseCleaner(final JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void cleanUp() { | ||
String sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'PUBLIC' AND table_type='BASE TABLE'"; | ||
List<String> tables = jdbcTemplate.queryForList(sql, String.class); | ||
for (String tableName : tables) { | ||
jdbcTemplate.update("DELETE FROM " + tableName); | ||
jdbcTemplate.update("ALTER TABLE " + tableName + " alter column id restart with 1"); | ||
} | ||
} | ||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
public DatabaseCleaner(EntityManager entityManager) { | ||
this.entityManager = entityManager; | ||
} | ||
|
||
@Transactional | ||
public void cleanUp() { | ||
// String sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'PUBLIC' AND table_type='BASE TABLE'"; | ||
// List<String> tables = jdbcTemplate.queryForList(sql, String.class); | ||
// for (String tableName : tables) { | ||
// jdbcTemplate.update("DELETE FROM " + tableName); | ||
// jdbcTemplate.update("ALTER TABLE " + tableName + " alter column id restart with 1"); | ||
// } | ||
entityManager.createNativeQuery("DELETE FROM MEMBER").executeUpdate(); | ||
entityManager.createNativeQuery("ALTER TABLE MEMBER alter column id restart with 1").executeUpdate(); | ||
|
||
entityManager.createNativeQuery("DELETE FROM MOIM").executeUpdate(); | ||
entityManager.createNativeQuery("ALTER TABLE MOIM alter column id restart with 1").executeUpdate(); | ||
|
||
entityManager.clear(); | ||
} | ||
} |
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