Last active
November 7, 2024 21:58
-
-
Save robstradling/e6685c10534ca21bb10b2871c8a154c0 to your computer and use it in GitHub Desktop.
Duplicate MySQL backend, then search'n'replace a bunch of stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
git checkout -b duplicate_mysql_to_postgresql | |
# Duplicate the MySQL quota.Manager and storage layer files into new PostgreSQL directories, preserving git line history. | |
# (h/t https://devblogs.microsoft.com/oldnewthing/20190919-00/?p=102904) | |
mkdir quota/postgresqlqm | |
git mv quota/mysqlqm/mysql_quota.go quota/postgresqlqm/postgresql_quota.go | |
git mv quota/mysqlqm/mysql_quota_test.go quota/postgresqlqm/postgresql_quota_test.go | |
git mv quota/mysqlqm/quota_provider.go quota/postgresqlqm | |
mkdir storage/postgresql | |
git mv storage/mysql/* storage/postgresql | |
git mv storage/postgresql/mysqlpb storage/postgresql/postgresqlpb | |
mkdir storage/postgresql/testdbpgx | |
git mv storage/testdb/testdb.go storage/postgresql/testdbpgx/testdbpgx.go | |
git mv storage/testdb/testdb_test.go storage/postgresql/testdbpgx/testdbpgx_test.go | |
git commit -m "Duplicate MySQL files to new PostgreSQL directories, preserving git line history" | |
git checkout HEAD~ quota/mysqlqm storage/mysql storage/testdb | |
git commit -m "Restore MySQL files" | |
git checkout - | |
git checkout -b postgresql_support | |
git merge --no-ff --no-edit duplicate_mysql_to_postgresql | |
git branch -D duplicate_mysql_to_postgresql | |
# Replace "MySQL" references with "PostgreSQL", preserving case of each reference. | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/mysql/postgresql/g" '{}' ';' | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/MySQL/PostgreSQL/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/mysql/postgresql/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/MySQL/PostgreSQL/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/mySQL/postgreSQL/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/Mysql/Postgresql/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/MYSQL/POSTGRESQL/g" '{}' ';' | |
sed -i "s/MySQL/PostgreSQL/g" storage/postgresql/schema/storage.sql | |
sed -i "s/MySQL/PostgreSQL/g" storage/postgresql/postgresqlpb/options.proto | |
sed -i "s/mysql/postgresql/g" storage/postgresql/postgresqlpb/options.proto | |
git commit -a -m "Replace 'MySQL' references with 'PostgreSQL', preserving case of each reference" | |
# Build the PostgreSQL protobuf definitions. | |
cd storage/postgresql/postgresqlpb | |
go generate | |
cd ../../.. | |
git commit -a -m "Build the PostgreSQL protobuf definitions" | |
# Rename PostgreSQL "testdb" package to "testdbpgx". | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/\"github.com\/google\/trillian\/storage\/testdb/testdb \"github.com\/google\/trillian\/storage\/postgresql\/testdbpgx/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/\"github.com\/google\/trillian\/storage\/testdb/testdb \"github.com\/google\/trillian\/storage\/postgresql\/testdbpgx/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/package testdb/package testdbpgx/g" '{}' ';' | |
git commit -a -m "Rename PostgreSQL 'testdb' package to 'testdbpgx'" | |
# Convert from database/sql API to jackc/pgx/v5 API. | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/sql\.DB/pgxpool\.Pool/g" '{}' ';' # "sql.DB" -> "pgxpool.Pool". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/sql\.DB/pgxpool\.Pool/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/sql\.Open/pgxpool\.New/g" '{}' ';' # "sql.Open" -> "pgxpool.New". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/sql\.Result/pgconn\.CommandTag/g" '{}' ';' # "sql.Result" -> "pgconn.CommandTag". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/\*sql\.Rows/pgx\.Rows/g" '{}' ';' # "*sql.Rows" -> "pgx.Rows". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/\*sql\.Tx/pgx\.Tx/g" '{}' ';' # "*sql.Tx" -> "pgx.Tx". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/sql\.ErrNoRows/pgx\.ErrNoRows/g" '{}' ';' # "sql.ErrNoRows" -> "pgx.ErrNoRows". | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/ExecContext/Exec/g" '{}' ';' # "ExecContext" -> "Exec". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/ExecContext/Exec/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/PingContext/Ping/g" '{}' ';' # "PingContext" -> "Ping". | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/QueryContext/Query/g" '{}' ';' # "QueryContext" -> "Query". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/QueryContext/Query/g" '{}' ';' | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/QueryRowContext/QueryRow/g" '{}' ';' # "QueryRowContext" -> "QueryRow". | |
find storage/postgresql -iname "*.go" -exec sed -i "s/QueryRowContext/QueryRow/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/nil \/\* opts \*\//pgx.TxOptions\{\}/g" '{}' ';' # "nil /* opts */" -> "pgx.TxOptions{}". | |
git commit -a -m "Convert from database/sql API to jackc/pgx/v5 API" | |
# Update imports. | |
echo "Using gopls to update imports. Please wait..." | |
find storage/postgresql -iname "*.go" -exec sed -i "s/go-sql-driver\/postgresql/jackc\/pgx\/v5\/pgxpool/g" '{}' ';' | |
sed -i "s/build batched_queue/IGNOREbuild batched_queue/g" storage/postgresql/queue_batching.go | |
find quota/postgresqlqm -iname "*.go" -exec gopls imports -w '{}' ';' 2>/dev/null # These first two 'gopls imports' are only partially successful and emit some errors. | |
find storage/postgresql -iname "*.go" -exec gopls imports -w '{}' ';' 2>/dev/null | |
find quota/postgresqlqm -iname "*.go" -exec sed -i "s/jackc\/pgx\/v4\/pgxpool/jackc\/pgx\/v5\/pgxpool/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/jackc\/pgx\/v4\/pgxpool/jackc\/pgx\/v5\/pgxpool/g" '{}' ';' | |
find storage/postgresql -iname "*.go" -exec sed -i "s/jackc\/pgx\"/jackc\/pgx\/v5\"/g" '{}' ';' | |
go mod tidy | |
find quota/postgresqlqm -iname "*.go" -exec gopls imports -w '{}' ';' # Repeating the 'gopls imports' should succeed without errors. | |
find storage/postgresql -iname "*.go" -exec gopls imports -w '{}' ';' | |
sed -i "s/IGNOREbuild batched_queue/build batched_queue/g" storage/postgresql/queue_batching.go | |
git commit -a -m "Update imports" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment