v003-create-orders.vala 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 string migration_namespace { get { return "app"; } }
  13. public override uint64 serial { get { return 3; } }
  14. public override string name { get { return "CreateOrders"; } }
  15. public override void up(MigrationBuilder b) throws SqlError {
  16. b.create_table("orders", t => {
  17. t.column<int64?>("id")
  18. .primary_key()
  19. .auto_increment();
  20. // FK to users with explicit name and cascade delete
  21. // indexed() auto-generates idx_orders_user_id
  22. t.column<int64?>("user_id")
  23. .not_null()
  24. .references("users", "id")
  25. .name("fk_orders_users")
  26. .on_delete_cascade()
  27. .indexed();
  28. // FK to products with default name and restrict delete
  29. // indexed() auto-generates idx_orders_product_id
  30. t.column<int64?>("product_id")
  31. .not_null()
  32. .references("products", "id")
  33. .on_delete_restrict()
  34. .indexed();
  35. t.column<int64?>("quantity");
  36. t.column<double?>("total");
  37. // Non-FK column with index for filtering
  38. // indexed() auto-generates idx_orders_status
  39. t.column<string>("status")
  40. .indexed();
  41. t.column<int64?>("created_at");
  42. });
  43. }
  44. public override void down(MigrationBuilder b) throws SqlError {
  45. b.drop_table("orders");
  46. }
  47. }