ProvideCommand provides migration and seed command.
(command *cobra.Command)
| 55 | |
| 56 | // ProvideCommand provides migration and seed command. |
| 57 | func (m Module) ProvideCommand(command *cobra.Command) { |
| 58 | var ( |
| 59 | force bool |
| 60 | rollbackId string |
| 61 | logger = logging.WithLevel(m.logger) |
| 62 | ) |
| 63 | migrateCmd := &cobra.Command{ |
| 64 | Use: "migrate [database]", |
| 65 | Short: "Migrate gorm tables", |
| 66 | Long: `Run all gorm table migrations.`, |
| 67 | RunE: func(cmd *cobra.Command, args []string) error { |
| 68 | connection := "default" |
| 69 | if len(args) > 0 { |
| 70 | connection = args[0] |
| 71 | } |
| 72 | |
| 73 | if m.env.IsProduction() && !force { |
| 74 | e := fmt.Errorf("migrations and rollback in production requires force flag to be set") |
| 75 | return e |
| 76 | } |
| 77 | |
| 78 | migrations := m.collectMigrations(connection) |
| 79 | |
| 80 | if rollbackId != "" { |
| 81 | if err := migrations.Rollback(rollbackId); err != nil { |
| 82 | return fmt.Errorf("unable to rollback: %w", err) |
| 83 | } |
| 84 | |
| 85 | logger.Info("rollback successfully completed") |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | if err := migrations.Migrate(); err != nil { |
| 90 | return fmt.Errorf("unable to migrate: %w", err) |
| 91 | } |
| 92 | |
| 93 | logger.Info("migration successfully completed") |
| 94 | return nil |
| 95 | }, |
| 96 | } |
| 97 | migrateCmd.Flags().BoolVarP(&force, "force", "f", false, "migrations and rollback in production requires force flag to be set") |
| 98 | migrateCmd.Flags().StringVarP(&rollbackId, "rollback", "r", "", "rollback to the given migration id") |
| 99 | migrateCmd.Flag("rollback").NoOptDefVal = "-1" |
| 100 | |
| 101 | seedCmd := &cobra.Command{ |
| 102 | Use: "seed [database]", |
| 103 | Short: "seed the database", |
| 104 | Long: `use the provided seeds to bootstrap fake data in database`, |
| 105 | RunE: func(cmd *cobra.Command, args []string) error { |
| 106 | connection := "default" |
| 107 | if len(args) > 0 { |
| 108 | connection = args[0] |
| 109 | } |
| 110 | |
| 111 | if m.env.IsProduction() && !force { |
| 112 | return fmt.Errorf("seeding in production requires force flag to be set") |
| 113 | } |
| 114 |
nothing calls this directly
no test coverage detected