(ctx context.Context, out io.Writer, _ *storepb.DatabaseSchemaMetadata)
| 57 | } |
| 58 | |
| 59 | func (d *Driver) Dump(ctx context.Context, out io.Writer, _ *storepb.DatabaseSchemaMetadata) error { |
| 60 | if d.db == nil { |
| 61 | return errors.New("connection not initialized") |
| 62 | } |
| 63 | |
| 64 | var builder strings.Builder |
| 65 | |
| 66 | databaseName := d.config.ConnectionContext.DatabaseName |
| 67 | database, err := d.SyncDBSchema(ctx) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if len(database.Schemas) == 0 { |
| 72 | return errors.New("database schemas is empty") |
| 73 | } |
| 74 | schema := database.Schemas[0] |
| 75 | |
| 76 | // Set current database |
| 77 | if _, err := d.db.ExecContext(ctx, fmt.Sprintf("USE %s", databaseName)); err != nil { |
| 78 | return errors.Wrap(err, "failed to set database") |
| 79 | } |
| 80 | |
| 81 | // Dump managed tables |
| 82 | for _, table := range schema.GetTables() { |
| 83 | tableDDL, err := d.showCreateDDL(ctx, "TABLE", table.Name) |
| 84 | if err != nil { |
| 85 | return errors.Wrapf(err, "failed to dump table %s", table.Name) |
| 86 | } |
| 87 | |
| 88 | // Dump indexes |
| 89 | for _, index := range table.GetIndexes() { |
| 90 | indexDDL, err := genIndexDDL(&IndexDDLOptions{ |
| 91 | databaseName: databaseName, |
| 92 | indexName: index.Name, |
| 93 | tableName: table.Name, |
| 94 | indexType: index.Type, |
| 95 | colNames: index.Expressions, |
| 96 | isWithDeferredRebuild: true, |
| 97 | idxProperties: nil, |
| 98 | indexTableName: "", |
| 99 | rowFmt: "", |
| 100 | storedAs: "", |
| 101 | storedBy: "", |
| 102 | location: "", |
| 103 | tableProperties: nil, |
| 104 | comment: index.Comment, |
| 105 | }) |
| 106 | if err != nil { |
| 107 | return errors.Wrapf(err, "failed to generate DDL for index %s", index.Name) |
| 108 | } |
| 109 | |
| 110 | _, _ = fmt.Fprintf(&builder, schemaStmtFmt, "INDEX", fmt.Sprintf("`%s`", index.Name), indexDDL) |
| 111 | } |
| 112 | _, _ = builder.WriteString(tableDDL) |
| 113 | } |
| 114 | |
| 115 | // Dump external tables |
| 116 | for _, extTable := range schema.GetExternalTables() { |
nothing calls this directly
no test coverage detected