Search code examples
javamysqlsqlfunctional-testing

Combine Java logging with mySQL


I am going to explain the path that led me to asking this question on the chance that I am going about this wrong (the lack of Google answers to help me are a good indicator that this might be possible).

I am writing functional tests for my project's API endpoints. I want my tests to clean up after themselves, this is problematic because when I create a test user with a POST. There is no corresponding DELETE. So I created a log file that lists the IDs of all of the users created.

Now I am attempting to create a stored procedure or SQL event that reads the IDs from the log file and then removes them from the Database. This seems like it shouldn't be difficult, but I cannot find anything online that would let me even know how to start. The LOAD DATA INFILE commands I've seen all want me to create a table. Can't I just read each line from a file without needing to create a new table in my database?


Solution

  • You have a couple of choices here. One is to read that log file with a Java program that makes it into a sequence of

    DELETE FROM table WHERE userid=value-from-the-logfile
    

    queries, and sends them to the database server.

    Another is to load the log file into a temporary table with LOAD DATA INFILE, then issue this query:

    DELETE FROM table WHERE userid IN (SELECT userid FROM temptable)