From b3667d60cdf6dcbb889631122d029baffc2efb85 Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Thu, 20 Aug 2020 21:08:59 -0600 Subject: [PATCH] implementing SQLDriver.AlterTableAddColumn (#65) --- drivers/mysql/metadata.go | 3 +-- drivers/mysql/mysql.go | 17 ++++++++++----- drivers/postgres/postgres.go | 17 ++++++++++----- drivers/sqlite3/sqlite3.go | 17 ++++++++++----- drivers/sqlite3/testdata/sakila.db | Bin 5849088 -> 5849088 bytes drivers/sqlserver/sqlserver.go | 17 ++++++++++----- libsq/driver/driver.go | 6 ++++-- libsq/driver/driver_test.go | 32 +++++++++++++++++++++++++++++ 8 files changed, 85 insertions(+), 24 deletions(-) diff --git a/drivers/mysql/metadata.go b/drivers/mysql/metadata.go index b5356877..f195a188 100644 --- a/drivers/mysql/metadata.go +++ b/drivers/mysql/metadata.go @@ -377,7 +377,7 @@ ORDER BY c.TABLE_NAME ASC, c.ORDINAL_POSITION ASC` tblMetas = append(tblMetas, curTblMeta) - rowCountTbl, rowCount, i := curTblName.String, &curTblMeta.RowCount, len(tblMetas) + rowCountTbl, rowCount, i := curTblName.String, &curTblMeta.RowCount, len(tblMetas)-1 gRowCount.Go(func() error { err := db.QueryRowContext(gctx, "SELECT COUNT(*) FROM `"+rowCountTbl+"`").Scan(rowCount) if err != nil { @@ -394,7 +394,6 @@ ORDER BY c.TABLE_NAME ASC, c.ORDINAL_POSITION ASC` } return nil }) - } col := &source.ColMetadata{ diff --git a/drivers/mysql/mysql.go b/drivers/mysql/mysql.go index 0bd7a23f..05fdd63d 100644 --- a/drivers/mysql/mysql.go +++ b/drivers/mysql/mysql.go @@ -102,6 +102,18 @@ func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel. return errz.Err(err) } +// AlterTableAddColumn implements driver.SQLDriver. +func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl string, col string, kind sqlz.Kind) error { + q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + dbTypeNameFromKind(kind) + + _, err := db.ExecContext(ctx, q) + if err != nil { + return errz.Wrapf(err, "alter table: failed to add column %q to table %q", col, tbl) + } + + return nil +} + // PrepareInsertStmt implements driver.SQLDriver. func (d *driveri) PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string, numRows int) (*driver.StmtExecer, error) { destColsMeta, err := d.getTableRecordMeta(ctx, db, destTbl, destColNames) @@ -316,11 +328,6 @@ func (d *driveri) Truncate(ctx context.Context, src *source.Source, tbl string, return beforeCount, errz.Err(tx.Commit()) } -// AlterTableAddColumn implements driver.Driver. -func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl string, col string, kind sqlz.Kind, ordinal int) error { - return errz.New("not implemented") -} - // database implements driver.Database. type database struct { log lg.Log diff --git a/drivers/postgres/postgres.go b/drivers/postgres/postgres.go index c27cca32..5d08ad28 100644 --- a/drivers/postgres/postgres.go +++ b/drivers/postgres/postgres.go @@ -157,6 +157,18 @@ func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel. return errz.Err(err) } +// AlterTableAddColumn implements driver.SQLDriver. +func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl string, col string, kind sqlz.Kind) error { + q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + dbTypeNameFromKind(kind) + + _, err := db.ExecContext(ctx, q) + if err != nil { + return errz.Wrapf(err, "alter table: failed to add column %q to table %q", col, tbl) + } + + return nil +} + // PrepareInsertStmt implements driver.SQLDriver. func (d *driveri) PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string, numRows int) (*driver.StmtExecer, error) { // Note that the pgx driver doesn't support res.LastInsertId. @@ -396,11 +408,6 @@ func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (sqlz.RecordMeta, drive return recMeta, mungeFn, nil } -// AlterTableAddColumn implements driver.Driver. -func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl string, col string, kind sqlz.Kind, ordinal int) error { - return errz.New("not implemented") -} - // database is the postgres implementation of driver.Database. type database struct { log lg.Log diff --git a/drivers/sqlite3/sqlite3.go b/drivers/sqlite3/sqlite3.go index a5e7a150..467aaf43 100644 --- a/drivers/sqlite3/sqlite3.go +++ b/drivers/sqlite3/sqlite3.go @@ -249,6 +249,18 @@ func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel. return errz.Err(stmt.Close()) } +// AlterTableAddColumn implements driver.SQLDriver. +func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl string, col string, kind sqlz.Kind) error { + q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + DBTypeForKind(kind) + + _, err := db.ExecContext(ctx, q) + if err != nil { + return errz.Wrapf(err, "alter table: failed to add column %q to table %q", col, tbl) + } + + return nil +} + // PrepareInsertStmt implements driver.SQLDriver. func (d *driveri) PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string, numRows int) (*driver.StmtExecer, error) { destColsMeta, err := d.getTableRecordMeta(ctx, db, destTbl, destColNames) @@ -356,11 +368,6 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri return colTypes, nil } -// AlterTableAddColumn implements driver.Driver. -func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl string, col string, kind sqlz.Kind, ordinal int) error { - return errz.New("not implemented") -} - func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName string, colNames []string) (sqlz.RecordMeta, error) { colTypes, err := d.TableColumnTypes(ctx, db, tblName, colNames) if err != nil { diff --git a/drivers/sqlite3/testdata/sakila.db b/drivers/sqlite3/testdata/sakila.db index d8c0d95498edc070cf8caf9faae609a3b8c6a722..9e4cf655ff3edd1b64dae4d5ee3d6cbe56b8247f 100644 GIT binary patch delta 653 zcmYMyO-~a+9LMq9?JltE!&((vSZc>wwbiPHqTn+Nu8J&uqG%cqwALP)AT?x7yl85W zcq~T4#;Y&Diy=L5@tqhGjo}S=^58e=8V{e$FTXj={3p}t{8T#FvcgeG(mwP_1Ck^? zdY8$`(bu)jn!H(4c50!|-&B1=-{^&l%F98&`z!Rdq<@hoBbKGSneyt7SDW>Q*Ir-A z1&h|5D?4_@5fyvZbwtpLl+jPECt~gS)8=}sA@YTa$S=FD$oQ{JIq5IO81Z|7$AxLj`_d-yiVE}^|f(`@wupb9-5GD>GjKesBqZr09L=eRYMqyzL zF&sx60w<8bNhEO!r*Q@;q;VGKkimIez(r&+jtS&2iA%VQDNN%EuHqW5;|6Zx7G_{$ z7IU}_2X}B6_b`w9Sb&Q~27{&4HwX4lnODoe%n+Kb;(aO@g&2yHx$|l+bNw^J+h*qIdaNi=@s0%mI zDtw6`itq)3HqoD2wD=u9IGoiNkH3p?B&|l1AV_zA1f3uVp&OS+V>*vqMl_cZF%{9? zzZ4Wc=`$JQns}yH6Cc_~_2F5xsNZIdU44~_>kqBfyT9jV{Z;LBiWdX1-M!<(*vY}c z&hCEfp$BKd3l@{=kyr_$^eQta-Z1=Cvq*1ReA!L=lQnf>M;B95M_rq5_pL z!Hgo VXGrD3^F2jLetnBX#uk;{