()
| 154 | } |
| 155 | |
| 156 | func (o *migrateCreateOptions) run() (version int64, err error) { |
| 157 | var op herrors.Op = "migrate.migrateCreateOptions.run" |
| 158 | timestamp := getTime() |
| 159 | createOptions := mig.New(timestamp, o.name, filepath.Join(o.EC.MigrationDir, o.Source.Name)) |
| 160 | |
| 161 | if o.fromServer { |
| 162 | o.sqlServer = true |
| 163 | } |
| 164 | |
| 165 | var migrateDrv *migrate.Migrate |
| 166 | // disabling auto state migrations for migrate create command |
| 167 | o.EC.DisableAutoStateMigration = true |
| 168 | if o.sqlServer || o.upSQLChanged || o.downSQLChanged { |
| 169 | migrateDrv, err = migrate.NewMigrate(o.EC, true, o.Source.Name, o.Source.Kind) |
| 170 | if err != nil { |
| 171 | return 0, herrors.E(op, fmt.Errorf("cannot create migrate instance: %w", err)) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if o.sqlFile != "" { |
| 176 | // sql-file flag is set |
| 177 | err := createOptions.SetSQLUpFromFile(o.sqlFile) |
| 178 | if err != nil { |
| 179 | return 0, herrors.E(op, fmt.Errorf("cannot set sql file: %w", err)) |
| 180 | } |
| 181 | } |
| 182 | if o.sqlServer { |
| 183 | data, err := migrateDrv.ExportSchemaDump(o.includeSchemas, o.excludeSchemas, o.Source.Name, o.Source.Kind) |
| 184 | if err != nil { |
| 185 | return 0, herrors.E(op, fmt.Errorf("cannot fetch schema dump: %w", err)) |
| 186 | } |
| 187 | err = createOptions.SetSQLUp(string(data)) |
| 188 | if err != nil { |
| 189 | return 0, herrors.E(op, fmt.Errorf("while writing data from server into the up.sql file: %w", err)) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // create pure sql based migrations here |
| 194 | if o.upSQLChanged { |
| 195 | err = createOptions.SetSQLUp(o.upSQL) |
| 196 | if err != nil { |
| 197 | return 0, herrors.E(op, fmt.Errorf("up migration with SQL string could not be created: %w", err)) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if o.downSQLChanged { |
| 202 | err = createOptions.SetSQLDown(o.downSQL) |
| 203 | if err != nil { |
| 204 | return 0, herrors.E(op, fmt.Errorf("down migration with SQL string could not be created: %w", err)) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if o.sqlFile == "" && !o.sqlServer && o.EC.Config.Version != cli.V1 { |
| 209 | // Set empty data for [up|down].sql |
| 210 | if !o.upSQLChanged { |
| 211 | createOptions.SQLUp = []byte(``) |
| 212 | } |
| 213 | if !o.downSQLChanged { |
no test coverage detected