Skip to content

Commit 3beeb38

Browse files
committed
Add README and metadata to database dump.
1 parent e760a53 commit 3beeb38

File tree

4 files changed

+66
-22
lines changed

4 files changed

+66
-22
lines changed

src/tasks/dump_db.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,57 @@ pub fn dump_db(
2929
/// make sure it gets deleted again even in the case of an error.
3030
#[derive(Debug)]
3131
pub struct DumpDirectory {
32+
pub timestamp: chrono::DateTime<chrono::Utc>,
3233
pub export_dir: PathBuf,
3334
}
3435

3536
impl DumpDirectory {
3637
pub fn create() -> Result<Self, PerformError> {
37-
let timestamp = chrono::Utc::now().format("%Y-%m-%d-%H%M%S").to_string();
38-
let export_dir = std::env::temp_dir().join("dump-db").join(timestamp);
38+
let timestamp = chrono::Utc::now();
39+
let timestamp_str = timestamp.format("%Y-%m-%d-%H%M%S").to_string();
40+
let export_dir = std::env::temp_dir().join("dump-db").join(timestamp_str);
3941
std::fs::create_dir_all(&export_dir)?;
40-
Ok(Self { export_dir })
42+
Ok(Self {
43+
timestamp,
44+
export_dir,
45+
})
4146
}
4247

4348
pub fn dump_db(&self, database_url: &str) -> Result<(), PerformError> {
49+
self.add_readme()?;
50+
self.add_metadata()?;
4451
let export_script = self.export_dir.join("export.sql");
4552
let import_script = self.export_dir.join("import.sql");
4653
gen_scripts::gen_scripts(&export_script, &import_script)?;
4754
std::fs::create_dir(self.export_dir.join("data"))?;
4855
run_psql(&export_script, database_url)
4956
}
57+
58+
fn add_readme(&self) -> Result<(), PerformError> {
59+
use std::io::Write;
60+
61+
let mut readme = File::create(self.export_dir.join("README.md"))?;
62+
readme.write_all(include_bytes!("dump_db/readme_for_tarball.md"))?;
63+
Ok(())
64+
}
65+
66+
fn add_metadata(&self) -> Result<(), PerformError> {
67+
#[derive(Serialize)]
68+
struct Metadata<'a> {
69+
timestamp: &'a chrono::DateTime<chrono::Utc>,
70+
crates_io_commit: String,
71+
format_version: &'static str,
72+
}
73+
let metadata = Metadata {
74+
timestamp: &self.timestamp,
75+
crates_io_commit: dotenv::var("HEROKU_SLUG_COMMIT")
76+
.unwrap_or_else(|_| "unknown".to_owned()),
77+
format_version: "0.1",
78+
};
79+
let file = File::create(self.export_dir.join("metadata.json"))?;
80+
serde_json::to_writer_pretty(file, &metadata)?;
81+
Ok(())
82+
}
5083
}
5184

5285
impl Drop for DumpDirectory {

src/tasks/dump_db/dump-export.sql.hbs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
-- This script was used to create this database dump, and is only included in
2-
-- the archive for reference.
3-
41
BEGIN;
52
{{~#each tables}}
63
{{~#if this.filter}}

src/tasks/dump_db/dump-import.sql.hbs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
-- Script for psql to restore the dump into a local crates.io database.
2-
--
3-
-- WARNING: This will destroy the current database contents.
4-
--
5-
-- Instructions:
6-
--
7-
-- 1. Create a new database and run the Diesel migrations.
8-
--
9-
-- createdb DATABASE_NAME
10-
-- diesel migration run --database-url DATABASE_URL
11-
--
12-
-- 2. Run this script.
13-
--
14-
-- cd DUMP_DIRECTORY
15-
-- psql DATABASE_URL < import.sql
16-
171
BEGIN;
182
-- Set defaults for non-nullable columns not included in the dump.
193
{{~#each tables as |table|}}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# crates.io Database Dump
2+
3+
This is a dump of the public information in the crates.io database.
4+
5+
## Files
6+
7+
* `data/` – the CSV files with the actual dump data.
8+
* `export.sql` – the `psql` script that was used to create this database dump. It is only included in the archive for reference.
9+
* `import.sql` – a `psql` script that can be used to restore the dump into a PostgreSQL database with the same schema as the `crates.io` database, destroying all current data.
10+
* `metadata.json` – some metadata of this dump.
11+
12+
## Metadata Fields
13+
14+
* `timestamp` – the UTC time the dump was started.
15+
* `crates_io_commit` – the git commit hash of the deployed version of crates.io that created this dump.
16+
* `format_version` – the version of the layout and format of this dump. Roughly follows SemVer conventions.
17+
18+
## Restoring to a Local crates.io Database
19+
20+
WARNING: This will destroy the current database contents.
21+
22+
1. Create a new database and run the Diesel migrations.
23+
24+
createdb DATABASE_NAME
25+
diesel migration run --database-url DATABASE_URL
26+
27+
2. Run this script.
28+
29+
cd DUMP_DIRECTORY
30+
psql DATABASE_URL < import.sql

0 commit comments

Comments
 (0)