Best EvoMaster code snippet using org.evomaster.client.java.controller.db.DbCleaner.resetSequences
Source:DbCleaner.java
...29 with FKs, we must temporarily disable the integrity checks30 */31 s.execute("SET REFERENTIAL_INTEGRITY FALSE");32 truncateTables(tablesToSkip, s, schemaName, false);33 resetSequences(s, schemaName);34 s.execute("SET REFERENTIAL_INTEGRITY TRUE");35 s.close();36 } catch (Exception e) {37 throw new RuntimeException(e);38 }39 }40 public static void clearDatabase_Postgres(Connection connection) {41 clearDatabase_Postgres(connection, "public", null);42 }43 public static void clearDatabase_Postgres(Connection connection, String schemaName, List<String> tablesToSkip) {44 try {45 Statement s = connection.createStatement();46 truncateTables(tablesToSkip, s, schemaName, true);47 resetSequences(s, schemaName);48 s.close();49 } catch (Exception e) {50 throw new RuntimeException(e);51 }52 }53 private static void truncateTables(List<String> tablesToSkip, Statement s, String schema, boolean singleCommand) throws SQLException {54 // Find all tables and truncate them55 Set<String> tables = new HashSet<>();56 ResultSet rs = s.executeQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='" + schema + "' AND (TABLE_TYPE='TABLE' OR TABLE_TYPE='BASE TABLE')");57 while (rs.next()) {58 tables.add(rs.getString(1));59 }60 rs.close();61 if (tables.isEmpty()) {62 throw new IllegalStateException("Could not find any table");63 }64 if (tablesToSkip != null) {65 for (String skip : tablesToSkip) {66 if (!tables.stream().anyMatch(t -> t.equalsIgnoreCase(skip))) {67 String msg = "Asked to skip table '" + skip + "', but it does not exist.";68 msg += " Existing tables in schema '"+schema+"': [" +69 tables.stream().collect(Collectors.joining(", ")) + "]";70 throw new IllegalStateException(msg);71 }72 }73 }74 List<String> tablesToClear = tables.stream()75 .filter(n -> tablesToSkip == null || tablesToSkip.isEmpty() ||76 !tablesToSkip.stream().anyMatch(skip -> skip.equalsIgnoreCase(n)))77 .collect(Collectors.toList());78 if (singleCommand) {79 String ts = tablesToClear.stream()80 .sorted()81 .collect(Collectors.joining(","));82 s.executeUpdate("TRUNCATE TABLE " + ts);83 } else {84 //note: if one at a time, need to make sure to first disable FK checks85 for(String t : tablesToClear){86 s.executeUpdate("TRUNCATE TABLE " + t);87 }88 }89 }90 private static void resetSequences(Statement s, String schema) throws SQLException {91 ResultSet rs;// Idem for sequences92 Set<String> sequences = new HashSet<>();93 rs = s.executeQuery("SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA='" + schema + "'");94 while (rs.next()) {95 sequences.add(rs.getString(1));96 }97 rs.close();98 for (String seq : sequences) {99 s.executeUpdate("ALTER SEQUENCE " + seq + " RESTART WITH 1");100 }101 /*102 Note: we reset all sequences from 1. But the original database might103 have used a different value.104 In most cases (99.99%), this should not be a problem....
resetSequences
Using AI Code Generation
1import org.evomaster.client.java.controller.db.DbCleaner;2import org.evomaster.client.java.controller.db.SqlScriptRunner;3public class EMResetDatabase {4 public static void main(String[] args) throws Exception {5 String pathToScript = "/path/to/sql/script";6 SqlScriptRunner runner = new SqlScriptRunner(pathToScript);7 runner.run();8 DbCleaner.resetSequences();9 }10}11DROP SCHEMA public CASCADE;12CREATE SCHEMA public;13GRANT ALL ON SCHEMA public TO postgres;14GRANT ALL ON SCHEMA public TO public;
resetSequences
Using AI Code Generation
1org.evomaster.client.java.controller.db.DbCleaner.resetSequences(Arrays.asList("table_name1", "table_name2"));2org.evomaster.client.java.controller.db.DbCleaner.resetSequences(Arrays.asList("table_name1", "table_name2"));3org.evomaster.client.java.controller.db.DbCleaner.resetSequences(Arrays.asList("table_name1", "table_name2"));4org.evomaster.client.java.controller.db.DbCleaner.resetSequences(Arrays.asList("table_name1", "table_name2"));5org.evomaster.client.java.controller.db.DbCleaner.resetSequences(Arrays.asList("table_name1", "table_name2"));6org.evomaster.client.java.controller.db.DbCleaner.resetSequences(Arrays.asList("table_name1", "table
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!