@@ -15,8 +15,6 @@ import (
1515 "github.com/tursodatabase/turso-cli/internal/prompt"
1616)
1717
18- const multipartUploadThresholdBytes = 100 * 1024 * 1024 // 100MB
19-
2018type Database struct {
2119 ID string `json:"dbId" mapstructure:"dbId"`
2220 Name string
@@ -133,8 +131,7 @@ type DBSeed struct {
133131 Timestamp * time.Time `json:"timestamp,omitempty"`
134132 // This is only used locally when uploading a database file and
135133 // never passed to the control plane as JSON.
136- Filepath string `json:"-"`
137- Multipart bool `json:"-"`
134+ Filepath string `json:"-"`
138135}
139136
140137type RemoteEncryption struct {
@@ -158,17 +155,14 @@ type CreateDatabaseBody struct {
158155func (d * DatabasesClient ) Create (name , location , image , extensions , group string , schema string , isSchema bool , seed * DBSeed , sizeLimit , remoteEncryptionCipher , remoteEncryptionKey string , spinner * prompt.SpinnerT ) (* CreateDatabaseResponse , error ) {
159156 isTursoServerUpload := seed != nil && seed .Type == "database_upload" && seed .Filepath != ""
160157 var uploadFilepath string
161- var useMultipart bool
162158 var params CreateDatabaseBody
163159 if isTursoServerUpload {
164160 uploadFilepath = seed .Filepath
165- useMultipart = seed .Multipart
166161 // Clear the unused seed parameters, only Type=database_upload is used.
167162 seed .Filepath = ""
168163 seed .Name = ""
169164 seed .URL = ""
170165 seed .Timestamp = nil
171- seed .Multipart = false
172166 params = CreateDatabaseBody {
173167 Name : name ,
174168 Location : location ,
@@ -216,7 +210,7 @@ func (d *DatabasesClient) Create(name, location, image, extensions, group string
216210 }
217211
218212 if isTursoServerUpload {
219- if _ , err = d .UploadDatabaseAWS (data , group , uploadFilepath , remoteEncryptionCipher , remoteEncryptionKey , useMultipart , spinner ); err != nil {
213+ if _ , err = d .UploadDatabaseAWS (data , group , uploadFilepath , remoteEncryptionCipher , remoteEncryptionKey , spinner ); err != nil {
220214 // Clean up the database if the upload fails
221215 if deleteErr := d .Delete (data .Database .Name ); deleteErr != nil {
222216 fmt .Printf ("%v" , deleteErr )
@@ -237,36 +231,26 @@ func (d *DatabasesClient) Create(name, location, image, extensions, group string
237231// This call happens in DatabasesClient.Create() above, after which it calls this function.
238232// 2. This function creates a DB token for the newly-created DB, and then calls turso-server to upload the database file.
239233// turso-server will perform validations on the file and 'activate' the db if everything is ok.
240- func (d * DatabasesClient ) UploadDatabaseAWS (resp * CreateDatabaseResponse , group , uploadFilepath , remoteEncryptionCipher , remoteEncryptionKey string , useMultipart bool , spinner * prompt.SpinnerT ) (* CreateDatabaseResponse , error ) {
241- // Create a short-lived DB token for the newly created database to facilitate the upload
242- token , err := d . Token ( resp . Database . Name , "1h" , false , nil , nil )
243- if err != nil {
244- return nil , fmt . Errorf ( "could not create database token: %w" , err )
234+ func (d * DatabasesClient ) UploadDatabaseAWS (resp * CreateDatabaseResponse , group , uploadFilepath , remoteEncryptionCipher , remoteEncryptionKey string , spinner * prompt.SpinnerT ) (* CreateDatabaseResponse , error ) {
235+ dbName := resp . Database . Name
236+ tokenTTL := 5 * time . Minute
237+ tokenProvider := func () ( string , error ) {
238+ return d . Token ( dbName , "5m" , false , nil , nil )
245239 }
246240
247241 baseURL , err := url .Parse (fmt .Sprintf ("https://%s" , resp .Database .Hostname ))
248242 if err != nil {
249243 return nil , fmt .Errorf ("unable to create TursoServerClient: %v" , err )
250244 }
251- tursoServerClient , err := NewTursoServerClient (baseURL , token , d .client .cliVersion , d .client .Org )
245+ tursoServerClient , err := NewTursoServerClient (baseURL , tokenProvider , tokenTTL , d .client .cliVersion , d .client .Org )
252246 if err != nil {
253247 return nil , fmt .Errorf ("could not create Turso server client: %w" , err )
254248 }
255249
256250 // Upload the database file
257251 spinner .Text (fmt .Sprintf ("Uploading database %s in group %s, this may take a while..." , internal .Emph (resp .Database .Name ), internal .Emph (group )))
258252
259- stat , err := os .Stat (uploadFilepath )
260- if err != nil {
261- return nil , fmt .Errorf ("failed to fetch file size %s: %w" , uploadFilepath , err )
262- }
263-
264- uploadFunc := tursoServerClient .UploadFileSinglePart
265- if useMultipart || stat .Size () > multipartUploadThresholdBytes {
266- uploadFunc = tursoServerClient .UploadFileMultipart
267- }
268-
269- err = uploadFunc (uploadFilepath , remoteEncryptionCipher , remoteEncryptionKey , func (progressPct int , uploadedBytes int64 , totalBytes int64 , elapsedTime time.Duration , done bool ) {
253+ err = tursoServerClient .UploadFileMultipart (uploadFilepath , remoteEncryptionCipher , remoteEncryptionKey , func (progressPct int , uploadedBytes int64 , totalBytes int64 , elapsedTime time.Duration , done bool ) {
270254 totalSeconds := int (elapsedTime .Seconds ())
271255 minutes := totalSeconds / 60
272256 seconds := totalSeconds % 60
@@ -304,15 +288,14 @@ func (d *DatabasesClient) Export(dbName, dbUrl, outputFile string, withMetadata
304288 return fmt .Errorf ("file %s already exists, use `--overwrite` flag to overwrite it" , outputFile )
305289 }
306290 }
307- token , err := d .Token (dbName , "1h" , false , nil , nil )
308- if err != nil {
309- return fmt .Errorf ("could not create database token: %w" , err )
291+ tokenProvider := func () (string , error ) {
292+ return d .Token (dbName , "1h" , false , nil , nil )
310293 }
311294 baseURL , err := url .Parse (dbUrl )
312295 if err != nil {
313296 return fmt .Errorf ("could not parse database URL: %w" , err )
314297 }
315- tursoServerClient , err := NewTursoServerClient (baseURL , token , d .client .cliVersion , d .client .Org )
298+ tursoServerClient , err := NewTursoServerClient (baseURL , tokenProvider , time . Hour , d .client .cliVersion , d .client .Org )
316299 if err != nil {
317300 return fmt .Errorf ("could not create Turso server client: %w" , err )
318301 }
0 commit comments