Respawn Documentation

repository·main·Indexed 25 days ago

https://github.com/jbogard/respawn

A utility for resetting test databases to a clean state by deleting data based on foreign key relationships. Supports SQL Server, PostgreSQL, MySQL, Oracle, and Informix via RespawnerOptions and the ResetAsync method.

Tokens
677
Snippets
4
Records
5
Agent score
34%

What's inside Respawn

  1. Set up local development dependencies with Docker

    main

    If your tests require local PostgreSQL and MySQL instances, you can use Docker Compose. From the solution root, run the following command to pull images and start the containers:

    docker-compose up -d
  2. Configure Schemas and DbAdapter in RespawnerOptions

    main

    When working with databases other than SQL Server, you can specify SchemasToInclude and optionally provide a DbAdapter. While the adapter is often inferred from the connection, you can explicitly set it for SQL Server, PostgreSQL, MySQL, Oracle, and Informix.

    var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
    {
        SchemasToInclude = new []
        {
            "public"
        },
        DbAdapter = DbAdapter.Postgres // 👈 optional, inferred from the connection
    });
  3. Reset the database using ResetAsync

    main

    In your test fixtures, call ResetAsync to return the database to a clean state. For SQL Server, you can pass a connection string name. For other databases (like PostgreSQL), pass an open DbConnection.

    // Using a connection string name (SQL Server)
    await respawner.ResetAsync("MyConnectionStringName");
    
    // Using an open DbConnection (Other databases)
    using (var conn = new NpgsqlConnection("ConnectionString"))
    {
        await conn.OpenAsync();
        await respawner.ResetAsync(conn);
    }
  4. Initialize a Respawner with RespawnerOptions

    main

    To use Respawn, create a Respawner instance using the Respawner.CreateAsync factory method. You can configure which tables to ignore or which schemas to include/exclude using RespawnerOptions.

    var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
    {
        TablesToIgnore = new Table[]
        {
            "sysdiagrams",
            "tblUser",
            "tblObjectType",
            new Table("MyOtherSchema", "MyOtherTable")
        },
        SchemasToExclude = new []
        {
            "RoundhousE"
        }
    });