import old data
This commit is contained in:
@@ -53,6 +53,7 @@ mod m20260625_000001_add_avatar_to_users;
|
||||
mod m20260627_000001_order_residence_address;
|
||||
mod m20260627_000002_payment_settings;
|
||||
mod m20260627_000003_account_cart_items;
|
||||
mod m20260729_000001_add_legacy_id;
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@@ -110,6 +111,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260627_000001_order_residence_address::Migration),
|
||||
Box::new(m20260627_000002_payment_settings::Migration),
|
||||
Box::new(m20260627_000003_account_cart_items::Migration),
|
||||
Box::new(m20260729_000001_add_legacy_id::Migration),
|
||||
// inject-above (do not remove this comment)
|
||||
]
|
||||
}
|
||||
|
||||
49
migration/src/m20260729_000001_add_legacy_id.rs
Normal file
49
migration/src/m20260729_000001_add_legacy_id.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
//! `legacy_id` — the row's id in the old PrestaShop e-shop
|
||||
//! (<http://e-shop.kompress.sk>), recorded during the catalog import.
|
||||
//!
|
||||
//! Its only job is to make the import repeatable: the task looks a row up by
|
||||
//! `legacy_id` to decide "already imported, skip" instead of inserting a second
|
||||
//! copy. SKU cannot serve that purpose — the old shop reuses codes both across
|
||||
//! products and across sizes of one product (e.g. `2181` on two sizes of the
|
||||
//! same item), so it identifies nothing.
|
||||
//!
|
||||
//! NULL for everything created in this shop; only imported rows carry a value.
|
||||
//! The uniqueness is enforced by a partial unique index rather than in Rust
|
||||
//! because it is exactly the guarantee the import relies on to stay idempotent,
|
||||
//! and it has to hold even if two imports were ever run concurrently.
|
||||
|
||||
use loco_rs::schema::*;
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
const TABLES: [&str; 3] = ["categories", "products", "product_variants"];
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
|
||||
for table in TABLES {
|
||||
add_column(m, table, "legacy_id", ColType::IntegerNull).await?;
|
||||
// Partial index: many rows legitimately have no legacy id, and
|
||||
// NULLs must not collide with each other.
|
||||
m.get_connection()
|
||||
.execute_unprepared(&format!(
|
||||
"CREATE UNIQUE INDEX idx_{table}_legacy_id
|
||||
ON {table} (legacy_id) WHERE legacy_id IS NOT NULL;"
|
||||
))
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
|
||||
for table in TABLES {
|
||||
m.get_connection()
|
||||
.execute_unprepared(&format!("DROP INDEX IF EXISTS idx_{table}_legacy_id;"))
|
||||
.await?;
|
||||
remove_column(m, table, "legacy_id").await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user