title: Why is my database not being cleared after each unit test?
body: <p>I am trying to run a tests when no user is created and one can sign up for the first time. The test runs perfectly the first time and passes. The thing is that if I run it a second time, my container is not deleted properly and we end up with a <code>HTTPStatus.conflict</code> instead of <code>.ok</code>, so the user does already exist.</p>
  <p>I am running a Docker container on my MacBook with <code>docker-compose-testing</code>, <code>docker-compose</code> and <code>testing.Dockerfile</code> set.</p>
  <p>This error is also triggered when running the test:</p>
  <p><code>caught error: &quot;server: syntax error at end of input (scanner_yyerror)&quot;</code></p>
  <p>What is it missing here? Wouldn't <code>autoRevert()</code> and <code>autoMigrate()</code> clear my database?</p>
  <pre class="lang-swift prettyprint-override"><code>func testSignUpRoute_userDoesNotExistInDatabase_userSignUpAndHTTPStatusIsOk() throws {
    // Configuration in setUp and tearDown.
    var app = Application(.testing)
    defer { app.shutdown() }
    try configure(app)
    try app.autoRevert().wait()
    try app.autoMigrate().wait()
    
    // Given
    let expected: HTTPStatus = .ok
    let headers = [
      &quot;email&quot;: &quot;foo@email.com&quot;,
      &quot;password&quot;: &quot;fooEncrypted&quot;
    ]
    
    // When
    try app.test(.POST, &quot;users/signup&quot;) { request in
      try request.content.encode(headers)
    } afterResponse: { response in
      let result = response.status
      
      // Then
      XCTAssertEqual(result, expected, &quot;Response status must be \(expected)&quot;)
    }
  }
  </code></pre>
  <p>This is the order in which my migration happens in the <code>configure.swift</code> file:</p>
  <pre class="lang-swift prettyprint-override"><code>public func configure(_ app: Application) throws {
    app.migrations.add(UserModelMigration_v1_0_0())
    app.migrations.add(AddressModelMigration_v1_0_0())
  }
  </code></pre>
  <p>And the this is how I revert all my models. <code>AddressModel</code> and <code>CompanyModel</code> are <code>@OptionalChild</code> of the <code>UserModel</code>. It seems like the issue comes from here, but I can not point it out.</p>
  <pre class="lang-swift prettyprint-override"><code>struct UserModelMigration_v1_0_0: Migration {
  
    func prepare(on database: Database) -&gt; EventLoopFuture&lt;Void&gt; {
      database.schema(UserModel.schema)
        .id()
        .field(UserModel.Key.email, .string, .required)
        .field(UserModel.Key.password, .string, .required)
        .unique(on: UserModel.Key.email)
        .create()
    }
  
    func revert(on database: Database) -&gt; EventLoopFuture&lt;Void&gt; {
      database
        .schema(UserModel.schema)
        .update()
    }
  }
  </code></pre>
  <pre class="lang-swift prettyprint-override"><code>struct AddressModelMigration_v1_0_0: Migration {
  
    func prepare(on database: Database) -&gt; EventLoopFuture&lt;Void&gt; {
      database.schema(AddressModel.schema)
        .id()
        .field(AddressModel.Key.streetLineOne, .string, .required)
        .field(AddressModel.Key.city, .string, .required)
        .field(AddressModel.Key.userID, .uuid, .required,
               .references(UserModel.schema, .id,
                           onDelete: .cascade,
                           onUpdate: .cascade))
        .unique(on: AddressModel.Key.userID)
        .create()
    }
  
    func revert(on database: Database) -&gt; EventLoopFuture&lt;Void&gt; {
      database
        .schema(AddressModel.schema)
        .update()
    }
  }
  </code></pre>
  <p>This is my <code>docker-compose-testing.yml</code> file</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '3'
  
  services:
  
    testingonlinux:
  
      depends_on:
        - postgres
  
      build:
        context: .
        dockerfile: testing.Dockerfile
  
      environment:
        - DATABASE_HOST=postgres
        - DATABASE_PORT=5434
  
    postgres:
  
      image: &quot;postgres&quot;
  
      environment:
        - POSTGRES_DB=foo_db_testing
        - POSTGRES_USER=foo_user
        - POSTGRES_PASSWORD=foo_password
  </code></pre>
  <p>This is my <code>docker-compose.yml</code> file</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '3.8'
  
  volumes:
    db_data:
  
  x-shared_environment: &amp;shared_environment
    LOG_LEVEL: ${LOG_LEVEL:-debug}
    DATABASE_HOST: db
    DATABASE_NAME: vapor_database
    DATABASE_USERNAME: vapor_username
    DATABASE_PASSWORD: vapor_password
  
  services:
    app:
      image: FooServerSide:latest
      build:
        context: .
      environment:
        &lt;&lt;: *shared_environment
      depends_on:
        - db
      ports:
        - '8080:8080'
      # user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
      command: [&quot;serve&quot;, &quot;--env&quot;, &quot;production&quot;, &quot;--hostname&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8080&quot;]
    migrate:
      image: FooServerSide:latest
      build:
        context: .
      environment:
        &lt;&lt;: *shared_environment
      depends_on:
        - db
      command: [&quot;migrate&quot;, &quot;--yes&quot;]
      deploy:
        replicas: 0
    revert:
      image: FooServerSide:latest
      build:
        context: .
      environment:
        &lt;&lt;: *shared_environment
      depends_on:
        - db
      command: [&quot;migrate&quot;, &quot;--revert&quot;, &quot;--yes&quot;]
      deploy:
        replicas: 0
    db:
      image: postgres:12-alpine
      volumes:
        - db_data:/var/lib/postgresql/data/pgdata
      environment:
        PGDATA: /var/lib/postgresql/data/pgdata
        POSTGRES_USER: vapor_username
        POSTGRES_PASSWORD: vapor_password
        POSTGRES_DB: vapor_database
      ports:
        - '5432:5432'
  </code></pre>
  <p>And my testing.Dockerfile</p>
  <pre class="lang-yaml prettyprint-override"><code>FROM swift:5.5
  
  WORKDIR /package
  
  COPY . ./
  
  CMD [&quot;swift&quot;, &quot;test&quot;, &quot;--enable-test-discovery&quot;]
  </code></pre>