cargo-pgrx 0.19.0

Cargo subcommand for 'pgrx' to make Postgres extension development easy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
use cargo_toml::{Dependency, DepsSet, Manifest};
use crates_index::SparseIndex;
use eyre::eyre;
use semver::VersionReq;
use std::collections::HashSet;
use std::sync::LazyLock;
use std::{fs, path::PathBuf};
use toml_edit::DocumentMut;
use tracing::info;

use crate::CommandExecute;

/// Upgrade pgrx crate versions in `Cargo.toml`.
/// Defaults to latest.
#[derive(clap::Args, Debug)]
#[clap(author)]
pub(crate) struct Upgrade {
    /// Specify a version requirement to upgrade to, rather than defaulting to the latest version. Accepts any Cargo version requirement syntax: bare versions (`0.16.1`, treated as caret), explicit operators (`^0.16`, `~0.16.1`, `=0.16.1`, `>=0.16, <0.17`), etc.
    #[clap(long)]
    pub(crate) to: Option<VersionReq>,

    /// Path to the manifest file (usually Cargo.toml). Defaults to
    /// "./Cargo.toml" in the working directory.
    #[clap(long = "manifest-path", short)]
    pub(crate) manifest_path: Option<PathBuf>,

    /// Flag for upgrading PGRX to pre-release versions.
    #[clap(long = "include-prereleases")]
    pub(crate) include_prereleases: bool,

    /// Select a package within the current workspace within which to upgrade
    /// pgrx package versions. Defaults to the root manifest in the working
    /// directory.
    #[clap(long, short)]
    pub(crate) package: Option<String>,

    /// Dry-run - if this flag is set, Cargo.toml will not be modified.
    /// Instead, this command will print the text of the new Cargo.toml
    /// that would have been generated if it was modified.
    #[clap(long = "dry-run", short = 'n')]
    pub(crate) dry_run: bool,
}

impl CommandExecute for Upgrade {
    #[tracing::instrument(level = "error", skip(self))]
    fn execute(self) -> eyre::Result<()> {
        let path = self.manifest_path.map_or_else(|| PathBuf::from("./Cargo.toml"), |p| p.clone());
        let path = find_manifest_file(&path.canonicalize()?, self.package.as_ref())?;

        let version = if let Some(v) = self.to {
            TargetVersion::Match(v)
        } else if self.include_prereleases {
            TargetVersion::DiscoverPrerelease
        } else {
            TargetVersion::DiscoverReleased
        };

        let updated = process_manifest_file(&path, &version)?;

        if self.dry_run {
            Ok(println!("{}", updated.to_string()))
        } else {
            fs::write(&path, updated.to_string())
                .map_err(|err| eyre!("Unable to write the updated Cargo.toml to disk: {err}"))
        }
    }
}

#[derive(Debug)]
enum TargetVersion {
    DiscoverPrerelease,
    DiscoverReleased,
    Match(VersionReq),
}

// Strategy for rewriting this dependency's version field. Encoding the two cases as a type lets each match arm own exactly one representation, instead of threading an Option<String> + tupl match across two stages.
#[derive(Debug)]
enum Rewrite {
    /// Overwrite the version field with this literal string,  Used when the user supplies a full Cargo version requirement.
    Replace(String),
    /// Substitute only the bare version number in the existing  requirement, preserving any leading operator (`^`, `=`, `~`).
    SwapBareVersion(String),
}

/// Starting at path, search for the Cargo manifest containing package.
#[tracing::instrument(level = "error")]
fn find_manifest_file(path: &PathBuf, package: Option<&String>) -> eyre::Result<PathBuf> {
    let (manifest_dirpath, manifest_filepath) = if fs::metadata(&path)?.is_dir() {
        (path.clone(), path.join("Cargo.toml"))
    } else {
        (path.parent().expect("parent").to_path_buf(), path.clone())
    };

    let input = Manifest::from_path(&manifest_filepath)
        .map_err(|e| eyre!("Error opening manifest: {e}"))?;

    match (package, &input.workspace) {
        // Without a package argument, the manifest argument is already correct.
        (None, _) => Ok(manifest_filepath),

        // With a package argument and no workspace, check the name in the manifest.
        (Some(name), None) => {
            if let Some(input_package) = &input.package
                && input_package.name().to_lowercase() == name.to_lowercase()
            {
                Ok(manifest_filepath)
            } else {
                Err(eyre!("No package {name:?} in {:?}", manifest_filepath.file_name()))
            }
        }

        // With a package argument in a workspace, search among the workspace members.
        // Report errors from each member when the package is not found.
        (Some(name), Some(workspace)) => {
            let mut errs = Vec::with_capacity(workspace.members.len());

            for dir in &workspace.members {
                match find_manifest_file(&manifest_dirpath.join(dir), package) {
                    Ok(v) => return Ok(v),
                    Err(e) => errs.push(format!("- {dir}: {e}")),
                }
            }

            Err(eyre!("No package {name:?} in {manifest_filepath:?}:\n{}", errs.join("\n")))
        }
    }
}

