Plm migration (#38)

Plm migration
stable
Baptiste Gelez 5 years ago committed by GitHub
commit 8ba20275f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -102,3 +102,35 @@ plm search unlock -p Plume
**Arguments:**
- `--path`, `-n`: path to plume working directory.
## `plm migration`
Manage migrations.
### `plm migration run`
Run migrations.
**Example:**
```bash
plm migration run -p Plume
```
**Arguments:**
- `--path`, `-p`: path to Plume working directory.
### `plm migration redo`
Rerun latest migration.
**Example:**
```bash
plm migration redo -p Plume
```
**Arguments:**
- `--path`, `-p`: path to Plume working directory.

@ -17,6 +17,47 @@ emails during development. You can do it by passing the `--feature debug-mailer`
flags to `cargo`. When enabled, mails will be logged to the standard output instead
of being sent for real.
## Migrations
Migrations are files than can be used to update the database schema (for instance to add a new field to a model).
To create new migrations you will need a tool called `diesel`, that can be installed with:
```bash
cargo install diesel_cli --no-default-features --features DATABASE --version '=1.3.0'
```
After that to create a migration, both for PostgreSQL and SQlite, you need to run these two commands:
```
MIGRATION_DIRECTORY=migrations/postgres diesel migration generate NAME
MIGRATION_DIRECTORY=migrations/sqlite diesel migration generate NAME
```
Where `NAME` is the name you want to give to your migration, in one "word", for instance `add_role_to_users`.
New files will be generated in `migrations/postgres` and `migrations/sqlite`, called `up.sql` and `down.sql`.
The former should run the actual migration, and the later undo it.
You can also run some Rust code in migrations, by writing it in comments starting with `#!`, and wrapped in a closure taking
a database connection and a path to the current directory. You can access the `plume-models` modules with the `super` module.
Here is an example:
```sql
--#!|conn: &Connection, path: &Path| {
--#! println!("Running a migration from {}", path);
--#! println!("The admin of this instance is @{}", Instance::get_local(conn).unwrap().main_admin(conn).unwrap().name());
--#! Ok(())
--#!}
```
If your function is too long, you can also put it in `plume-models`, and simply give it's full identifier in the comment:
```sql
--#! crate::migrations::functions::my_migration_function
```
To run migrations, you can use `plm migration run`. To cancel and re-run them, use `plm migration redo`.
## Testing the federation
To test the federation, you'll need to setup another database,
@ -29,7 +70,7 @@ times. Then create a copy of your `.env` file in another directory, and change t
and `ROCKET_PORT` variables. Then copy the migration files in this new directory and run them.
```
diesel migration run
plm migration run
```
Setup the new instance with `plm` [as explained here](/installation/config).

@ -51,8 +51,3 @@ PLUME_LOGO_192=icons/trwnh/paragraphs/plumeParagraphs192.png
PLUME_LOGO_256=icons/trwnh/paragraphs/plumeParagraphs256.png
PLUME_LOGO_512=icons/trwnh/paragraphs/plumeParagraphs512.png
```
## Diesel
Diesel, the tool we use to run migrations may be configured with the `DATABASE_URL` which should contain the URL of the
PostgreSQL database, or the path to the SQLite file. Otherwise, you can specify `--database-url YOUR-URL` everytime you run a `diesel` command.

@ -43,11 +43,10 @@ For more information about what you can put in your `.env`,
see [the documentation about environment variables](/environment).
Now we need to run migrations. Migrations are scripts used to update
the database. They are run by `diesel` that we previously installed.
To run the migrations, you can do:
the database. To run the migrations, you can do:
```bash
diesel migration run
plm migration run
```
Migrations should be run after each update. When in doubt, run them.

@ -40,7 +40,8 @@ docker-compose up -d postgres
# Create the database role (it will ask you for the password)
docker-compose run --rm postgres su postgres -c createuser plume -dP
# Setup the database (create it and run migrations)
docker-compose run --rm plume diesel database setup
docker-compose run --rm postgres su postgres -c createdb plume -O plume
docker-compose run --rm plume plm database setup
# Setup your instance
docker-compose run --rm plume plm instance new

@ -13,15 +13,12 @@ Then, you'll need to install Plume and the CLI tools to manage your instance.
Run the following commands.
```bash
# Install diesel, a tool to manage your database
# Replace DATABASE with either postgres or sqlite depending on what you want to use
cargo install diesel_cli --no-default-features --features DATABASE --version '=1.3.0'
# Build the front-end
cargo install cargo-web
cargo web deploy -p plume-front
# Build the back-end, replacing DATABASE with your choice from installing diesel
# Build the back-end, replacing DATABASE either with
# postgres or sqlite depending on what you want to use
cargo install --no-default-features --features DATABASE
# Build plm, the CLI helper, replacing DATABASE again

@ -14,7 +14,7 @@ cargo web deploy -p plume-front
cargo install --force --no-default-features --features DATABASE && cargo install --path plume-cli --force --features DATABASE
# Run the migrations
diesel migration run
plm migration run
# If you are using sysvinit
sudo service plume restart

Loading…
Cancel
Save