If you already have a Database and would like to start using codd, here's a guideline to approach the problem. Remember to be very careful if/when making any changes to your Prod DB:
- Configure your environment variables as explained in the README and in CONFIGURATION.md.
- In that configuration make sure you have that extra
dev-only
folder to hold SQL migrations that will only run in developers' machines. - Run
pg_dump your_database > dump-migration.sql
locally. Do not usepg_dumpall
because it includes psql's meta-commands that codd doesn't support. - Run
dropdb your_database
to drop your DB locally. - Add a bootstrap migration similar to the one exemplified in BOOTSTRAPPING.md, but with ownership, encoding and locale equal to your Production DB's. The database's and the public's Schema ownership might need some manual intervention to match in different environments.
- What do we mean? Cloud services such as Amazon's RDS will create Schemas and DBs owned by users managed by them - such as the
rdsadmin
user -, that we don't usually replicate locally. We can either replicate these locally so we don't need to touch our Prod DB or change our Prod DB so only users managed by us are ever referenced in any environment.
- What do we mean? Cloud services such as Amazon's RDS will create Schemas and DBs owned by users managed by them - such as the
- Make sure the bootstrap migrations added in the previous step create the database, roles and ownership match what you get in Production.
- Use psql's
\dg
to view roles in your Prod DB. - Use psql's
\l
to check DB ownership and permissions of your Prod DB. - Use psql's
\dn+
to check the public schema's ownership and permissions in your Prod DB. Once your bootstrapping migration is ready, runcodd add bootstrap-migration.sql --dest-folder your-dev-only-folder
. This will create your database with no tables or data in it.
- Use psql's
- Run
codd add dump-migration.sql --dest-folder your-dev-only-folder
. Dumps can some times fail to be applied due to privileges being enforced by postgresql itself, so make sure to edit and change the dump file accordingly so that it can be applied. This often means adding a custom-- codd-connection
comment on top to make it run as a privileged enough user, like thepostgres
user. - You should now have your database back and managed through codd.
- Make sure your Production environment variable
CODD_MIGRATION_DIRS
does not contain yourdev-only
folder. Add any future SQL migrations to yourall-migrations
folder. - Before deploying with codd, we strongly recommend you run
codd verify-schema
with your environment variables connected to your Production database and make sure schemas match. - In Production, we strongly recommend running
codd up --lax-check
(the default) to start with until you get acquainted enough to consider strict-checking. Make sure you readcodd up --help
to better understand your options.