Forcing a failed mvn flyway:migrate to skip failed schema migration, and try executing next schema - postgresql

I was trying to run flyway:migrate on my projects postgres database. I have made the changes to a table manually and because of that the schema migration using flyway is failing, which is blocking next schema migration execution.
table : foo
required_change : ALTER TABLE foo ALTER COLUMN id DROP NOT NULL
current_schema_version : 2
next_schema_version : 3
Error:
[ERROR] com.googlecode.flyway.core.api.FlywayException: Migration of schema "public" to version 3 failed! Changes successfully rolled back.
How could I skip failing schema and make flyway:migrate execute next schema defined?

It might be simplest to undo the manual change so that Flyway can run successfully. For example, if you dropped the column then add it back in, then run the Flyway script to drop it.

So I found one possible solution to this problem, which goes as follows:
(1). In mysql database there is a table schema_version, which maintains the migration version number , it's status and other related information.
for ex.
mysql> desc schema_version;
version_rank
installed_rank
version
description
type
script
checksum
installed_by
installed_on
execution_time
success
for a failed migration the success field values stores 0, to override that and execute the next flyway:migration you can manually set the value to 1 (this would make sure, you don't lose the data stored on the table you created manually , when migration failed.
(2). Adding following on your pom.xml while running flyway:migration temporarily (while testing) also helps.
<validateOnMigrate>false</validateOnMigrate>

Related

Prisma db push error when removing column that has a default value

After removing a column from schema.prisma, and running npm prisma db push, I'm getting the error...
Error: The object 'users_role_df' is dependent on column 'role'.
I'm using Prisma 3.3.0 with the sqlserver connection.
The model looks like this...
model User {
id ....
name ...
role String #default("USER")
}
I see the initial push created the CONSTRAINT users_role_df but now when I remove it from the model, and run push, it's not handling removing the constraint first and then the column.
How can I fix this?
You could try running the command npx prisma migrate dev --create-only to see what the generated SQL looks like for the migration.
You could manually add a DROP CONSTRAINT users_role_df; inside the generated migration or change the order of the SQL commands (if such a command already exists in the generated migration).
It is fine for you to make changes to the migration file as long as you do it before applying the migration to your database.

TypeORM migration entries lost from DB, `migration:run` re-runs them, then fails with "relation already exists"

I have a NestJS app with TypeORM, dockerized. I have synchronize turned off, using migrations instead. In the container entry point, I do yarn typeorm migration:run. It works well the first time around, and according to the logs it inserts records into the migrations table.
I noticed that when I start the project the next time it often tries to re-run migrations and fails (as expected) due to "relation already exists". At this point I can verify that entries are indeed missing from the migrations table via docker-compose exec db psql -U postgres -c 'SELECT * FROM "migrations" "migrations". The DB schema is up to date. When I insert a new record manually it gets an incremental ID after the missing records. So the records were there at some point.
I can't figure out what might cause entries in the migrations table to disappear (be rolled back?). This happens on the project linked above. It's a straightforward example project. I don't have an entity accidentally named "migrations". :)
As a workaround I currently insert into the migrations table manually:
docker-compose exec db psql -U postgres -c "INSERT INTO migrations (timestamp, name) VALUES ('1619623728180', 'AddTable1619623728180');"
Running specs that synchronized the DB was the issue.
I had a .env.test to use a different DB, but as it turns out that is not supported by dotenv. There are a few ways to make it work. I chose dotenv-flow/config and added it to my test script:
jest --collect-coverage --setupFiles dotenv-flow/config

PG::DuplicateTable: ERROR: relation "boards" already exists

I created a model in no2 branch, then I find some error and decided to delete this branch and start a new one.
But when I rewrote this model and db:migrate , it told me that this table already exist.
I tried db:rollback but didn't work, and the migrate:status showed that:
and my schema.rb is empty
I dropped my db & re db:migrate.
Use sql script, to remove a record from schema_migrations table where version is 20191215065743.
Further, if the boards table exist, drop it using psql command.

Flyway: When is repair required for a PostgreSQL database?

It looks like when a run a migration which fails, that migration is not added to the schema.version table but is reflected locally as pending state in the info command.
Given this, repair is not required as there is no checksum stored in schema.version.
So I suppose my question is; is there a scenario where repair is required for PostgreSQL and also, what scenario puts a row into schema_version that has a non-TRUE value for success?
You are correct. For PostgreSQL and other databases with DDL transaction support, success is always true.
The only time repair is required, is when for some reason you had to change a migration and the checksums need to be realigned.

Tables are not executed after validating and updating schema

I have created entities in zend framework 2 using doctrine 2. After that I used this command to validate current schema.
./vendor/bin/doctrine-module orm:validate-schema
I got output like:
Mapping] OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.
Then I executed update command,
./vendor/bin/doctrine-module orm:schema-tool:update --force
The output for that is like:
Database schema updated successfully! "7" queries were executed
But, the problem is, There is no tables created in my database. What's wrong with this?
I used to run doctrine-module orm:validate-schema and then doctrine-module orm:schema-tool:create.
Here is good project to try:
Fmi-example on github