bikeshedder / tusker

PostgreSQL migration management tool
The Unlicense
213 stars 17 forks source link

Diff from database fails with `FATAL: database "tusker" does not exist` #32

Closed jcerjak closed 9 months ago

jcerjak commented 10 months ago

Steps to reproduce the issue:

  1. Install tusker
  2. Copy tusker.toml.example to tusker.toml and specify database connection string (url) in [database] section
  3. Run tusker diff database
  4. Expected: diff is displayed. Actual: error FATAL: database "tusker" does not exist is displayed

Shouldn't matter, but note that I installed by cloning the repo from Github (commit: 166cd9d79544b699604bafe5c0781be41ca7b348), replaced psycopg2 with psycopg2-binary in pyproject.toml and ran poetry install.

jcerjak commented 10 months ago

If I comment out dbname in tusker.toml, then it works. I'm reading https://github.com/bikeshedder/tusker?tab=readme-ov-file#what-does-the-dbname-setting-in-tuskertoml-mean and I don't fully understand when dbname should be defined.

bikeshedder commented 10 months ago

tusker diff database does diff the schema against the database specified in the [database] section. As you haven't created that database you get that error.

This command is meant to be run after you have migrated your database.

Quoting from the README:

Is it possible to diff the schema and/or migrations against an existing database?

Yes. This feature has been added in 0.2. You can pass a from and to argument to the tusker diff command. Check the output of `tusker diff --help for more details.

(...)

What does the dbname setting in tusker.toml mean?

When diffing against a ready migrated database this database name is used. This command will print out the difference between the current database schema and the target schema:

Normally you will just run tusker diff and use the created migrations in a migration management tool of your choice. tusker diff database is for checking if the migrations were applied correctly.

bikeshedder commented 10 months ago

If I comment out dbname in tusker.toml, then it works. I'm reading https://github.com/bikeshedder/tusker?tab=readme-ov-file#what-does-the-dbname-setting-in-tuskertoml-mean and I don't fully understand when dbname should be defined.

If you do not specify a dbname it will use the default database which defaults to your current user name. If that database exists you don't get an error. Still it's probably not what you want as explained in my previous answer.

jcerjak commented 10 months ago

tusker diff database does diff the schema against the database specified in the [database] section. As you haven't created that database you get that error.

Based on the README I was somehow under the impression that dbname is an internal database tusker uses when doing the diffs, not my main database. Maybe the README can be clarified a bit? And/or using dbname = "<your db name here>" in tusker.toml.example would also help.

This command is meant to be run after you have migrated your database.

I was first trying to export the schema of the existing database, looks like tusker diff database --reverse does the trick.

tusker diff database is for checking if the migrations were applied correctly.

I was thinking of also doing roundtrip development. For example, do some schema changes in a GUI tool, then export those changes to git repo for versioning etc. Do you see some potential issues with this approach?

jcerjak commented 10 months ago

Otherwise feel free to close this issue, apart from potential documentation improvements, the issue is resolved.

bikeshedder commented 10 months ago

For dumping the initial schema from an existing database you should rather be using pg_dump --schema-only.

If you want to edit your schema inside the database you could use pg_dump --schema-only > schema.sql to update the schema file and then use tusker diff to create the migrations.

Personally I prefer editing my schema in files so I can organize them, add comments, use code generators, etc.

If you're editing the database already in a tool maybe that tool already has some kind of diffing mechanism built-in? pgadmin4 does have built-in diffing and is said to be much better than what migra (the library tusker uses) provides.

The README states that the dbname is also used when creating the temporary databases:

Tusker also needs to create temporary databases when diffing against the schema and/or migrations. The two databases are called {dbname}_{timestamp}_schema and {dbname}_{timestamp}_migrations.

jcerjak commented 10 months ago

Thanks for the info @bikeshedder . I've created a PR to include some of this information in the README: https://github.com/bikeshedder/tusker/pull/33

bikeshedder commented 9 months ago

I just merged the PR.