-
Notifications
You must be signed in to change notification settings - Fork 364
CAPI Migration Style Guide
Everything that is written for Cloud Controller needs to work for postgres and mysql.
If anything goes wrong during a migration in mysql the db will not rollback. Here is an example of a dangerous migration. If this migration blew up in the middle of running on mysql the db would have to be rolled back by hand. This is because mysql does not support DDL transactions.
module VCAP::CloudController
class Dog < Sequel::Model
end
end
Sequel.migration do
up do
## NOT in transaction
alter_table :dogs do
add_column :testing, String
end
## Each update is its own transaction
VCAP::CloudController::Dog.each_with_index do |dog, i|
dog.update(guid: "c"*(i+1))
end
## ONE transaction for all updates
transaction do
VCAP::CloudController::Dog.each_with_index do |dog, i|
dog.update(guid: "d"*(i+1))
end
end
end
end
In the below example, NOTHING will rollback if the database is mysql. The DDL action will cancel out any effects of a transaction.
module VCAP::CloudController
class Dog < Sequel::Model
end
end
Sequel.migration do
up do
transaction do
# This looks like it is in a transaction, IT IS NOT
# When this migration errors nothing will be rolled back
# The guids will remain updated and the column `testing` will still exist
VCAP::CloudController::Dog.each_with_index do |dog, i|
dog.update(guid: "a"*(i+1))
end
alter_table :dogs do
add_column :testing, String
end
raise "wut"
end
end
end
- Separate data changing migrations from schema changing migrations (DDL statements).
- Explicitly wrap data changes in a transaction
- Avoiding big schema changes in a single migration since if one fails, the whole migration needs to be rolled back manually. Minute migration files are easier to roll back.
If you don't, mysql and postgres will default to different constraint names, this makes it more difficult to edit the constraints in the future.
If you write a uniqueness constraint where any of the values can be null
, remember that null != null
.
For example the values [1, 1, null]
and [1, 1, null]
are unique.
Sorry.
If you must do different things for mysql and postgres you can check the type with the following code.
Sequel::Model.db.adapter_scheme == :postgres
This means that all migrations must be backwards compatible (with some exceptions).
-
Pipelines
-
Contributing
- Tips and Tricks
- Cloud Controller API v3 Style Guide
- Playbooks
- Development configuration
- Testing
-
Architectural Details
-
CC Resources
- Apps
- Audit Events
- Deployments
- Labels
- Services
- Sidecars
-
Dependencies
-
Troubleshooting
- Ruby Console Script to Find Fields that Cannot Be Decrypted
- Logging database queries in unit tests
- Inspecting blobstore cc resources and cc packages(webdav)
- How to Use USR1 Trap for Diagnostics
- How to Perf: Finding and Fixing Bottlenecks
- How to get access to mysql database
- How To Get a Ruby Heap Dumps & GC Stats from CC
- How to curl v4 internal endpoints with mtls
- How to access Bosh Director console and restore an outdated Cloud Config
- Analyzing Cloud Controller's NGINX logs using the toplogs script
-
k8s
-
Archive