about the design philosophy behind the display_id in Medusa orders.
#13009
leosin
started this conversation in
Feature Requests
Replies: 1 comment 4 replies
-
|
I've successfully implemented a migration to replace Medusa's default sequential display_id with random 7-digit numbers for better order privacy and UX. import { Migration } from "@mikro-orm/migrations"
export class Migration20250701140000 extends Migration {
override async up(): Promise<void> {
// 1. First, remove the default value
this.addSql(`
ALTER TABLE "order" ALTER COLUMN display_id DROP DEFAULT;
`)
// 2. Create the random ID generation function
this.addSql(`
CREATE OR REPLACE FUNCTION generate_random_display_id()
RETURNS INTEGER AS $$
DECLARE
random_id INTEGER;
max_attempts INTEGER := 100;
attempts INTEGER := 0;
BEGIN
LOOP
-- Generate random 7-digit number between 1000000 and 9999999
random_id := floor(random() * 9000000 + 1000000)::INTEGER;
-- Check if this ID already exists
IF NOT EXISTS (SELECT 1 FROM "order" WHERE display_id = random_id) THEN
RETURN random_id;
END IF;
attempts := attempts + 1;
IF attempts >= max_attempts THEN
RAISE EXCEPTION 'Could not generate unique display_id after % attempts', max_attempts;
END IF;
END LOOP;
END;
$$ LANGUAGE plpgsql;
`)
// 3. Update the existing orders with random display_ids
this.addSql(`
DO $$
DECLARE
order_record RECORD;
new_display_id INTEGER;
BEGIN
FOR order_record IN SELECT id FROM "order" LOOP
new_display_id := generate_random_display_id();
UPDATE "order" SET display_id = new_display_id WHERE id = order_record.id;
END LOOP;
END $$;
`)
// 4. Create the unique index after updating the existing data
this.addSql(`
CREATE UNIQUE INDEX IF NOT EXISTS "IDX_order_display_id_unique" ON "order" (display_id);
`)
// 5. Create the trigger function (modified to be more robust)
this.addSql(`
CREATE OR REPLACE FUNCTION set_order_display_id()
RETURNS TRIGGER AS $$
BEGIN
-- Seulement si display_id n'est pas déjà défini
IF NEW.display_id IS NULL THEN
NEW.display_id := generate_random_display_id();
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
`)
// 6. Create the trigger
this.addSql(`
DROP TRIGGER IF EXISTS order_display_id_trigger ON "order";
CREATE TRIGGER order_display_id_trigger
BEFORE INSERT ON "order"
FOR EACH ROW
EXECUTE FUNCTION set_order_display_id();
`)
}
override async down(): Promise<void> {
// Drop the trigger
this.addSql(`DROP TRIGGER IF EXISTS order_display_id_trigger ON "order";`)
// Drop the trigger function
this.addSql(`DROP FUNCTION IF EXISTS set_order_display_id();`)
// Drop the random ID generation function
this.addSql(`DROP FUNCTION IF EXISTS generate_random_display_id();`)
// Drop the unique index
this.addSql(`DROP INDEX IF EXISTS "IDX_order_display_id_unique";`)
// Restore the original DEFAULT constraint
this.addSql(`
ALTER TABLE "order" ALTER COLUMN display_id SET DEFAULT nextval('order_display_id_seq'::regclass);
`)
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there! I'd like to ask about the design philosophy behind the
display_idin Medusa orders.I understand that
display_idexists to provide customers with a simple, memorable order number, which is definitely practical. However, I've noticed it uses a globally incrementing approach, which has me a bit puzzled.From a business perspective, this sequential order numbering seems to inadvertently expose store volume information. For instance, if a customer sees their order number is #1234, they can roughly infer how many total orders the store has processed. For new merchants, this might affect customer confidence in the brand's credibility; for established merchants, competitors could easily analyze business growth patterns through order numbers.
I'm wondering:
display_id- does it have other design considerations I'm missing?I might not be understanding this deeply enough, so I'd appreciate everyone's insights. After all, platforms like Shopify use non-sequential order numbers by default, so I'm curious about Medusa's design rationale in this regard.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions