v003-create-orders.vala 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using InvercargillSql;
  2. using InvercargillSql.Migrations;
  3. /**
  4. * Migration to create the orders table with foreign keys and indexes.
  5. *
  6. * Demonstrates the fluent FK and index API:
  7. * - .references() for foreign key constraints
  8. * - .indexed() for automatic index creation
  9. * - Chaining FK options like .name() and .on_delete_cascade()
  10. */
  11. public class V003_CreateOrders : Migration {
  12. public override int version { get { return 3; } }
  13. public override string name { get { return "CreateOrders"; } }
  14. public override void up(MigrationBuilder b) throws SqlError {
  15. b.create_table("orders", t => {
  16. t.column<int64?>("id")
  17. .primary_key()
  18. .auto_increment();
  19. // FK to users with explicit name and cascade delete
  20. // indexed() auto-generates idx_orders_user_id
  21. t.column<int64?>("user_id")
  22. .not_null()
  23. .references("users", "id")
  24. .name("fk_orders_users")
  25. .on_delete_cascade()
  26. .indexed();
  27. // FK to products with default name and restrict delete
  28. // indexed() auto-generates idx_orders_product_id
  29. t.column<int64?>("product_id")
  30. .not_null()
  31. .references("products", "id")
  32. .on_delete_restrict()
  33. .indexed();
  34. t.column<int64?>("quantity");
  35. t.column<double?>("total");
  36. // Non-FK column with index for filtering
  37. // indexed() auto-generates idx_orders_status
  38. t.column<string>("status")
  39. .indexed();
  40. t.column<int64?>("created_at");
  41. });
  42. }
  43. public override void down(MigrationBuilder b) throws SqlError {
  44. b.drop_table("orders");
  45. }
  46. }