MCPcopy
hub / github.com/pocketbase/pocketbase / migrateCreateHandler

Method migrateCreateHandler

plugins/migratecmd/migratecmd.go:146–194  ·  view source on GitHub ↗
(template string, args []string, interactive bool)

Source from the content-addressed store, hash-verified

144}
145
146func (p *plugin) migrateCreateHandler(template string, args []string, interactive bool) (string, error) {
147 if len(args) < 1 {
148 return "", errors.New("missing migration file name")
149 }
150
151 name := args[0]
152 dir := p.config.Dir
153
154 filename := fmt.Sprintf("%d_%s.%s", time.Now().Unix(), inflector.Snakecase(name), p.config.TemplateLang)
155
156 resultFilePath := path.Join(dir, filename)
157
158 if interactive {
159 confirm := osutils.YesNoPrompt(fmt.Sprintf("Do you really want to create migration %q?", resultFilePath), false)
160 if !confirm {
161 fmt.Println("The command has been cancelled")
162 return "", nil
163 }
164 }
165
166 // get default create template
167 if template == "" {
168 var templateErr error
169 if p.config.TemplateLang == TemplateLangJS {
170 template, templateErr = p.jsBlankTemplate()
171 } else {
172 template, templateErr = p.goBlankTemplate()
173 }
174 if templateErr != nil {
175 return "", fmt.Errorf("failed to resolve create template: %v", templateErr)
176 }
177 }
178
179 // ensure that the migrations dir exist
180 if err := os.MkdirAll(dir, os.ModePerm); err != nil {
181 return "", err
182 }
183
184 // save the migration file
185 if err := os.WriteFile(resultFilePath, []byte(template), 0644); err != nil {
186 return "", fmt.Errorf("failed to save migration file %q: %v", resultFilePath, err)
187 }
188
189 if interactive {
190 fmt.Printf("Successfully created file %q\n", resultFilePath)
191 }
192
193 return filename, nil
194}
195
196func (p *plugin) migrateCollectionsHandler(args []string, interactive bool) (string, error) {
197 createArgs := []string{"collections_snapshot"}

Callers 2

createCommandMethod · 0.95

Calls 5

jsBlankTemplateMethod · 0.95
goBlankTemplateMethod · 0.95
SnakecaseFunction · 0.92
YesNoPromptFunction · 0.92
UnixMethod · 0.80

Tested by

no test coverage detected