@@ -13,7 +13,6 @@ import (
1313 "github.com/jackc/pgx/v4"
1414 "github.com/spf13/afero"
1515 "github.com/supabase/cli/internal/utils"
16- "github.com/supabase/cli/internal/utils/flags"
1716 "github.com/supabase/cli/internal/utils/tenant"
1817 "github.com/supabase/cli/pkg/api"
1918 "github.com/supabase/cli/pkg/cast"
@@ -22,40 +21,18 @@ import (
2221)
2322
2423func Run (ctx context.Context , projectRef string , skipPooler bool , fsys afero.Fs , options ... func (* pgx.ConnConfig )) error {
25- majorVersion := utils . Config . Db . MajorVersion
24+ // 1. Link postgres version
2625 if err := checkRemoteProjectStatus (ctx , projectRef , fsys ); err != nil {
2726 return err
2827 }
29-
30- // 1. Check service config
28+ // 2. Check service config
3129 keys , err := tenant .GetApiKeys (ctx , projectRef )
3230 if err != nil {
3331 return err
3432 }
3533 LinkServices (ctx , projectRef , keys .ServiceRole , skipPooler , fsys )
36-
37- // 2. Check database connection
38- if config , err := flags .NewDbConfigWithPassword (ctx , projectRef ); err != nil {
39- fmt .Fprintln (os .Stderr , utils .Yellow ("WARN:" ), err )
40- } else if err := linkDatabase (ctx , config , fsys , options ... ); err != nil {
41- return err
42- }
43-
4434 // 3. Save project ref
45- if err := utils .WriteFile (utils .ProjectRefPath , []byte (projectRef ), fsys ); err != nil {
46- return err
47- }
48- fmt .Fprintln (os .Stdout , "Finished " + utils .Aqua ("supabase link" )+ "." )
49-
50- // 4. Suggest config update
51- if utils .Config .Db .MajorVersion != majorVersion {
52- fmt .Fprintln (os .Stderr , utils .Yellow ("WARNING:" ), "Local database version differs from the linked project." )
53- fmt .Fprintf (os .Stderr , `Update your %s to fix it:
54- [db]
55- major_version = %d
56- ` , utils .Bold (utils .ConfigPath ), utils .Config .Db .MajorVersion )
57- }
58- return nil
35+ return utils .WriteFile (utils .ProjectRefPath , []byte (projectRef ), fsys )
5936}
6037
6138func LinkServices (ctx context.Context , projectRef , serviceKey string , skipPooler bool , fsys afero.Fs ) {
@@ -66,7 +43,7 @@ func LinkServices(ctx context.Context, projectRef, serviceKey string, skipPooler
6643 func () error { return linkNetworkRestrictions (ctx , projectRef ) },
6744 func () error { return linkPostgrest (ctx , projectRef ) },
6845 func () error { return linkGotrue (ctx , projectRef ) },
69- func () error { return linkStorage (ctx , projectRef ) },
46+ func () error { return linkStorage (ctx , projectRef , fsys ) },
7047 func () error {
7148 if skipPooler {
7249 utils .Config .Db .Pooler .ConnectionString = ""
@@ -128,15 +105,15 @@ func linkGotrueVersion(ctx context.Context, api tenant.TenantAPI, fsys afero.Fs)
128105 return utils .WriteFile (utils .GotrueVersionPath , []byte (version ), fsys )
129106}
130107
131- func linkStorage (ctx context.Context , projectRef string ) error {
108+ func linkStorage (ctx context.Context , projectRef string , fsys afero. Fs ) error {
132109 resp , err := utils .GetSupabase ().V1GetStorageConfigWithResponse (ctx , projectRef )
133110 if err != nil {
134111 return errors .Errorf ("failed to read Storage config: %w" , err )
135112 } else if resp .JSON200 == nil {
136113 return errors .Errorf ("unexpected Storage config status %d: %s" , resp .StatusCode (), string (resp .Body ))
137114 }
138115 utils .Config .Storage .FromRemoteStorageConfig (* resp .JSON200 )
139- return nil
116+ return utils . WriteFile ( utils . StorageMigrationPath , [] byte ( utils . Config . Storage . TargetMigration ), fsys )
140117}
141118
142119func linkStorageVersion (ctx context.Context , api tenant.TenantAPI , fsys afero.Fs ) error {
@@ -253,8 +230,24 @@ func checkRemoteProjectStatus(ctx context.Context, projectRef string, fsys afero
253230 }
254231
255232 // Update postgres image version to match the remote project
256- if version := resp .JSON200 .Database .Version ; len (version ) > 0 {
257- return utils .WriteFile (utils .PostgresVersionPath , []byte (version ), fsys )
233+ return linkPostgresVersion (resp .JSON200 .Database .Version , fsys )
234+ }
235+
236+ func linkPostgresVersion (version string , fsys afero.Fs ) error {
237+ if len (version ) == 0 {
238+ return nil
258239 }
259- return nil
240+ majorVersion , err := strconv .ParseUint (strings .Split (version , "." )[0 ], 10 , 7 )
241+ if err != nil {
242+ return errors .Errorf ("invalid major version: %w" , err )
243+ }
244+ if uint64 (utils .Config .Db .MajorVersion ) != majorVersion {
245+ fmt .Fprintln (os .Stderr , utils .Yellow ("WARNING:" ), "Local database version differs from the linked project." )
246+ fmt .Fprintf (os .Stderr , `Update your %s to fix it:
247+ [db]
248+ major_version = %d
249+ ` , utils .Bold (utils .ConfigPath ), majorVersion )
250+ }
251+ utils .Config .Db .MajorVersion = uint (majorVersion )
252+ return utils .WriteFile (utils .PostgresVersionPath , []byte (version ), fsys )
260253}
0 commit comments