zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
644 stars 70 forks source link

Load extensions after DB reset #477

Closed KDederichs closed 1 year ago

KDederichs commented 1 year ago

Hey again,

so after figuring out why https://github.com/zenstruck/foundry/issues/472 does what it does it's kinda a foundry issue still, sorry :D

What happens is when it's dropping the Database and recreating it it'll also reset the enabled extensions since those on a per database bases it seems.

Now here's the question: Is there a way to enable those extensions on database reset again? I saw you can run your own migrations every time but honestly, I'd love to avoid that.

If there's no convenient way would you be open if I made a PR for something like this?:

    database_resetter:

        # Config related to ORM
        orm:

            # Enables extensions after database reset
            load_postgres_extensions_after_reset: ['postgis']

That way users could just specify if they want something some extensions enabled after.

Alternatively there'd also be the option of maybe adding something like this to the DatabaseResetter:

     private static array $sqlAfterReset = [];
    public static function addSqlAfterResetStatement(string $statement): bool
    {
        return self::sqlAfterReset[] = $statement;
    }

    public static function getSqlAfterResetStatments(): array
    {
        return self::sqlAfterReset;
    }

and then in the ORMDatabaseResetter's dropAndResetDatabase:

          foreach ( DatabaseResetter::getSqlAfterResetStatments() as $statement) {
            $this->runCommand(
                $this->application,
                'doctrine:query:sql',
                [
                    $statement,
                    '--connection' => $connection,
                ]
            );
}

Which would offer a bit more flexibility? Edit: Might be a bit harder though since I think that one's executed before the setup methods?

nikophil commented 1 year ago

a foundry issue still, sorry :D

😂🫢

Maybe you can leverage the global state and use invokable service to run the needed sql query to enable the extension?

Might be a bit harder though since I think that one's executed before the setup methods?

The db is reset before each test so, adding the extension will be needed on each test But if you use dama, it will be ran only once

KDederichs commented 1 year ago

Stories doesn't seem to work, something is blocking the Create Extension command when executed from those. (PG throwing FATAL: terminating connection due to administrator command)

            $this->runCommand(
                $this->application,
                'doctrine:query:sql',
                [
                    'sql' => 'CREATE EXTENSION IF NOT EXISTS postgis;',
                    '--connection' => $connection,
                ]
            );

after

            $this->runCommand(
                $this->application,
                'doctrine:database:create',
                [
                    '--connection' => $connection,
                ]
            );

works though

kbond commented 1 year ago

@KDederichs, could you switch to using migrations instead of schema (zenstruck_foundry.database_resetter.orm.reset_mode: migrate)? Then you can add this requirement to your migration's sql?

KDederichs commented 1 year ago

@kbond That just gives me this Symfony\Component\DependencyInjection\Exception\LogicException: Container extension "database_resetter" is not registered. in /srv/app/vendor/symfony/dependency-injection/ContainerBuilder.php:229 anything else that needs to be changed for that to work?

kbond commented 1 year ago

To clarify, do you have migrations for your project?

anything else that needs to be changed for that to work?

Sorry, I should have been more explicit, in config/packages/zenstruck_foundry.yaml:

zenstruck_foundry:
    database_resetter:
        orm:
            reset_mode: migrate

Here's the docs on the topic: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#database-reset (bottom of this section)

KDederichs commented 1 year ago

Yeah I do have migrations for my project and I did add that config section, which then resulted in the aforementioned error. That's why I'm asking if there's something else that needs to be done :)

Edit: What scratch that, I just remembered I got that setup where it loads tests in prod mode to speed them up (either from here or the DAMA bundle), of course it doesn't load that in prod mode then... I'm dumb.

Works now