jbogard / Respawn

Intelligent database cleaner for integration tests
Apache License 2.0
2.67k stars 134 forks source link

PostgresDbAdapter reseed issues #95

Open tsanton opened 2 years ago

tsanton commented 2 years ago

Hi Jimmy, and again: great lib :)

There seems to be an issue with PostgresDbAdapter.BuildReseedSql

The issue boils down to a mismatch between the C# & PgSQL table name concatenation:

Here's the GetFullName:

public string GetFullName(char quoteIdentifier) =>
            Schema == null
                ? $"{quoteIdentifier}{Name}{quoteIdentifier}"
                : $"{quoteIdentifier}{Schema}{quoteIdentifier}.{quoteIdentifier}{Name}{quoteIdentifier}";
//for table {"schema":"public","table": "a"}
var tableNames = string.Join(", ", tablesToDelete.Select(t => $"'{t.GetFullName(QuoteCharacter)}'"));
//Given quote char " yielding:'"public"."a"'
--Given schema:table 'public:a'
//....
where AND '""' || table_schema || '"".""' || table_name || '""' IN ({tableNames})
--Here the where resolves to '"public"".""a"' IN ({tableNames})

To test it run the following:

create table public.a (id int generated always as identity(start with 1 increment by 1), val int);
create table public.b (id serial, val int);

select
    isc.table_schema,
    isc.table_name,
    '"' || isc.table_schema || '"".""' || isc.table_name || '"' wrong_but_currently_implemented_format,
    '"' || isc.table_schema || '"."' || isc.table_name || '"' suggested_fix
FROM information_schema.columns isc
where isc.table_schema in('public')

I'd be happy to PR the quote-fix if you'd like!

/Tobias

jbogard commented 2 years ago

Sure! A test would be great too.