migra/docs/quickstart.md

88 lines
3.7 KiB
Markdown
Raw Permalink Normal View History

2020-04-14 14:46:10 +03:00
# Quickstart
## Installation
2017-08-20 06:25:05 +03:00
`migra` is written in Python so you need to install it with `pip`, the Python Package Manager (don't worry, you don't need to know or use any Python to use the `migra` command).
1. Make sure you have [pip](https://pip.pypa.io/en/stable/installing/) properly installed.
2. Run:
2020-04-14 14:46:10 +03:00
pip install migra[pg]
2017-08-20 06:25:05 +03:00
This will install the latest version of migra from PyPI (the global Python Package Index), along with psycopg2, the python PostgreSQL driver.
2020-04-14 14:46:10 +03:00
Alternatively, if you don't want to install Python, you can run it from a self-contained Docker image by first running:
2020-04-23 04:23:54 +03:00
:::shell
2020-04-14 14:46:10 +03:00
docker pull djrobstep/migra
2020-04-14 14:46:10 +03:00
then creating a short alias to it with:
2020-04-23 04:23:54 +03:00
:::shell
2020-04-14 14:46:10 +03:00
alias migra="docker run djrobstep/migra migra"
2017-08-20 06:25:05 +03:00
3. Confirm migra is installed by running `migra --help`. The output should begin like this:
usage: migra [-h] [--unsafe] dburl_from dburl_target
## Comparing two database schemas
To compare two database schemas:
2020-04-14 14:46:10 +03:00
migra [url_of_database_A] [url_of_database_B]
2017-08-20 06:25:05 +03:00
For example, we have two databases, named "alpha" and "beta". We can compare them using this command:
2020-04-14 14:46:10 +03:00
:::shell
migra postgresql:///alpha postgresql:///beta
2017-08-20 06:25:05 +03:00
Migra will then generate whatever SQL is required to change the schema of database `alpha` to match database `beta`.
If the two database schemas match exactly, you'll get empty output, because no changes are required. This functions like the well-known [diff command](https://en.wikipedia.org/wiki/Diff_utility), which also returns empty output when comparing two identical files.
### Warning
Don't blindly copy-and-paste the output of the `migra` command.
`migra` features a safeguard against generation of dangerous statements. If the command generates a drop statement, `migra` will exit with an error. If you're sure you want the drop statement(s), you can turn off this safeguard behaviour with the `--unsafe` flag:
2020-04-14 14:46:10 +03:00
:::shell
2017-08-20 06:25:05 +03:00
migra --unsafe postgresql:///alpha postgresql:///beta
## Making changes to database schemas
### Suggestion
If you're making changes to a serious production database, use a copy of it for these steps instead so you're not changing your production environment until you intend to.
You can make a schema-only dump of your PostgreSQL database with the following command:
2020-04-14 14:46:10 +03:00
:::shell
pg_dump --no-owner --no-privileges --schema-only name_of_database -f schema.dump.sql
2017-08-20 06:25:05 +03:00
### Steps
2020-04-14 14:46:10 +03:00
#### Connect
Get the connection string of the database you want to make changes to. `migra` needs to connect to this database so it can analyse the database's schema.
2017-08-20 06:25:05 +03:00
2. Prepare a second PostgreSQL database. This database needs to have the new/desired/target schema. You might create a temporary database and set it up for this purpose.
3. Generate a migration script using the following command (substituting your own connection strings):
2020-04-14 14:46:10 +03:00
:::shell
migra --unsafe postgresql:///existing postgresql:///database_with_new_schema > migration_script.sql
2017-08-20 06:25:05 +03:00
4. Carefully review the migration script in `migration_script.sql`
Consider in particular:
- The generated script may result in data loss from your database when you apply this script. Consider if you intend for this to happen or if you need to add statements to copy data out of the relevant tables/columns before you drop them forever.
2020-04-14 14:46:10 +03:00
- Some migration operations can take a long time and cause interruptions and downtime, particularly when involving tables containing large amounts of data..
2017-08-20 06:25:05 +03:00
5. Apply `migration_script.sql` to your production database with a command similar to the following (again substituting your own connection string).
2020-04-14 14:46:10 +03:00
:::shell
psql postgresql://production -1 -f migration_script.sql