ALTER TABLE job ADD COLUMN create_ts TIMESTAMPTZ NOT NULL DEFAULT NOW(),
ADD COLUMN created_by UUID REFERENCES organisation_link(id);
UPDATE job
SET create_ts = update_ts,
created_by = updated_by;
ALTER TABLE job ALTER COLUMN created_by SET NOT NULL;
Assistant
To undo the changes introduced by the SQL script you've given, you need to perform actions that revert each change step by step, considering how they affect your database. Remember, performing operations like these can lead to data loss (particularly with column removal or alteration), so please ensure you've backed up any critical data before proceeding.
Here's a breakdown on how to reverse each step in your provided script:
Adding columns with a default value and a foreign key reference: Your script adds two columns, create_ts with a TIMESTAMPTZ type and a default value, and created_by with a UUID type, referencing an id from the organisation_link table.
Updating the values of these new columns to match others in the table: Specifically, setting create_ts to equal update_ts and created_by to equal updated_by.
Altering the created_by column to set it as NOT NULL: This ensures that every row must have a value for created_by.
To undo these changes, you should:
Step 1: Drop the Constraint (If Any)
If there's a NOT NULL constraint that was added, it is going to be automatically removed when the column is dropped. However, if the column is part of any other constraint (like a foreign key that references it), you'll need to first drop those constraints. Since created_by references another table, if any other tables reference created_by, those constraints should be removed.
Step 2: Remove the Added Columns
Since the operation added two columns, you would remove them. However, if you have data in these columns that you need to preserve somewhere, ensure you back it up or migrate it before deleting the columns.
sql
ALTER TABLE job DROP COLUMN create_ts,
DROP COLUMN created_by;
This command will remove both columns from the job table, effectively undoing the addition of these columns.
Important Considerations:
Data Loss: Removing columns will result in the permanent loss of data stored within them unless backed up or migrated elsewhere.
Dependencies: If other database objects depend on these columns (like views, stored procedures, or triggers), you might need to adjust or remove these objects as well.
Constraints and Indexes: If you added any indexes or additional constraints to these columns beyond what you've shown, those would need to be dropped as well.
Referential Integrity: Dropping the created_by column will remove the foreign key constraint but consider the impact on referential integrity within your database.
Step Rollback: Given the update action you performed, if any original data in the create_ts or created_by columns was overwritten, there's no way to retrieve it without a backup.
Finally, it's important to thoroughly test these rollback operations on a non-production version of your database to ensure they don't have unintended consequences. Additionally, always ensure you have a recent backup before making schema changes.