Closed danicc097 closed 2 years ago
@danicc097 It's interesting :)
I think to actually use this feature user need to add VSCode setting for migration database file, in the migration directory.
Is this understanding, correct? The sample needs to be enhanced.
The migrationsFolder
setting may be a relative directory. I'm currently using it as migrationsFolder: "./db/migrations/"
in my .vscode/settings.json
or workspace settings. So in ./db/migrations
I can have a single schema.up.sql
or multiple XXXXXXXX_<migration_name>.up.sql
files that get executed in order
If you are writing development and migration in a single VSCode setting, you will only see one database.
In development we expect the database to have the tables predefined, but in migration we expect the database to be empty.
How would you reconcile the two?
We need to write two VSCode setting files.
As it is now, if you are opening a migration file it will be detected (right now by filename: document.uri.endsWith(file)
, but could be matching the complete migrationsFolder
setting to be 100% sure). <- needs testing
When opening any migration file inside migrationsFolder
execution of the migrations stops until the last migration, so you can work on the latest or even check previous migrations for correctness
When opening a query file all migrations are executed
The database schema for production and migration should be the same name or switched by the VSCode setting.
The migration file for your test case does not do table drop first.
If it is not an empty database (or schema), is it possible to edit the migration file?
I added more examples and tests, I hope it's clearer now
Offtopic: Is queryFileStaticAnalysis
used? It clutters the logs with errors but none are shown to the user, I'm not sure what it's supposed to do
I can now confirm locally that it works!!
Migration will be a big features by itself and should be configured as follows to allow for future upgrade.
{
"plpgsqlLanguageServer.migration": {
"migrationFolder": "migrations/migrations_test",
"upMigrationFilePattern": "*.up.pgsql",
"downMigrationFilePattern": "*.down.pgsql",
}
}
Just a note, for users with multiple RDBs, this tool uses ".pgsql" as the default extension.
You added new option "postMigrations", What is the need for this option?
The migration features is only performed when editing the migration file, so post migrations will always ROLLBACK.
I usually have some scripts that setup triggers, etc. which are generic and get called after all migrations run, so as to not rewrite them in each new migration. This flag is optional is by default
Currently migrations are run every time, not just when editing a migration file (the difference is that, when editing a migration file, execution of the migrations stop at the opened migration file so it is correctly analyzed).
If you want to work on queries with production data and not on a clean state from migrations, I can add an optional alwaysRunMigrations
bool option. With alwaysRunMigrations
undefined migrations will run only if we detect we are in the plpgsqlLanguageServer.migrations.folder
.
Hmm..., I don't think it is a good idea to implicitly update the database.
The way this tool currently update the database is only if user explicitly executes the "Execute the Current File Query" command.
The implicit way of updating the database is fine for those who understand it, but I think it will confuse newcomers to the project.
At this time I would like to remove the "postMigrations" feature from this PR.
I'm not proposing to update the database on analysis, that would be destructive and confusing for the user. In the end all this PR would do is setup a clean state for the analysis by dropping and recreating inside a transaction if the user opts into the migration
optional feature (then everything is rollbacked). Additionally, what I proposed is some extra configuration to allow for:
postMigrations
(optional parameter)migration
setting set) via something like alwaysRunMigrations
. If I understand correctly you would not like to have migrations run when you're editing a query, only when editing migrations.I could add a comment specifying the migrations feature does not commit any changes (which reminds me I also need to sanitize and remove all begin
, rollback
and commit
lines from migrations like it was done with statements)
I see.
It certainly doesn't seem to update the database.
I will merge this and release it after some refactoring.
I'm busy this week, so I will probably work on it next week.
What does this PR do?
Add an option to run migrations automatically in the same transaction if a folder is given.
Use case: while developing it's simpler to create a dedicated empty database that gets clean state and allows to develop both migrations itself and queries at the same time depending on what file is opened (will execute migrations up to N revision accordingly)
What issues does this PR fix or reference?
Is it tested? How?