@@ -13,72 +13,6 @@ import (
1313 _ "github.com/mattn/go-sqlite3"
1414)
1515
16- // TestCreateLinkWithDownloadsRemainingDefaultExpiresAt tests that creating a link
17- // with only downloadsRemaining set (and no expiresAt) results in a MySQL-compatible
18- // default expires_at value.
19- //
20- // This test reproduces GitHub issue #1584:
21- // When calling createLink mutation with downloadsRemaining set, the default
22- // expires_at value of 1970-01-01 (Unix epoch 0) causes MySQL to fail with:
23- // "incorrect date time value '1970-01-01' for column 'expires_at' at row 1"
24- //
25- // MySQL's minimum DATETIME value is '1000-01-01 00:00:00', but values near the
26- // Unix epoch (1970-01-01) can cause issues with timezone conversions that result
27- // in negative timestamps, which MySQL rejects.
28- func TestCreateLinkWithDownloadsRemainingDefaultExpiresAt (t * testing.T ) {
29- graph := enttest .Open (t , "sqlite3" , "file:ent?mode=memory&cache=shared&_fk=1" )
30- defer graph .Close ()
31-
32- ctx := context .Background ()
33-
34- // Create an asset first (required for link creation)
35- asset := graph .Asset .Create ().
36- SetName ("TestAsset" ).
37- SetContent ([]byte ("test content" )).
38- SaveX (ctx )
39-
40- // Create a link with only downloadsRemaining set (simulating the GraphQL mutation)
41- // This is the problematic case from issue #1584
42- downloadsRemaining := 1
43- input := ent.CreateLinkInput {
44- AssetID : asset .ID ,
45- DownloadsRemaining : & downloadsRemaining ,
46- // ExpiresAt is intentionally NOT set to trigger the default value
47- }
48-
49- link , err := graph .Link .Create ().SetInput (input ).Save (ctx )
50- require .NoError (t , err , "Link creation should succeed" )
51-
52- // The bug: the default expires_at is time.Unix(0, 0) which equals 1970-01-01 00:00:00 UTC
53- // This value is rejected by MySQL with the error:
54- // "incorrect date time value '1970-01-01' for column 'expires_at'"
55- //
56- // MySQL minimum valid DATETIME is '1000-01-01 00:00:00'
57- // However, timestamps near epoch 0 (1970-01-01) can cause issues with timezones
58- // that result in negative Unix timestamps, which MySQL rejects.
59- //
60- // The default should NOT be Unix epoch 0 (1970-01-01).
61- epochTime := time .Unix (0 , 0 )
62- mysqlMinTime := time .Date (1000 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
63-
64- // This test will FAIL if the bug is present (default is epoch 0)
65- // and PASS once the fix is applied
66- if link .ExpiresAt .Equal (epochTime ) {
67- t .Errorf ("BUG DETECTED (GitHub #1584): Default expires_at is Unix epoch 0 (1970-01-01 00:00:00 UTC), " +
68- "which MySQL rejects with error: 'incorrect date time value'. " +
69- "Current value: %v. The default should be a MySQL-compatible value >= %v" ,
70- link .ExpiresAt .UTC (), mysqlMinTime )
71- }
72-
73- // Verify the default expires_at is MySQL-compatible (>= 1000-01-01)
74- assert .True (t , link .ExpiresAt .After (mysqlMinTime ) || link .ExpiresAt .Equal (mysqlMinTime ),
75- "Default expires_at (%v) must be >= MySQL minimum datetime (%v)" ,
76- link .ExpiresAt , mysqlMinTime )
77-
78- // Verify the link was created with the correct downloadsRemaining
79- assert .Equal (t , 1 , link .DownloadsRemaining )
80- }
81-
8216// TestCreateLinkWithExplicitExpiresAt verifies that explicitly setting expiresAt works correctly.
8317func TestCreateLinkWithExplicitExpiresAt (t * testing.T ) {
8418 graph := enttest .Open (t , "sqlite3" , "file:ent?mode=memory&cache=shared&_fk=1" )
@@ -107,4 +41,4 @@ func TestCreateLinkWithExplicitExpiresAt(t *testing.T) {
10741 // Verify the explicit expiresAt was used
10842 assert .WithinDuration (t , futureTime , link .ExpiresAt , time .Second )
10943 assert .Equal (t , 5 , link .DownloadsRemaining )
110- }
44+ }
0 commit comments