Skip to content

Commit 7b3abba

Browse files
authored
Fix papercuts (#343)
* add log_format option to rfd-processor * remove custom deserialization code * fix extraneous diff when generating schema.rs * remove invalid diesel.toml field * update setup instructions
1 parent 94ad9c8 commit 7b3abba

File tree

8 files changed

+87
-54
lines changed

8 files changed

+87
-54
lines changed

SETUP.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,34 @@ emitted at:
3939
### Installation
4040

4141
Once all of the dependencies have been installed, database migrations will need to be run to prepare
42-
the database tables. These can be run using the [`diesel` cli ](https://diesel.rs/guides/getting-started).
43-
44-
To run them:
42+
the database tables. These can be run using the `rfd-installer` tool:
4543

4644
```sh
4745
cd rfd-model
46+
V_ONLY=1 DATABASE_URL=<database-url> cargo run -p rfd-installer
4847
DATABASE_URL=<database-url> diesel migration run
4948
```
5049

5150
Replace `<database-url>` with the url to the Postgres instance that the API and processor will be
5251
configured to run against.
5352

53+
### Running database migrations
54+
55+
After the initial migration described above, any future database migration can
56+
be executed with the following commands:
57+
58+
```sh
59+
cd rfd-model
60+
DATABASE_URL=<database-url> diesel migration run
61+
```
62+
63+
> [!NOTE]
64+
>
65+
> If the generated `schema.rs` includes additional tables in its diff, it means
66+
> v-api added more tables of its own. You should exclude them in
67+
> `rfd-model/diesel.toml` and re-run migrations. The extraneous tables should
68+
> then disappear from `schema.rs`.
69+
5470
### Configuration
5571

5672
Each part (the api and processor) has its own configuration file, and the respective application
@@ -90,4 +106,4 @@ cargo run -p rfd-cli --features local-dev
90106

91107
The processor has multiple jobs that are able to be run, and configuration is only required for
92108
jobs that are going to be run. The `actions` key defines the jobs that should be run. By default
93-
all jobs are disabled. In this this mode the processor will only construct a database of RFDs.
109+
all jobs are disabled. In this this mode the processor will only construct a database of RFDs.

rfd-api/src/config.rs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ use std::{collections::HashMap, path::PathBuf};
66

77
use config::{Config, ConfigError, Environment, File};
88
use rfd_data::content::RfdTemplate;
9-
use serde::{
10-
de::{self, Visitor},
11-
Deserialize, Deserializer,
12-
};
13-
use thiserror::Error;
9+
use serde::Deserialize;
1410
use v_api::config::{AsymmetricKey, AuthnProviders, JwtConfig};
1511
use v_model::schema_ext::MagicLinkMedium;
1612

1713
use crate::server::SpecConfig;
1814

19-
#[derive(Debug, Error)]
20-
pub enum AppConfigError {
21-
#[error("Encountered invalid log format.")]
22-
InvalidLogFormatVariant,
23-
}
24-
2515
#[derive(Debug, Deserialize)]
2616
pub struct AppConfig {
2717
pub log_format: ServerLogFormat,
@@ -40,42 +30,13 @@ pub struct AppConfig {
4030
pub services: ServicesConfig,
4131
}
4232

43-
#[derive(Debug)]
33+
#[derive(Debug, Deserialize)]
34+
#[serde(rename_all = "kebab-case")]
4435
pub enum ServerLogFormat {
4536
Json,
4637
Pretty,
4738
}
4839

49-
impl<'de> Deserialize<'de> for ServerLogFormat {
50-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
51-
where
52-
D: Deserializer<'de>,
53-
{
54-
struct ExternalId;
55-
56-
impl<'de> Visitor<'de> for ExternalId {
57-
type Value = ServerLogFormat;
58-
59-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
60-
formatter.write_str("string")
61-
}
62-
63-
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
64-
where
65-
E: de::Error,
66-
{
67-
match value {
68-
"json" => Ok(Self::Value::Json),
69-
"pretty" => Ok(Self::Value::Pretty),
70-
_ => Err(de::Error::custom(AppConfigError::InvalidLogFormatVariant)),
71-
}
72-
}
73-
}
74-
75-
deserializer.deserialize_any(ExternalId)
76-
}
77-
}
78-
7940
#[derive(Debug, Default, Deserialize)]
8041
pub struct SearchConfig {
8142
pub host: String,

rfd-model/diesel-schema.patch

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
diff --git a/src/schema.rs b/src/schema.rs
5+
index 8eb4b49..cbc1526 100644
6+
--- a/src/schema.rs
7+
+++ b/src/schema.rs
8+
@@ -1,4 +1,6 @@
9+
-// @generated automatically by Diesel CLI.
10+
+// This Source Code Form is subject to the terms of the Mozilla Public
11+
+// License, v. 2.0. If a copy of the MPL was not distributed with this
12+
+// file, You can obtain one at https://mozilla.org/MPL/2.0/.
13+
14+
pub mod sql_types {
15+
#[derive(diesel::sql_types::SqlType)]

rfd-model/diesel.toml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33

44
[print_schema]
55
file = "src/schema.rs"
6+
patch_file = "diesel-schema.patch"
67

7-
[migrations_directory]
8-
dir = "migrations"
8+
[print_schema.filter]
9+
except_tables = [
10+
"access_groups",
11+
"api_key",
12+
"api_user",
13+
"api_user_access_token",
14+
"api_user_contact_email",
15+
"api_user_provider",
16+
"link_request",
17+
"login_attempt",
18+
"magic_link_attempt",
19+
"magic_link_client",
20+
"magic_link_client_redirect_uri",
21+
"magic_link_client_secret",
22+
"mapper",
23+
"oauth_client",
24+
"oauth_client_redirect_uri",
25+
"oauth_client_secret",
26+
]

rfd-model/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fmt::Display;
1515
use thiserror::Error;
1616

1717
pub mod db;
18+
#[rustfmt::skip]
1819
pub mod schema;
1920
pub mod schema_ext;
2021
pub mod storage;

rfd-model/src/schema.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,9 @@ diesel::joinable!(rfd_pdf -> rfd (rfd_id));
9191
diesel::joinable!(rfd_pdf -> rfd_revision (rfd_revision_id));
9292
diesel::joinable!(rfd_revision -> rfd (rfd_id));
9393

94-
diesel::allow_tables_to_appear_in_same_query!(job, rfd, rfd_pdf, rfd_revision,);
94+
diesel::allow_tables_to_appear_in_same_query!(
95+
job,
96+
rfd,
97+
rfd_pdf,
98+
rfd_revision,
99+
);

rfd-processor/config.example.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Allowed values: json, pretty
2+
log_format = "json"
3+
14
# Controls if the processor should run
25
processor_enabled = true
36

@@ -82,4 +85,4 @@ host = ""
8285
# API Key for reading and writing documents
8386
key = ""
8487
# Search index to store documents in
85-
index = ""
88+
index = ""

rfd-processor/src/main.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ mod util;
3131
#[derive(Debug, Deserialize)]
3232
pub struct AppConfig {
3333
pub log_directory: Option<String>,
34+
#[serde(default)]
35+
pub log_format: LogFormat,
3436
pub processor_enabled: bool,
3537
pub processor_batch_size: i64,
3638
pub processor_interval: u64,
@@ -49,6 +51,15 @@ pub struct AppConfig {
4951
pub search_storage: Vec<SearchConfig>,
5052
}
5153

54+
#[derive(Debug, Default, Deserialize)]
55+
#[serde(rename_all = "kebab-case")]
56+
pub enum LogFormat {
57+
Pretty,
58+
// The default value is used to avoid breaking old configuration files.
59+
#[default]
60+
Json,
61+
}
62+
5263
#[derive(Debug, Error)]
5364
pub enum AppError {
5465
#[error("Job task failed")]
@@ -133,13 +144,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
133144
NonBlocking::new(std::io::stdout())
134145
};
135146

136-
let _subscriber = tracing_subscriber::fmt()
147+
let subscriber = tracing_subscriber::fmt()
137148
.with_file(false)
138149
.with_line_number(false)
139150
.with_env_filter(EnvFilter::from_default_env())
140-
.with_writer(writer)
141-
.json()
142-
.init();
151+
.with_writer(writer);
152+
153+
match config.log_format {
154+
LogFormat::Pretty => subscriber.pretty().init(),
155+
LogFormat::Json => subscriber.json().init(),
156+
}
143157

144158
let ctx = Arc::new(Context::new(Database::new(&config.database_url).await, &config).await?);
145159

0 commit comments

Comments
 (0)