A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
1package db
2
3import (
4 "testing"
5)
6
7func TestSplitSQLStatements(t *testing.T) {
8 tests := []struct {
9 name string
10 query string
11 expected []string
12 }{
13 {
14 name: "single statement",
15 query: "SELECT 1",
16 expected: []string{"SELECT 1"},
17 },
18 {
19 name: "single statement with semicolon",
20 query: "SELECT 1;",
21 expected: []string{"SELECT 1"},
22 },
23 {
24 name: "two statements",
25 query: "SELECT 1; SELECT 2;",
26 expected: []string{"SELECT 1", "SELECT 2"},
27 },
28 {
29 name: "statements with comments",
30 query: `-- This is a comment
31ALTER TABLE foo ADD COLUMN bar TEXT;
32
33-- Another comment
34UPDATE foo SET bar = 'test';`,
35 expected: []string{
36 "-- This is a comment\nALTER TABLE foo ADD COLUMN bar TEXT",
37 "-- Another comment\nUPDATE foo SET bar = 'test'",
38 },
39 },
40 {
41 name: "comment-only sections filtered",
42 query: `-- Just a comment
43;
44SELECT 1;`,
45 expected: []string{"SELECT 1"},
46 },
47 {
48 name: "empty query",
49 query: "",
50 expected: nil,
51 },
52 {
53 name: "whitespace only",
54 query: " \n\t ",
55 expected: nil,
56 },
57 {
58 name: "migration 0005 format",
59 query: `-- Add is_attestation column to track attestation manifests
60-- Attestation manifests have vnd.docker.reference.type = "attestation-manifest"
61ALTER TABLE manifest_references ADD COLUMN is_attestation BOOLEAN DEFAULT FALSE;
62
63-- Mark existing unknown/unknown platforms as attestations
64-- Docker BuildKit attestation manifests always have unknown/unknown platform
65UPDATE manifest_references
66SET is_attestation = 1
67WHERE platform_os = 'unknown' AND platform_architecture = 'unknown';`,
68 expected: []string{
69 "-- Add is_attestation column to track attestation manifests\n-- Attestation manifests have vnd.docker.reference.type = \"attestation-manifest\"\nALTER TABLE manifest_references ADD COLUMN is_attestation BOOLEAN DEFAULT FALSE",
70 "-- Mark existing unknown/unknown platforms as attestations\n-- Docker BuildKit attestation manifests always have unknown/unknown platform\nUPDATE manifest_references\nSET is_attestation = 1\nWHERE platform_os = 'unknown' AND platform_architecture = 'unknown'",
71 },
72 },
73 }
74
75 for _, tt := range tests {
76 t.Run(tt.name, func(t *testing.T) {
77 result := splitSQLStatements(tt.query)
78
79 if len(result) != len(tt.expected) {
80 t.Errorf("got %d statements, want %d\ngot: %v\nwant: %v",
81 len(result), len(tt.expected), result, tt.expected)
82 return
83 }
84
85 for i := range result {
86 if result[i] != tt.expected[i] {
87 t.Errorf("statement %d:\ngot: %q\nwant: %q", i, result[i], tt.expected[i])
88 }
89 }
90 })
91 }
92}