feat: PostgreSQL driver support

This commit is contained in:
fsylvain committed 2026-10-06 13:35:10 +02:00
1 parent eb9bfabed5
commit dc5d12348c
24 files changed
+1585 -75

No files matched your search

@@ -17,18 +17,44 @@ func NewMySQLBackup(db *gorm.DB, directory string, now func() time.Time) *DumpBa
return NewDumpBackup(func() (DumpSource, error) { return openMySQLDumpSource(db) }, directory, now)
}
type txDumpSource struct {
tx *gorm.DB
quote func(name string) string
convert func(value any) []byte
}
func openTxDumpSource(db *gorm.DB, quote func(name string) string, convert func(value any) []byte) (*txDumpSource, error) {
tx := db.Begin(&sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true})
if tx.Error != nil {
return nil, tx.Error
}
return &txDumpSource{tx: tx, quote: quote, convert: convert}, nil
}
func (s *txDumpSource) Rows(table string) (DumpRows, error) {
rows, err := s.tx.Raw("SELECT * FROM " + s.quote(table)).Rows()
if err != nil {
return nil, err
}
return &sqlDumpRows{rows: rows, convert: s.convert}, nil
}
func (s *txDumpSource) Close() error {
return s.tx.Rollback().Error
}
type mysqlDumpSource struct {
tx *gorm.DB
*txDumpSource
}
var _ DumpSource = &mysqlDumpSource{}
func openMySQLDumpSource(db *gorm.DB) (*mysqlDumpSource, error) {
tx := db.Begin(&sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true})
if tx.Error != nil {
return nil, tx.Error
source, err := openTxDumpSource(db, quoteIdentifier, dumpBytes)
if err != nil {
return nil, err
}
return &mysqlDumpSource{tx: tx}, nil
return &mysqlDumpSource{source}, nil
}
func (s *mysqlDumpSource) Tables() ([]string, error) {
@@ -46,25 +72,14 @@ func (s *mysqlDumpSource) CreateStatement(table string) (string, error) {
return statement, nil
}
func (s *mysqlDumpSource) Rows(table string) (DumpRows, error) {
rows, err := s.tx.Raw("SELECT * FROM " + quoteIdentifier(table)).Rows()
if err != nil {
return nil, err
}
return &mysqlDumpRows{rows: rows}, nil
type sqlDumpRows struct {
rows *sql.Rows
convert func(value any) []byte
}
func (s *mysqlDumpSource) Close() error {
return s.tx.Rollback().Error
}
var _ DumpRows = &sqlDumpRows{}
type mysqlDumpRows struct {
rows *sql.Rows
}
var _ DumpRows = &mysqlDumpRows{}
func (r *mysqlDumpRows) Columns() ([]DumpColumn, error) {
func (r *sqlDumpRows) Columns() ([]DumpColumn, error) {
types, err := r.rows.ColumnTypes()
if err != nil {
return nil, err
@@ -76,11 +91,11 @@ func (r *mysqlDumpRows) Columns() ([]DumpColumn, error) {
return columns, nil
}
func (r *mysqlDumpRows) Next() bool {
func (r *sqlDumpRows) Next() bool {
return r.rows.Next()
}
func (r *mysqlDumpRows) Values() ([][]byte, error) {
func (r *sqlDumpRows) Values() ([][]byte, error) {
columnCount, err := r.rows.Columns()
if err != nil {
return nil, err
@@ -95,16 +110,16 @@ func (r *mysqlDumpRows) Values() ([][]byte, error) {
}
values := make([][]byte, len(scanned))
for index, value := range scanned {
values[index] = dumpBytes(value)
values[index] = r.convert(value)
}
return values, nil
}
func (r *mysqlDumpRows) Err() error {
func (r *sqlDumpRows) Err() error {
return r.rows.Err()
}
func (r *mysqlDumpRows) Close() error {
func (r *sqlDumpRows) Close() error {
return r.rows.Close()
}