/// Load the Cargo manifest at path and return a copy with updated dependency versions.
#[tracing::instrument(level = "error")]
fn process_manifest_file(path: &PathBuf, version: &TargetVersion) -> eyre::Result<DocumentMut> {
    let input = Manifest::from_path(&path).map_err(|e| eyre!("Error opening manifest: {e}"))?;

    let mut output: DocumentMut = fs::read_to_string(&path)
        .map_err(|e| eyre!("Error opening manifest: {e}"))?
        .parse()
        .map_err(|e| eyre!("Error parsing manifest: {e}"))?;

    update_manifest(&input, &mut output, &version)?;

    Ok(output)
}

/// Update versions in the "dependencies", "dev-dependencies", and "workspace.dependencies" sections
/// of a Cargo manifest loaded into source and sink.
#[tracing::instrument(level = "error")]
fn update_manifest(
    source: &Manifest,
    sink: &mut DocumentMut,
    target: &TargetVersion,
) -> eyre::Result<()> {
    if !source.dependencies.is_empty() {
        let section = sink["dependencies"].as_table_like_mut();
        let section = section.expect("source and sink diverged");

        update_manifest_section(section, &source.dependencies, target)?;
    }

    if !source.dev_dependencies.is_empty() {
        let section = sink["dev-dependencies"].as_table_like_mut();
        let section = section.expect("source and sink diverged");

        update_manifest_section(section, &source.dev_dependencies, target)?;
    }

    if let Some(workspace) = &source.workspace
        && !workspace.dependencies.is_empty()
    {
        let section = sink["workspace"]["dependencies"].as_table_like_mut();
        let section = section.expect("source and sink diverged");

        update_manifest_section(section, &workspace.dependencies, target)?;
    }

    Ok(())
}

/// Update dependency versions in a single section of a Cargo manifest.
fn update_manifest_section<T: toml_edit::TableLike + ?Sized>(
    section: &mut T,
    dependencies: &DepsSet,
    target: &TargetVersion,
) -> eyre::Result<()> {
    static RELEVANT_PACKAGES: LazyLock<HashSet<&str>> = LazyLock::new(|| {
        HashSet::from([
            "pgrx",
            "pgrx-bench",
            "pgrx-macros",
            "pgrx-pg-config",
            "pgrx-pg-sys",
            "pgrx-sql-entity-graph",
            "pgrx-tests",
        ])
    });

    // Regex to capture any operators in the manifest version requirement.
    static REQUIREMENT_REGEX: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"^([^0-9.]*)([0-9.].*)$").unwrap());

    for (local_name, dependency) in dependencies {
        let crate_name = match dependency {
            Dependency::Inherited(_) => continue,
            Dependency::Simple(_) => local_name,
            Dependency::Detailed(detail) => {
                if let Some(crate_name) = &detail.package {
                    crate_name
                } else {
                    local_name
                }
            }
        };

        if !RELEVANT_PACKAGES.contains(crate_name.as_str()) {
            continue;
        }

        if dependency_inherits_workspace(section, local_name) {
            continue;
        }

        let index = match dependency {
            Dependency::Inherited(_) => continue,
            Dependency::Simple(_) => SparseIndex::new_cargo_default()?,
            Dependency::Detailed(detail) => {
                if let Some(registry) = &detail.registry {
                    unimplemented!("custom registry {registry:?}")
                } else {
                    SparseIndex::new_cargo_default()?
                }
            }
        };

        let strategy: Rewrite = match target {
            TargetVersion::Match(req) => Rewrite::Replace(req.to_string()),
            TargetVersion::DiscoverPrerelease => {
                let krate = index.crate_from_cache(crate_name)?;
                let highest = krate.highest_version();
                let bare = (!highest.is_yanked())
                    .then_some(highest.version())
                    .ok_or_else(|| {
                        eyre!("Latest version {:?} for {crate_name:?} is yanked", highest.version())
                    })?
                    .to_owned();
                Rewrite::SwapBareVersion(bare)
            }
            TargetVersion::DiscoverReleased => {
                let krate = index.crate_from_cache(crate_name)?;
                let highest = krate.highest_normal_version();
                let bare = highest
                    .ok_or_else(|| eyre!("No released version for {crate_name:?}"))?
                    .version()
                    .to_owned();
                Rewrite::SwapBareVersion(bare)
            }
        };

        let rewrite = |existing: &str| -> String {
            match &strategy {
                Rewrite::Replace(s) => s.to_string(),
                Rewrite::SwapBareVersion(v) => {
                    REQUIREMENT_REGEX.replace(existing, format!("{}{}", "${1}", v)).into_owned()
                }
            }
        };

        match dependency {
            Dependency::Inherited(_) => continue,
            Dependency::Simple(requirement) => {
                let next_requirement = rewrite(requirement);
                section.insert(&local_name, next_requirement.into());
            }
            Dependency::Detailed(detail) => {
                let Some(requirement) = &detail.version else {
                    info!("No version specified for {local_name}, not upgrading.");
                    continue;
                };
                let next_requirement = rewrite(requirement);
                let table = section.get_mut(&local_name).expect("source and sink diverged");
                let table = table.as_table_like_mut().expect("source and sink diverged");
                table.insert("version", next_requirement.into());
            }
        }
    }

    Ok(())
}

