Make ancestor cols not nullable, add fk constraints

Also removed the temporary backfill, now that it is complete.

Updates models so cols are not optional.
This commit is contained in:
Tyler Hallada 2022-01-24 21:41:45 -05:00
parent 10c299dc45
commit 17facae842
5 changed files with 19 additions and 42 deletions

View File

@ -0,0 +1,6 @@
ALTER TABLE "plugins" ALTER COLUMN "mod_id" SET NOT NULL;
ALTER TABLE "plugins" ADD CONSTRAINT "plugins_mod_id_fkey" FOREIGN KEY ("mod_id") REFERENCES "mods" ("id");
ALTER TABLE "plugin_cells" ALTER COLUMN "file_id" SET NOT NULL;
ALTER TABLE "plugin_cells" ADD CONSTRAINT "plugin_cells_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files" ("id");
ALTER TABLE "plugin_cells" ALTER COLUMN "mod_id" SET NOT NULL;
ALTER TABLE "plugin_cells" ADD CONSTRAINT "plugin_cells_mod_id_fkey" FOREIGN KEY ("mod_id") REFERENCES "mods" ("id");

View File

@ -215,35 +215,6 @@ pub async fn main() -> Result<()> {
.connect(&env::var("DATABASE_URL")?) .connect(&env::var("DATABASE_URL")?)
.await?; .await?;
// Temporary backfill
sqlx::query!(
r#"UPDATE plugins
SET mod_id = files.mod_id
FROM files
WHERE
files.id = plugins.file_id AND
plugins.mod_id IS NULL
"#,
)
.execute(&pool)
.await?;
sqlx::query!(
r#"UPDATE plugin_cells
SET
file_id = plugins.file_id,
mod_id = files.mod_id
FROM plugins
JOIN files ON plugins.file_id = files.id
WHERE
plugins.id = plugin_cells.plugin_id AND
plugin_cells.file_id IS NULL AND
plugin_cells.mod_id IS NULL
"#,
)
.execute(&pool)
.await?;
return Ok(());
let args: Args = argh::from_env(); let args: Args = argh::from_env();
if let Some(dump_edits) = args.dump_edits { if let Some(dump_edits) = args.dump_edits {

View File

@ -10,7 +10,7 @@ pub struct Plugin {
pub name: String, pub name: String,
pub hash: i64, pub hash: i64,
pub file_id: i32, pub file_id: i32,
pub mod_id: Option<i32>, pub mod_id: i32,
pub version: f64, pub version: f64,
pub size: i64, pub size: i64,
pub author: Option<String>, pub author: Option<String>,
@ -27,7 +27,7 @@ pub struct UnsavedPlugin<'a> {
pub name: &'a str, pub name: &'a str,
pub hash: i64, pub hash: i64,
pub file_id: i32, pub file_id: i32,
pub mod_id: Option<i32>, pub mod_id: i32,
pub version: f64, pub version: f64,
pub size: i64, pub size: i64,
pub author: Option<&'a str>, pub author: Option<&'a str>,

View File

@ -11,8 +11,8 @@ pub struct PluginCell {
pub id: i32, pub id: i32,
pub plugin_id: i32, pub plugin_id: i32,
pub cell_id: i32, pub cell_id: i32,
pub file_id: Option<i32>, pub file_id: i32,
pub mod_id: Option<i32>, pub mod_id: i32,
pub editor_id: Option<String>, pub editor_id: Option<String>,
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime, pub created_at: NaiveDateTime,
@ -22,8 +22,8 @@ pub struct PluginCell {
pub struct UnsavedPluginCell<'a> { pub struct UnsavedPluginCell<'a> {
pub plugin_id: i32, pub plugin_id: i32,
pub cell_id: i32, pub cell_id: i32,
pub file_id: Option<i32>, pub file_id: i32,
pub mod_id: Option<i32>, pub mod_id: i32,
pub editor_id: Option<&'a str>, pub editor_id: Option<&'a str>,
} }
@ -32,8 +32,8 @@ pub async fn insert(
pool: &sqlx::Pool<sqlx::Postgres>, pool: &sqlx::Pool<sqlx::Postgres>,
plugin_id: i32, plugin_id: i32,
cell_id: i32, cell_id: i32,
file_id: Option<i32>, file_id: i32,
mod_id: Option<i32>, mod_id: i32,
editor_id: Option<String>, editor_id: Option<String>,
) -> Result<PluginCell> { ) -> Result<PluginCell> {
sqlx::query_as!( sqlx::query_as!(
@ -64,8 +64,8 @@ pub async fn batched_insert<'a>(
for batch in plugin_cells.chunks(BATCH_SIZE) { for batch in plugin_cells.chunks(BATCH_SIZE) {
let mut plugin_ids: Vec<i32> = vec![]; let mut plugin_ids: Vec<i32> = vec![];
let mut cell_ids: Vec<i32> = vec![]; let mut cell_ids: Vec<i32> = vec![];
let mut file_ids: Vec<Option<i32>> = vec![]; let mut file_ids: Vec<i32> = vec![];
let mut mod_ids: Vec<Option<i32>> = vec![]; let mut mod_ids: Vec<i32> = vec![];
let mut editor_ids: Vec<Option<&str>> = vec![]; let mut editor_ids: Vec<Option<&str>> = vec![];
batch.iter().for_each(|unsaved_plugin_cell| { batch.iter().for_each(|unsaved_plugin_cell| {
plugin_ids.push(unsaved_plugin_cell.plugin_id); plugin_ids.push(unsaved_plugin_cell.plugin_id);

View File

@ -62,7 +62,7 @@ pub async fn process_plugin(
name: &db_file.name, name: &db_file.name,
hash: hash as i64, hash: hash as i64,
file_id: db_file.id, file_id: db_file.id,
mod_id: Some(db_mod.id), mod_id: db_mod.id,
version: plugin.header.version as f64, version: plugin.header.version as f64,
size: plugin_buf.len() as i64, size: plugin_buf.len() as i64,
author, author,
@ -134,8 +134,8 @@ pub async fn process_plugin(
.map(|(db_cell, plugin_cell)| UnsavedPluginCell { .map(|(db_cell, plugin_cell)| UnsavedPluginCell {
plugin_id: plugin_row.id, plugin_id: plugin_row.id,
cell_id: db_cell.id, cell_id: db_cell.id,
file_id: Some(db_file.id), file_id: db_file.id,
mod_id: Some(db_mod.id), mod_id: db_mod.id,
editor_id: plugin_cell.editor_id.as_ref().map(|id| id.as_ref()), editor_id: plugin_cell.editor_id.as_ref().map(|id| id.as_ref()),
}) })
.collect(); .collect();