pub async fn clone_data(
    txn: &mut PgConnection,
    from_data_id: &Uuid,
) -> Result<Uuid, error::JigCloneDraft> {
    let new_id = sqlx::query!(
        //language=SQL
        r#"
insert into jig_data
(display_name, created_at, updated_at, language, last_synced_at, description, theme, audio_background,
 audio_feedback_negative, audio_feedback_positive, direction, display_score, drag_assist, track_assessments, privacy_level)
select display_name,
       created_at,
       updated_at,
       language,
       last_synced_at,
       description,
       theme,
       audio_background,
       audio_feedback_negative,
       audio_feedback_positive,
       direction,
       display_score,
       drag_assist,
       track_assessments,
       privacy_level
from jig_data
where id = $1
returning id
        "#,
        from_data_id,
    )
    .fetch_one(&mut *txn)
    .await?
    .id;

    // copy metadata
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_module (stable_id, "index", jig_data_id, kind, contents)
select stable_id, "index", $2 as "jig_id", kind, contents
from jig_data_module
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;

    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_additional_resource(jig_data_id, url)
select $2, url
from jig_data_additional_resource
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;

    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_affiliation(jig_data_id, affiliation_id)
select $2, affiliation_id
from jig_data_affiliation
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;

    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_age_range(jig_data_id, age_range_id)
select $2, age_range_id
from jig_data_age_range
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;

    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_category(jig_data_id, category_id)
select $2, category_id
from jig_data_category
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;

    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_goal(jig_data_id, goal_id)
select $2, goal_id
from jig_data_goal
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;

    // copy modules

    Ok(new_id)
}
/// Clones a copy of the jig data and modules, preserving the module's stable IDs
Clones a copy of the jig data and modules, preserving the module's stable IDs
Clones a copy of the jig data and modules preserving the module ' s stable IDs
pub async fn clone_data(
    txn: &mut PgConnection,
    from_data_id: &Uuid,
) -> Result<Uuid, error::JigCloneDraft> {
    let new_id = sqlx::query!(
        //language=SQL
        r#"
insert into jig_data
(display_name, created_at, updated_at, language, last_synced_at, description, theme, audio_background,
 audio_feedback_negative, audio_feedback_positive, direction, display_score, drag_assist, track_assessments, privacy_level)
select display_name,
       created_at,
       updated_at,
       language,
       last_synced_at,
       description,
       theme,
       audio_background,
       audio_feedback_negative,
       audio_feedback_positive,
       direction,
       display_score,
       drag_assist,
       track_assessments,
       privacy_level
from jig_data
where id = $1
returning id
        "#,
        from_data_id,
    )
    .fetch_one(&mut *txn)
    .await?
    .id;
    // copy metadata
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_module (stable_id, "index", jig_data_id, kind, contents)
select stable_id, "index", $2 as "jig_id", kind, contents
from jig_data_module
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_additional_resource(jig_data_id, url)
select $2, url
from jig_data_additional_resource
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_affiliation(jig_data_id, affiliation_id)
select $2, affiliation_id
from jig_data_affiliation
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_age_range(jig_data_id, age_range_id)
select $2, age_range_id
from jig_data_age_range
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_category(jig_data_id, category_id)
select $2, category_id
from jig_data_category
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;
    sqlx::query!(
        //language=SQL
        r#"
insert into jig_data_goal(jig_data_id, goal_id)
select $2, goal_id
from jig_data_goal
where jig_data_id = $1
        "#,
        from_data_id,
        new_id,
    )
    .execute(&mut *txn)
    .await?;
    // copy modules
    Ok(new_id)
}
pub async fn clone_data ( txn : & mut PgConnection , from_data_id : & Uuid , ) -> Result < Uuid , error :: JigCloneDraft > { let new_id = sqlx :: query ! ( //language=SQL r#"
insert into jig_data
(display_name, created_at, updated_at, language, last_synced_at, description, theme, audio_background,
 audio_feedback_negative, audio_feedback_positive, direction, display_score, drag_assist, track_assessments, privacy_level)
select display_name,
       created_at,
       updated_at,
       language,
       last_synced_at,
       description,
       theme,
       audio_background,
       audio_feedback_negative,
       audio_feedback_positive,
       direction,
       display_score,
       drag_assist,
       track_assessments,
       privacy_level
from jig_data
where id = $1
returning id
        "# from_data_id ) . fetch_one ( & mut * txn ) . await ? . id ; // copy metadata sqlx :: query ! ( //language=SQL r#"
insert into jig_data_module (stable_id, "index", jig_data_id, kind, contents)
select stable_id, "index", $2 as "jig_id", kind, contents
from jig_data_module
where jig_data_id = $1
        "# from_data_id new_id ) . execute ( & mut * txn ) . await ? ; sqlx :: query ! ( //language=SQL r#"
insert into jig_data_additional_resource(jig_data_id, url)
select $2, url
from jig_data_additional_resource
where jig_data_id = $1
        "# from_data_id new_id ) . execute ( & mut * txn ) . await ? ; sqlx :: query ! ( //language=SQL r#"
insert into jig_data_affiliation(jig_data_id, affiliation_id)
select $2, affiliation_id
from jig_data_affiliation
where jig_data_id = $1
        "# from_data_id new_id ) . execute ( & mut * txn ) . await ? ; sqlx :: query ! ( //language=SQL r#"
insert into jig_data_age_range(jig_data_id, age_range_id)
select $2, age_range_id
from jig_data_age_range
where jig_data_id = $1
        "# from_data_id new_id ) . execute ( & mut * txn ) . await ? ; sqlx :: query ! ( //language=SQL r#"
insert into jig_data_category(jig_data_id, category_id)
select $2, category_id
from jig_data_category
where jig_data_id = $1
        "# from_data_id new_id ) . execute ( & mut * txn ) . await ? ; sqlx :: query ! ( //language=SQL r#"
insert into jig_data_goal(jig_data_id, goal_id)
select $2, goal_id
from jig_data_goal
where jig_data_id = $1
        "# from_data_id new_id ) . execute ( & mut * txn ) . await ? ; // copy modules Ok ( new_id ) }
Clones a copy of the jig data and modules, preserving the module's stable IDs
Clones a copy of the jig data and modules preserving the module ' s stable IDs
txn
&mut PgConnection
from_data_id
&Uuid
txn
&mut PgConnection
from_data_id
&Uuid