fn dependency_inherits_workspace<T: toml_edit::TableLike + ?Sized>(
    section: &T,
    local_name: &str,
) -> bool {
    section
        .get(local_name)
        .and_then(|item| item.as_table_like())
        .and_then(|table| table.get("workspace"))
        .and_then(|workspace| workspace.as_bool())
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use cargo_toml;
    use goldenfile;
    use std::fs;

    #[test]
    fn find_package_manifest() {
        let root_path = env!("CARGO_MANIFEST_DIR");
        let fixtures_path = format!("{root_path}/tests/fixtures/package");
        let manifest_path = format!("{fixtures_path}/expected-0.16.0.toml").into();

        let parsed = cargo_toml::Manifest::from_path(&manifest_path).expect("fixture exists");
        assert!(parsed.package.is_some() && parsed.workspace.is_none(), "package manifest");

        let found = super::find_manifest_file(&manifest_path, None).unwrap();
        assert_eq!(found, manifest_path);

        let found = super::find_manifest_file(&manifest_path, Some(&"package".to_owned())).unwrap();
        assert_eq!(found, manifest_path);

        let _ = super::find_manifest_file(&manifest_path, Some(&"other".to_owned())).unwrap_err();
    }

    #[test]
    fn find_package_manifest_in_workspace() {
        let root_path = env!("CARGO_MANIFEST_DIR");
        let fixtures_path = format!("{root_path}/tests/fixtures/workspace");
        let manifest_path = format!("{fixtures_path}/Cargo.toml").into();

        let parsed = cargo_toml::Manifest::from_path(&manifest_path).expect("fixture exists");
        assert!(parsed.package.is_none() && parsed.workspace.is_some(), "workspace manifest");

        let found = super::find_manifest_file(&manifest_path, None).unwrap();
        assert_eq!(found, manifest_path);

        let found = super::find_manifest_file(&manifest_path, Some(&"hello".to_owned())).unwrap();
        assert_eq!(found, format!("{fixtures_path}/hello/Cargo.toml"));

        let _ = super::find_manifest_file(&manifest_path, Some(&"other".to_owned())).unwrap_err();
    }

    #[test]
    fn process_package_manifest() {
        let root_path = env!("CARGO_MANIFEST_DIR");
        let fixtures_path = format!("{root_path}/tests/fixtures/package");
        let manifest_path = format!("{fixtures_path}/expected-0.16.0.toml");
        let mut golden = goldenfile::Mint::new(&fixtures_path);

        let parsed = cargo_toml::Manifest::from_path(&manifest_path).expect("fixture exists");
        assert!(parsed.package.is_some() && parsed.workspace.is_none(), "package manifest");

        let updated = super::process_manifest_file(
            &manifest_path.into(),
            &super::TargetVersion::Match(semver::VersionReq::parse("=0.16.1").unwrap()),
        )
        .unwrap();

        fs::write(golden.new_goldenpath("expected-0.16.1.toml").unwrap(), updated.to_string())
            .unwrap();
    }

    #[test]
    fn process_workspace_manifest() {
        let root_path = env!("CARGO_MANIFEST_DIR");
        let fixtures_path = format!("{root_path}/tests/fixtures/workspace");
        let manifest_path = format!("{fixtures_path}/Cargo.toml");
        let mut golden = goldenfile::Mint::new(&fixtures_path);

        let parsed = cargo_toml::Manifest::from_path(&manifest_path).expect("fixture exists");
        assert!(parsed.package.is_none() && parsed.workspace.is_some(), "workspace manifest");

        let updated = super::process_manifest_file(
            &manifest_path.into(),
            &super::TargetVersion::Match(semver::VersionReq::parse("=0.18.1").unwrap()),
        )
        .unwrap();

        fs::write(golden.new_goldenpath("expected-0.18.1.toml").unwrap(), updated.to_string())
            .unwrap();
    }

    #[test]
    fn process_workspace_package_manifest() {
        let root_path = env!("CARGO_MANIFEST_DIR");
        let fixtures_path = format!("{root_path}/tests/fixtures/workspace/hello");
        let manifest_path = format!("{fixtures_path}/Cargo.toml");
        let mut golden = goldenfile::Mint::new(&fixtures_path);

        let parsed = cargo_toml::Manifest::from_path(&manifest_path).expect("fixture exists");
        assert!(parsed.package.is_some() && parsed.workspace.is_none(), "package manifest");

        let updated = super::process_manifest_file(
            &manifest_path.into(),
            &super::TargetVersion::Match(semver::VersionReq::parse("=0.18.1").unwrap()),
        )
        .unwrap();

        fs::write(golden.new_goldenpath("expected-0.18.1.toml").unwrap(), updated.to_string())
            .unwrap();
    }

    use clap::Parser;
    use semver::VersionReq;

    #[derive(clap::Parser, Debug)]
    struct Wrap {
        #[clap(flatten)]
        inner: super::Upgrade,
    }

    #[test]
    fn parse_to_accepts_bare_version() {
        let parsed = Wrap::try_parse_from(["pgrx", "--to", "0.16.1"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse("0.16.1").unwrap()));
    }

    #[test]
    fn parse_to_accepts_partial_version() {
        // Cargo allows bare partial versions like `0.16`, treated as caret.
        let parsed = Wrap::try_parse_from(["pgrx", "--to", "0.16"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse("0.16").unwrap()));
    }

    #[test]
    fn parse_to_accepts_caret() {
        let parsed = Wrap::try_parse_from(["pgrx", "--to", "^0.16.1"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse("^0.16.1").unwrap()));
    }

    #[test]
    fn parse_to_accepts_tilde() {
        let parsed = Wrap::try_parse_from(["pgrx", "--to", "~0.16.1"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse("~0.16.1").unwrap()));
    }

    #[test]
    fn parse_to_accepts_exact_pin() {
        let parsed = Wrap::try_parse_from(["pgrx", "--to", "=0.16.1"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse("=0.16.1").unwrap()));
    }

    #[test]
    fn parse_to_accepts_range() {
        let parsed = Wrap::try_parse_from(["pgrx", "--to", ">=0.16, <0.17"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse(">=0.16, <0.17").unwrap()));
    }

    #[test]
    fn parse_to_accepts_prerelease() {
        let parsed = Wrap::try_parse_from(["pgrx", "--to", "=1.0.0-beta.1"]).unwrap();
        assert_eq!(parsed.inner.to, Some(VersionReq::parse("=1.0.0-beta.1").unwrap()));
    }

    #[test]
    fn parse_to_rejects_garbage() {
        let err = Wrap::try_parse_from(["pgrx", "--to", "foo"]).unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.contains("invalid value 'foo'"),
            "expected clap error to mention the invalid value, got: {rendered}"
        );
    }

    #[test]
    fn parse_to_rejects_empty() {
        let err = Wrap::try_parse_from(["pgrx", "--to", ""]).unwrap_err();
        let rendered = err.to_string();
        assert!(
            rendered.contains("invalid value ''"),
            "expected clap error to mention the empty value, got: {rendered}"
        );
    }

    #[test]
    fn parse_to_defaults_to_none() {
        let parsed = Wrap::try_parse_from(["pgrx"]).unwrap();
        assert_eq!(parsed.inner.to, None);
    }
}