Record dataset with name during init

This commit is contained in:
Jacob Hinkle 2022-11-15 09:49:35 -05:00
parent 2006453617
commit 04ecc38925
3 changed files with 10 additions and 5 deletions

View File

@ -60,13 +60,17 @@ pub fn schema_version(conn: &Connection) -> Result<NonZeroUsize, SchemaError> {
/// ///
/// This function initializes a database to the latest schema, and also generates a new random UUID /// This function initializes a database to the latest schema, and also generates a new random UUID
/// and associates that with this dataset. The generated uuid is returned. /// and associates that with this dataset. The generated uuid is returned.
pub fn init(conn: &mut Connection) -> Result<uuid::Uuid, RMError> { pub fn init(conn: &mut Connection, name: &str) -> Result<uuid::Uuid, RMError> {
MIGRATIONS.to_latest(conn)?; MIGRATIONS.to_latest(conn)?;
let u = uuid::Uuid::new_v4(); let u = uuid::Uuid::new_v4();
conn.execute( conn.execute(
"INSERT INTO local_metadata (key, value) VALUES ('dataset_uuid', ?)", "INSERT INTO local_metadata (key, value) VALUES ('dataset_uuid', ?)",
[u.to_string()], [u.to_string()],
)?; )?;
conn.execute(
"INSERT INTO dataset (key, name) VALUES (?1, ?2)",
(u.as_bytes(), name),
)?;
Ok(u) Ok(u)
} }

View File

@ -43,8 +43,8 @@ enum Command {
}, },
} }
fn init_schema(conn: &mut Connection) -> Uuid { fn init_schema(conn: &mut Connection, name: &str) -> Uuid {
match nancy::db::init(conn) { match nancy::db::init(conn, name) {
Err(e) => { Err(e) => {
log::error!("Encountered error in initializing schema: {:?}", e); log::error!("Encountered error in initializing schema: {:?}", e);
process::exit(1); process::exit(1);
@ -130,7 +130,7 @@ fn main() {
Ok(cc) => cc, Ok(cc) => cc,
}; };
conn.pragma_update(None, "foreign_keys", &"ON").unwrap(); conn.pragma_update(None, "foreign_keys", &"ON").unwrap();
let u = init_schema(&mut conn); let u = init_schema(&mut conn, name);
do_record( do_record(
&mut conn, &mut conn,
&format!("Initial recording of dataset {:?}", u), &format!("Initial recording of dataset {:?}", u),

View File

@ -65,7 +65,8 @@ CREATE INDEX FK_user_machine ON user (machine);
-- https://datatracker.ietf.org/doc/html/rfc4122.html -- https://datatracker.ietf.org/doc/html/rfc4122.html
-- This is possible in Python by simply calling uuid.uuid4() with no arguments -- This is possible in Python by simply calling uuid.uuid4() with no arguments
CREATE TABLE dataset ( CREATE TABLE dataset (
key BLOB PRIMARY KEY NOT NULL -- UUID generated by uuid.uuid4() key BLOB PRIMARY KEY NOT NULL, -- UUID generated by uuid.uuid4()
name TEXT NOT NULL
); );