Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into storage-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuanwo committed Jan 3, 2025
2 parents 8c71fb1 + 22844ee commit 86e7b31
Show file tree
Hide file tree
Showing 211 changed files with 5,995 additions and 1,275 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/loco-gen-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ jobs:
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2

- name: Install seaorm cli
run: cargo install sea-orm-cli

- run: |
cargo install --path ../loco-new
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/loco-gen-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ jobs:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test

- name: docker deployment
run: cargo run -- generate deployment
run: cargo run -- generate deployment --kind docker
working-directory: ./examples/demo
env:
LOCO_DEPLOYMENT_KIND: docker
REDIS_URL: redis://localhost:${{job.services.redis.ports[6379]}}
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/loco-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ on:
- master
paths:
- "loco-new/**"
- "loco-gen/**"
pull_request:
paths:
- "loco-new/**"
- "loco-gen/**"

env:
RUST_TOOLCHAIN: stable
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

## Unreleased

* feat: smart migration generator. you can now generate migration based on naming them for creating a table, adding columns, references, join tables and more. [https://github.com/loco-rs/loco/pull/1086](https://github.com/loco-rs/loco/pull/1086)
* feat: `cargo loco routes` will now pretty-print routes
* fix: guard jwt error behind feature flag. [https://github.com/loco-rs/loco/pull/1032](https://github.com/loco-rs/loco/pull/1032)
* fix: logger file_appender not using the seperated format setting. [https://github.com/loco-rs/loco/pull/1036](https://github.com/loco-rs/loco/pull/1036)
* seed cli command. [https://github.com/loco-rs/loco/pull/1046](https://github.com/loco-rs/loco/pull/1046)
* Updated validator to 0.19. [https://github.com/loco-rs/loco/pull/993](https://github.com/loco-rs/loco/pull/993)
### Breaking Changes
Bump validator to 0.19 in your local `Cargo.toml`
* Testing helpers: simplified function calls + adding html selector. [https://github.com/loco-rs/loco/pull/1047](https://github.com/loco-rs/loco/pull/1047)
### Breaking Changes
#### Updated Import Paths
Expand All @@ -31,6 +34,13 @@
let boot = boot_test::<App>().await.unwrap();
```
* implement commands to manage background jobs. [https://github.com/loco-rs/loco/pull/1071](https://github.com/loco-rs/loco/pull/1071)
* magic link. [https://github.com/loco-rs/loco/pull/1085](https://github.com/loco-rs/loco/pull/1085)
* infer migration. [https://github.com/loco-rs/loco/pull/1086](https://github.com/loco-rs/loco/pull/1086)
* Remove unnecessary calls to 'register_tasks' functions in scheduler. [https://github.com/loco-rs/loco/pull/1100](https://github.com/loco-rs/loco/pull/1100)
* implement commands to manage background jobs. [https://github.com/loco-rs/loco/pull/1071](https://github.com/loco-rs/loco/pull/1071)
* expose hello_name for SMTP client config. [https://github.com/loco-rs/loco/pull/1057](https://github.com/loco-rs/loco/pull/1057)
* use reqwest with rustls rather than openssl. [https://github.com/loco-rs/loco/pull/1058](https://github.com/loco-rs/loco/pull/1058)
* more flexible config, take more values from ENV. [https://github.com/loco-rs/loco/pull/1058](https://github.com/loco-rs/loco/pull/1058)


## v0.13.2
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ backtrace_printer = { version = "1.3.0" }

# cli
clap = { version = "4.4.7", features = ["derive"], optional = true }
colored = "2"
colored = { workspace = true }
reqwest = { version = "0.12.7", features = [
"charset",
"http2",
Expand Down Expand Up @@ -152,10 +152,10 @@ ulid = { version = "1", optional = true }
rusty-sidekiq = { version = "0.11.0", default-features = false, optional = true }
bb8 = { version = "0.8.1", optional = true }

scraper = { version = "0.21.0", optional = true }
scraper = { version = "0.21.0", features = ["deterministic"], optional = true }

[workspace.dependencies]

colored = { version = "2" }
chrono = { version = "0.4", features = ["serde"] }
tracing = "0.1.40"
regex = "1"
Expand Down
6 changes: 3 additions & 3 deletions docs-site/content/docs/extras/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $ cargo loco routes
[POST] /api/auth/register
[POST] /api/auth/reset
[POST] /api/auth/verify
[GET] /api/user/current
[GET] /api/auth/current
.
.
.
Expand Down Expand Up @@ -144,7 +144,7 @@ curl --location '127.0.0.1:5150/api/auth/reset' \
This endpoint is protected by auth middleware.

```sh
curl --location --request GET '127.0.0.1:5150/api/user/current' \
curl --location --request GET '127.0.0.1:5150/api/auth/current' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer TOKEN'
```
Expand Down Expand Up @@ -196,7 +196,7 @@ $ cargo loco routes
[POST] /api/auth/register
[POST] /api/auth/reset
[POST] /api/auth/verify
[GET] /api/user/current
[GET] /api/auth/current
.
.
.
Expand Down
2 changes: 2 additions & 0 deletions docs-site/content/docs/getting-started/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ use loco_rs::task::Vars;
use crate::models::users;
pub struct UserReport;
#[async_trait]
impl Task for UserReport {
fn task(&self) -> TaskInfo {
Expand Down
105 changes: 74 additions & 31 deletions docs-site/content/docs/the-app/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ impl super::_entities::users::ActiveModel {

# Crafting models

## Migrations
## The model generator

To add a new model _you have to use a migration_.
To add a new model the model generator creates a migration, runs it, and then triggers an entities sync from your database schema which will hydrate and create your model entities.

```
$ cargo loco generate model posts title:string! content:text user:references
Expand Down Expand Up @@ -173,28 +173,32 @@ For schema data types, you can use the following mapping to understand the schem

Using `user:references` uses the special `references` type, which will create a relationship between a `post` and a `user`, adding a `user_id` reference field to the `posts` table.

Using `aproved_by:references:users` uses the special `references:<table>` type, which will create a relationship between a `post` and a `user`, adding a `aproved_by` reference field to the `posts` table.
Using `approved_by:references:users` uses the special `references:<table>` type, which will create a relationship between a `post` and a `user`, adding a `approved_by` reference field to the `posts` table.

You can generate an empty model:

```
$ cargo loco generate model posts
```

You can generate an empty model **migration only** which means migrations will not run automatically:

Or a data model, without any references:

```
$ cargo loco generate model --migration-only posts
$ cargo loco generate model posts title:string! content:text
```

Or a data model, without any references:
## Migrations

Other than using the model generator, you drive your schema by *creating migrations*.

```
$ cargo loco generate model posts title:string! content:text
$ cargo loco generate migration <name of migration> [name:type, name:type ...]
```

This creates a migration in the root of your project in `migration/`.
You can now apply it:

You can apply it:

```
$ cargo loco db migrate
Expand All @@ -210,6 +214,63 @@ Loco is a migration-first framework, similar to Rails. Which means that when you

This enforces _everything-as-code_, _reproducibility_ and _atomicity_, where no knowledge of the schema goes missing.

**Naming the migration is important**, the type of migration that is being generated is inferred from the migration name.

### Create a new table

* Name template: `Create___`
* Example: `CreatePosts`

```
$ cargo loco g migration CreatePosts title:string content:string
```

### Add columns

* Name template: `Add___To___`
* Example: `AddNameAndAgeToUsers` (the string `NameAndAge` does not matter, you specify columns individually, however `Users` does matter because this will be the name of the table)

```
$ cargo loco g migration AddNameAndAgeToUsers name:string age:int
```

### Remove columns

* Name template: `Remove___From___`
* Example: `RemoveNameAndAgeFromUsers` (same note exists as in _add columns_)

```
$ cargo logo g migration RemoveNameAndAgeFromUsers name:string age:int
```

### Add references

* Name template: `Add___RefTo___`
* Example: `AddUserRefToPosts` (`User` does not matter, as you specify one or many references individually, `Posts` does matter as it will be the table name in the migration)

```
$ cargo loco g migration AddUserRefToPosts user:references
```

### Create a join table

* Name template: `CreateJoinTable___And___` (supported between 2 tables)
* Example: `CreateJoinTableUsersAndGroups`

```
$ cargo loco g migration CreateJoinTableUsersAndGroups count:int
```

You can also add some state columns regarding the relationship (such as `count` here).

### Create an empty migration

Use any descriptive name for a migration that does not fall into one of the above patterns to create an empty migration.

```
$ cargo loco g migration FixUsersTable
```

### Down Migrations

If you realize that you made a mistake, you can always undo the migration. This will undo the changes made by the migration (assuming that you added the appropriate code for `down` in the migration).
Expand Down Expand Up @@ -247,27 +308,11 @@ $ cargo loco generate model movies long_title:string added_by:references:users d
* reference added_by is in singular, the referenced model is a model and is plural: `added_by:references:users`
* column name in snake case: `long_title:string`

### Naming migrations

There are no rules for how to name migrations, but here's a few guidelines to keep your migration stack readable as a list of files:

* `<table>` - create a table, plural, `movies`
* `add_<table>_<field>` - add a column, `add_users_email`
* `index_<table>_<field>` - add an index, `index_users_email`
* `alter_` - change a schema, `alter_users`
* `delete_<table>_<field>` - remove a column, `delete_users_email`
* `data_fix_` - fix some data, using entity queries or raw SQL, `data_fix_users_timezone_issue_315`

Example:

```sh
$ cargo loco generate migration add_users_email
```

### Migration Definition

### Add or remove a column

Adding a column:
**Add a column**

```rust
manager
Expand All @@ -280,7 +325,7 @@ Adding a column:
.await
```

Dropping a column:
**Drop a column**

```rust
manager
Expand All @@ -293,8 +338,7 @@ Dropping a column:
.await
```

### Add index

**Add index**

You can copy some of this code for adding an index

Expand All @@ -310,8 +354,7 @@ You can copy some of this code for adding an index
.await;
```

### Create a data fix

**Create a data fix**

Creating a data fix in a migration is easy - just use SQL statements as you like:

Expand Down
2 changes: 1 addition & 1 deletion docs-site/translations/tour-fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Dans votre application côté client, vous enregistrez ce jeton JWT et effectuez
Ce point de terminaison est protégé par un middleware d'authentification. Nous utiliserons le jeton que nous avons obtenu précédemment pour effectuer une requête avec la technique _bearer token_ (remplacez `TOKEN` par le jeton JWT que vous avez obtenu précédemment):

```sh
$ curl --location --request GET '127.0.0.1:5150/api/user/current' \
$ curl --location --request GET '127.0.0.1:5150/api/auth/current' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer TOKEN'
```
Expand Down
Loading

0 comments on commit 86e7b31

Please sign in to comment.