-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
137 lines (109 loc) · 3.26 KB
/
import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package importcmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/macinnir/dvc/core/lib"
"github.com/macinnir/dvc/core/lib/importer"
"github.com/macinnir/dvc/core/lib/schema"
"go.uber.org/zap"
)
const CommandName = "import"
// Import fetches the sql schema from the target database (specified in dvc.toml)
// and from that generates the json representation at `[schema name].schema.json`
func Cmd(log *zap.Logger, config *lib.Config, args []string) error {
if len(args) > 0 {
return ImportSingleSchema(config, args)
}
return ImportAll(log, config)
}
func ImportSingleSchema(config *lib.Config, args []string) error {
schemaName := ""
connectionName := ""
if len(args) > 0 {
schemaName = args[0]
}
if len(args) > 1 {
connectionName = args[1]
}
schemaType := "app"
isCore := importer.IsCoreSchemaName(schemaName)
if isCore {
schemaType = "core"
}
fmt.Printf("Importing %s schema `%s` from `%s`\n", schemaType, schemaName, connectionName)
var e error
var remoteSchema *schema.Schema
if remoteSchema, e = importer.FetchSchema(config, schemaName, connectionName); e != nil {
return fmt.Errorf("Import: FetchSchema: %w", e)
}
srcFile := ""
if isCore {
// Write to core schema
srcFile = lib.CoreSchemasFilePath
// fmt.Println("Writing to ", lib.CoreSchemasFilePath)
} else {
// Write to app schema
srcFile = lib.SchemasFilePath
// fmt.Println("Writing to ", lib.SchemasFilePath)
}
localSchemas := &schema.SchemaList{}
srcBytes, _ := ioutil.ReadFile(srcFile)
if e = json.Unmarshal(srcBytes, localSchemas); e != nil {
return fmt.Errorf("Unmarshal schema: %w", e)
}
targetSchemaKey := -1
for k := range localSchemas.Schemas {
if localSchemas.Schemas[k].Name == schemaName {
targetSchemaKey = k
break
}
}
// Remote schema does not exist on the local set of schemas
// Add it
if targetSchemaKey == -1 {
localSchemas.Schemas = append(localSchemas.Schemas, remoteSchema)
} else {
localSchemas.Schemas[targetSchemaKey] = remoteSchema
}
var dbBytes []byte
dbBytes, _ = json.MarshalIndent(localSchemas, " ", " ")
return ioutil.WriteFile(srcFile, dbBytes, 0777)
}
func ImportAll(log *zap.Logger, config *lib.Config) error {
var e error
var allSchemas *schema.SchemaList
if allSchemas, e = importer.FetchAllUniqueSchemas(config); e != nil {
log.Error("Error importing all schemas", zap.Error(e))
return e
}
coreSchemaList := &schema.SchemaList{
Schemas: []*schema.Schema{},
}
appSchemaList := &schema.SchemaList{
Schemas: []*schema.Schema{},
}
// for k := range allSchemas.Schemas {
// fmt.Println(k, "Schema: "+allSchemas.Schemas[k].Name)
// }
// os.Exit(1)
for k := range allSchemas.Schemas {
if importer.IsCoreSchemaName(allSchemas.Schemas[k].Name) {
coreSchemaList.Schemas = append(coreSchemaList.Schemas, allSchemas.Schemas[k])
} else {
appSchemaList.Schemas = append(appSchemaList.Schemas, allSchemas.Schemas[k])
}
}
// Core Schema List
var dbBytes []byte
dbBytes, _ = json.MarshalIndent(coreSchemaList, " ", " ")
if e = ioutil.WriteFile(lib.CoreSchemasFilePath, dbBytes, 0644); e != nil {
return e
}
// App Schema List
dbBytes, _ = json.MarshalIndent(appSchemaList, " ", " ")
if e = ioutil.WriteFile(lib.SchemasFilePath, dbBytes, 0644); e != nil {
return e
}
return nil
}