export_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package sqlexport
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/danfragoso/pizzasql-next/pkg/storage"
  6. "github.com/danfragoso/pizzasql-next/pkg/testkv"
  7. )
  8. // A schema dump must keep a non-constant default as SQL text; emitting the
  9. // value evaluated at export time would freeze it.
  10. func TestExportPreservesDefaultExpression(t *testing.T) {
  11. kv := testkv.New(t)
  12. pool := kv.Pool(4)
  13. t.Cleanup(func() { pool.Close() })
  14. schemaManager := storage.NewSchemaManager(pool, "testdb")
  15. tableManager := storage.NewTableManager(pool, schemaManager, "testdb")
  16. if err := schemaManager.CreateTable(&storage.Schema{
  17. Name: "events",
  18. Columns: []storage.Column{
  19. {Name: "id", Type: "INTEGER", Nullable: false, PrimaryKey: true},
  20. },
  21. }); err != nil {
  22. t.Fatalf("create table: %v", err)
  23. }
  24. // The table is empty, so the schema manager needs no row evaluator for the
  25. // rewrite and the column can be declared with an expression default.
  26. if err := schemaManager.AddColumn("events", storage.Column{
  27. Name: "created_at",
  28. Type: "TIMESTAMP",
  29. Nullable: true,
  30. DefaultExpr: "CURRENT_TIMESTAMP",
  31. }, nil, nil); err != nil {
  32. t.Fatalf("add column: %v", err)
  33. }
  34. dump, err := ExportDatabase(schemaManager, tableManager, ExportOptions{Tables: []string{"events"}})
  35. if err != nil {
  36. t.Fatalf("export: %v", err)
  37. }
  38. if !strings.Contains(dump, "DEFAULT CURRENT_TIMESTAMP") {
  39. t.Fatalf("dump lost the default expression:\n%s", dump)
  40. }
  41. }
  42. // A composite key must be dumped as one table-level constraint; emitting an
  43. // inline PRIMARY KEY on each key column would describe two keys.
  44. func TestExportCompositePrimaryKey(t *testing.T) {
  45. kv := testkv.New(t)
  46. pool := kv.Pool(4)
  47. t.Cleanup(func() { pool.Close() })
  48. schemaManager := storage.NewSchemaManager(pool, "testdb")
  49. tableManager := storage.NewTableManager(pool, schemaManager, "testdb")
  50. if err := schemaManager.CreateTable(&storage.Schema{
  51. Name: "pairs",
  52. PrimaryKeyColumns: []string{"a", "b"},
  53. Columns: []storage.Column{
  54. {Name: "a", Type: "TEXT", Nullable: false},
  55. {Name: "b", Type: "TEXT", Nullable: false},
  56. {Name: "v", Type: "TEXT", Nullable: true},
  57. },
  58. }); err != nil {
  59. t.Fatalf("create table: %v", err)
  60. }
  61. dump, err := ExportDatabase(schemaManager, tableManager, ExportOptions{Tables: []string{"pairs"}})
  62. if err != nil {
  63. t.Fatalf("export: %v", err)
  64. }
  65. if got := strings.Count(dump, "PRIMARY KEY"); got != 1 {
  66. t.Fatalf("dump has %d PRIMARY KEY clauses, want 1:\n%s", got, dump)
  67. }
  68. if !strings.Contains(dump, "PRIMARY KEY (a, b)") {
  69. t.Fatalf("dump lost the composite key:\n%s", dump)
  70. }
  71. }