ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.
https://www.ardanlabs.com
Apache License 2.0
3.4k stars 612 forks source link

Why, when we want to edit a field or change its type, the changes are not applied in the database with the migration command. #382

Closed mrbardia72 closed 2 weeks ago

mrbardia72 commented 2 weeks ago

Why, when we want to edit a field or change its type, the changes are not applied in the database with the migration command.

ardan-bkennedy commented 2 weeks ago

I need more information. You will need to add a new version entry to the migration document. You can't edit a version that has been applied. Is this what you mean?

mrbardia72 commented 2 weeks ago

Yes, that's what I mean We have two cases: 1- Adding a field to the table 2- Changing the type of that field If possible, write me an example here

mrbardia72 commented 2 weeks ago

You said here You will need to add a new version entry to the migration document. For example, if I want to edit a field, how should I do this? For example, I want to change the name of a field or its type @ardan-bkennedy

himynamej commented 2 weeks ago

Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again.you must add new version of the migration or rollback the old migration.

mrbardia72 commented 2 weeks ago

How should I do what you said? Do you have a test sample?

After that, this rollback method does not cause data loss

@himynamej

ardan-bkennedy commented 2 weeks ago

You would use an ALTER COLUMN command in a new version of the migration. Here is a good example

-- Version: 1.19
-- Description: Alter payments table.
ALTER TABLE payments ADD COLUMN gateway_status INT NULL;
ALTER TABLE payments ADD COLUMN gateway_description TEXT NULL;
ALTER TABLE payments ADD COLUMN failed BOOL NULL;
UPDATE payments SET failed = false WHERE status != 'failed';
UPDATE payments SET failed = true WHERE status = 'failed';
ALTER TABLE payments ALTER COLUMN failed SET NOT NULL;
mrbardia72 commented 2 weeks ago

tnx