@@ -4,11 +4,11 @@ package cmd
4
4
5
5
import (
6
6
"fmt"
7
-
8
- "github.com/xataio/pgroll/cmd/flags"
9
- "github.com/xataio/pgroll/pkg/state"
7
+ "os"
8
+ "path/filepath"
10
9
11
10
"github.com/spf13/cobra"
11
+ "github.com/xataio/pgroll/pkg/migrations"
12
12
)
13
13
14
14
func pullCmd () * cobra.Command {
@@ -27,34 +27,51 @@ func pullCmd() *cobra.Command {
27
27
ctx := cmd .Context ()
28
28
targetDir := args [0 ]
29
29
30
- state , err := state . New (ctx , flags . PostgresURL (), flags . StateSchema () )
30
+ m , err := NewRoll (ctx )
31
31
if err != nil {
32
32
return err
33
33
}
34
- defer state .Close ()
34
+ defer m .Close ()
35
35
36
36
// Ensure that pgroll is initialized
37
- ok , err := state .IsInitialized (cmd .Context ())
37
+ ok , err := m . State () .IsInitialized (cmd .Context ())
38
38
if err != nil {
39
39
return err
40
40
}
41
41
if ! ok {
42
42
return errPGRollNotInitialized
43
43
}
44
44
45
- migs , err := state .SchemaHistory (ctx , flags .Schema ())
45
+ // Ensure that the target directory is valid, creating it if it doesn't
46
+ // exist
47
+ _ , err = os .Stat (targetDir )
48
+ if err != nil {
49
+ if os .IsNotExist (err ) {
50
+ err := os .MkdirAll (targetDir , 0o755 )
51
+ if err != nil {
52
+ return fmt .Errorf ("failed to create target directory: %w" , err )
53
+ }
54
+ } else {
55
+ return fmt .Errorf ("failed to stat directory: %w" , err )
56
+ }
57
+ }
58
+
59
+ // Get the list of missing migrations (those that have been applied to
60
+ // the target database but are missing in the local directory).
61
+ migs , err := m .MissingMigrations (ctx , os .DirFS (targetDir ))
46
62
if err != nil {
47
- return fmt .Errorf ("failed to read schema history : %w" , err )
63
+ return fmt .Errorf ("failed to read migrations from target directory : %w" , err )
48
64
}
49
65
66
+ // Write the missing migrations to the target directory
50
67
for i , mig := range migs {
51
68
prefix := ""
52
69
if withPrefixes {
53
70
prefix = fmt .Sprintf ("%04d" , i + 1 ) + "_"
54
71
}
55
- err := mig . WriteToFile ( targetDir , prefix , useJSON )
72
+ err := writeMigrationToFile ( mig , targetDir , prefix , useJSON )
56
73
if err != nil {
57
- return fmt .Errorf ("failed to write migration %q: %w" , mig .Migration . Name , err )
74
+ return fmt .Errorf ("failed to write migration %q: %w" , mig .Name , err )
58
75
}
59
76
}
60
77
return nil
@@ -66,3 +83,33 @@ func pullCmd() *cobra.Command {
66
83
67
84
return pullCmd
68
85
}
86
+
87
+ // WriteToFile writes the migration to a file in `targetDir`, prefixing the
88
+ // filename with `prefix`. The output format defaults to YAML, but can
89
+ // be changed to JSON by setting `useJSON` to true.
90
+ func writeMigrationToFile (m * migrations.Migration , targetDir , prefix string , useJSON bool ) error {
91
+ err := os .MkdirAll (targetDir , 0o755 )
92
+ if err != nil {
93
+ return err
94
+ }
95
+
96
+ suffix := "yaml"
97
+ if useJSON {
98
+ suffix = "json"
99
+ }
100
+
101
+ fileName := fmt .Sprintf ("%s%s.%s" , prefix , m .Name , suffix )
102
+ filePath := filepath .Join (targetDir , fileName )
103
+
104
+ file , err := os .Create (filePath )
105
+ if err != nil {
106
+ return err
107
+ }
108
+ defer file .Close ()
109
+
110
+ if useJSON {
111
+ return m .WriteAsJSON (file )
112
+ } else {
113
+ return m .WriteAsYAML (file )
114
+ }
115
+ }
0 commit comments