Best EvoMaster code snippet using org.evomaster.client.java.controller.db.DbCleaner.resetSequenceCommand
Source:DbCleaner.java
...172 sequences.add(rs.getString(1));173 }174 rs.close();175 for (String seq : sequences) {176 s.executeUpdate(resetSequenceCommand(seq, type));177 }178 /*179 Note: we reset all sequences from 1. But the original database might180 have used a different value.181 In most cases (99.99%), this should not be a problem.182 We could allow using different values in this API... but, maybe just easier183 for the user to reset it manually if really needed?184 */185 }186 private static void disableReferentialIntegrity(Statement s, DatabaseType type) throws SQLException {187 switch (type)188 {189 case POSTGRES: break;190 case MS_SQL_SERVER:191 //https://stackoverflow.com/questions/159038/how-can-foreign-key-constraints-be-temporarily-disabled-using-t-sql192 //https://stackoverflow.com/questions/155246/how-do-you-truncate-all-tables-in-a-database-using-tsql#156813193 //https://docs.microsoft.com/en-us/sql/relational-databases/tables/disable-foreign-key-constraints-with-insert-and-update-statements?view=sql-server-ver15194 s.execute("EXEC sp_MSForEachTable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"");195 break;196 case H2:197 s.execute("SET REFERENTIAL_INTEGRITY FALSE");198 break;199 case MARIADB:200 case MYSQL:201 s.execute("SET @@foreign_key_checks = 0;");202 break;203 case OTHER:204 throw new DbUnsupportedException(type);205 }206 }207 private static void enableReferentialIntegrity(Statement s, DatabaseType type) throws SQLException {208 switch (type)209 {210 case POSTGRES: break;211 case MS_SQL_SERVER:212 s.execute("exec sp_MSForEachTable \"ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all\"");213 break;214 case H2:215 /*216 For H2, we have to delete tables one at a time... but, to avoid issues217 with FKs, we must temporarily disable the integrity checks218 */219 s.execute( "SET REFERENTIAL_INTEGRITY TRUE");220 break;221 case MARIADB:222 case MYSQL:223 s.execute("SET @@foreign_key_checks = 1;");224 break;225 case OTHER:226 throw new DbUnsupportedException(type);227 }228 }229 private static int getDefaultReties(DatabaseType type){230 switch (type){231 case MS_SQL_SERVER:232 case POSTGRES: return 0;233 case H2:234 case MARIADB:235 case MYSQL: return 3;236 }237 throw new DbUnsupportedException(type);238 }239 private static String getDefaultSchema(DatabaseType type){240 switch (type){241 case H2: return "PUBLIC";242 //https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/ownership-and-user-schema-separation-in-sql-server243 case MS_SQL_SERVER: return "dbo";244 case MARIADB:245 case MYSQL: throw new IllegalArgumentException("there is no default schema for "+type+", and you must specify a db name here");246 case POSTGRES: return "public";247 }248 throw new DbUnsupportedException(type);249 }250 private static boolean isSingleCleanCommand(DatabaseType type){251 return type == DatabaseType.POSTGRES;252 }253 private static String getAllTableHasIdentify(DatabaseType type, String schema){254 if(type != DatabaseType.MS_SQL_SERVER)255 throw new IllegalArgumentException("getAllTableHasIdentify only supports for MS_SQL_SERVER, not for "+type);256 return getAllTableCommand(type, schema) + " AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1";257 }258 private static String getAllTableCommand(DatabaseType type, String schema) {259 String command = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where (TABLE_TYPE='TABLE' OR TABLE_TYPE='BASE TABLE')";260 switch (type){261 // https://stackoverflow.com/questions/175415/how-do-i-get-list-of-all-tables-in-a-database-using-tsql, TABLE_CATALOG='"+dbname+"'"262 case MS_SQL_SERVER:263 // for MySQL, schema is dbname264 case MYSQL:265 case MARIADB:266 case H2:267 case POSTGRES:268 if (schema.isEmpty())269 return command;270 return command + " AND TABLE_SCHEMA='" + schema +"'";271 }272 throw new DbUnsupportedException(type);273// case MS_SQL_SERVER_2000:274// return "SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = '"+schema+"'"; // shcema can be 'U'275 }276 private static String getAllSequenceCommand(DatabaseType type, String schemaName) {277 String command = "SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.SEQUENCES";278 switch (type){279 case MYSQL:280 case MARIADB: return getAllTableCommand(type, schemaName);281 case H2:282 case POSTGRES:283 //https://docs.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/sequences-transact-sql?view=sql-server-ver15284 case MS_SQL_SERVER:285 if (schemaName.isEmpty())286 return command;287 else288 return command + " WHERE SEQUENCE_SCHEMA='" + schemaName + "'";289 }290 throw new DbUnsupportedException(type);291 }292 private static String resetSequenceCommand(String sequence, DatabaseType type) {293 switch (type){294 case MARIADB:295 case MYSQL: return "ALTER TABLE " + sequence + " AUTO_INCREMENT=1;";296 case MS_SQL_SERVER:297 case H2:298 case POSTGRES: return "ALTER SEQUENCE " + sequence + " RESTART WITH 1";299 }300 throw new DbUnsupportedException(type);301 }302}...
resetSequenceCommand
Using AI Code Generation
1package org.evomaster.client.java.controller.db;2import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;3import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;4import org.evomaster.client.java.controller.api.dto.database.operations.ResetSequenceCommandDto;5import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;6import java.sql.Connection;7import java.sql.SQLException;8import java.util.ArrayList;9import java.util.List;10public class ResetSequenceEMTest extends DbTestBase {11 public List<DatabaseCommandDto> getInitDbCommands() {12 List<DatabaseCommandDto> commands = new ArrayList<>();13 commands.addAll(SqlScriptExecutor.getInitDbCommandsFromResource("resetSequence.sql"));14 return commands;15 }16 public void checkStateOfSutAfterTest() throws SQLException {17 try (Connection c = getConnection()) {18 TableDto table = DbSchemaDto.extractTable(getSchema(), "test_table");19 String expected = "1";20 String actual = DbUtils.getValueFromTable(c, table, "id");21 if (!expected.equals(actual)) {22 throw new IllegalStateException("Expected id to be " + expected + ", but was " + actual);23 }24 }25 }26 public List<DatabaseCommandDto> getDatabaseCommandsAfterTest() {27 List<DatabaseCommandDto> commands = new ArrayList<>();28 commands.add(new ResetSequenceCommandDto("test_table", "id", 100));29 return commands;30 }31}32package org.evomaster.client.java.controller.db;33import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;34import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;35import org.evomaster.client.java.controller.api.dto.database.operations.ResetSequenceCommandDto;36import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;37import java.sql.Connection;38import java.sql.SQLException;39import java.util.ArrayList;40import java.util.List;
resetSequenceCommand
Using AI Code Generation
1DbCleaner.resetSequenceCommand("users", 1);2DbCleaner.resetSequenceCommand("users", 100);3DbCleaner.resetSequenceCommand("users", 1000);4DbCleaner.resetSequenceCommand("users", 10000);5DbCleaner.resetSequenceCommand("users", 100000);6DbCleaner.resetSequenceCommand("users", 1000000);7DbCleaner.resetSequenceCommand("users", 10000000);8DbCleaner.resetSequenceCommand("users", 100000000);9DbCleaner.resetSequenceCommand("users", 1000000000);10DbCleaner.resetSequenceCommand("users", 10000000000);11DbCleaner.resetSequenceCommand("users", 100000000000);12DbCleaner.resetSequenceCommand("users",
resetSequenceCommand
Using AI Code Generation
1DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");2DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");3DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");4DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");5DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");6DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");7DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");8DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");9DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");10DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");11DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");12DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");13DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");14DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");15DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");16DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");17DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");18DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");19DbCleaner.resetSequenceCommand("public", "users", "id", "postgres");20DbCleaner.resetSequenceCommand("public", "users", "id",
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!!