Skip to content

Database Quick Reference Card โ€‹

Quick reference for common database operations in go-crypto-wallet project.

๐ŸŽฏ Common Workflows โ€‹

Schema Change Workflow โ€‹

bash
# 1. Edit HCL schema
vim tools/atlas/schemas/{db_dialect}/watch.hcl

# 2. Format and validate
make atlas-fmt && make atlas-lint

# 3. Regenerate migrations
make atlas-dev-reset

# 4. Apply to database
docker compose down -v && docker compose up -d wallet-mysql
make atlas-migrate-docker

# 5. Extract and convert schemas
make dump-schema-all
make extract-sqlc-schema-all
# Manually convert to SQLite/PostgreSQL

# 6. Regenerate code
make sqlc        # MySQL
make sqlc-sqlite # SQLite
make sqlc-postgres # PostgreSQL

# 7. Verify
make go-lint && make check-build && make go-test

Add New Query โ€‹

bash
# 1. Add query to SQL file
vim tools/sqlc/queries/mysql/address.sql

# 2. Regenerate code
make sqlc && make sqlc-sqlite

# 3. Use in repository
vim internal/infrastructure/repository/watch/mysql/address_sqlc.go

Database Reset โ€‹

bash
# Complete reset (all data lost)
docker compose down -v
docker compose up -d wallet-mysql
make atlas-migrate-docker

# Reset specific schema
docker compose exec wallet-mysql mysql -uroot -proot \
  -e "DROP DATABASE watch; CREATE DATABASE watch;"
make atlas-migrate-docker

๐Ÿ“ File Locations โ€‹

tools/atlas/
โ”œโ”€โ”€ schemas/              # โœ๏ธ  EDIT HERE - Source of truth
โ”‚   โ”œโ”€โ”€ watch.hcl
โ”‚   โ”œโ”€โ”€ keygen.hcl
โ”‚   โ””โ”€โ”€ sign.hcl
โ””โ”€โ”€ migrations/           # ๐Ÿ”’ AUTO-GENERATED - Do not edit
    โ”œโ”€โ”€ watch/*.sql
    โ”œโ”€โ”€ keygen/*.sql
    โ””โ”€โ”€ sign/*.sql

tools/sqlc/
โ”œโ”€โ”€ queries/
โ”‚   โ”œโ”€โ”€ mysql/            # โœ๏ธ  EDIT HERE - MySQL queries (? placeholders)
โ”‚   โ”‚   โ”œโ”€โ”€ address.sql
โ”‚   โ”‚   โ”œโ”€โ”€ btc_tx.sql
โ”‚   โ”‚   โ””โ”€โ”€ *.sql
โ”‚   โ””โ”€โ”€ postgres/       # โœ๏ธ  EDIT HERE - PostgreSQL queries ($1,$2 placeholders)
โ”‚       โ””โ”€โ”€ *.sql
โ”œโ”€โ”€ schemas/
โ”‚   โ”œโ”€โ”€ mysql/            # ๐Ÿ”„ EXTRACTED - From MySQL dump
โ”‚   โ”‚   โ”œโ”€โ”€ 01_watch.sql
โ”‚   โ”‚   โ”œโ”€โ”€ 02_keygen.sql
โ”‚   โ”‚   โ””โ”€โ”€ 03_sign.sql
โ”‚   โ”œโ”€โ”€ postgres/       # ๐Ÿ”„ EXTRACTED - From PostgreSQL dump
โ”‚   โ”‚   โ””โ”€โ”€ *.sql
โ”‚   โ””โ”€โ”€ sqlite/           # โœ๏ธ  CONVERTED - Manual type mapping
โ”‚       โ””โ”€โ”€ *.sql

internal/infrastructure/database/
โ”œโ”€โ”€ mysql/sqlcgen/        # ๐Ÿ”’ AUTO-GENERATED
โ”œโ”€โ”€ sqlite/sqlcgen/       # ๐Ÿ”’ AUTO-GENERATED
โ””โ”€โ”€ postgres/sqlcgen/   # ๐Ÿ”’ AUTO-GENERATED (coming soon)

Legend:

  • โœ๏ธ Manual editing allowed/required
  • ๐Ÿ”’ Auto-generated - Do not edit
  • ๐Ÿ”„ Extracted from database

โšก Make Commands โ€‹

Atlas (Schema Migrations) โ€‹

CommandDescription
make atlas-fmtFormat HCL schema files
make atlas-lintValidate HCL schema files
make atlas-dev-resetRegenerate all migrations from scratch
make atlas-migrateApply migrations (local)
make atlas-migrate-dockerApply migrations (Docker)
make atlas-statusShow migration status (local)
make atlas-status-dockerShow migration status (Docker)
make atlas-validateValidate migration files

SQLC (Code Generation) โ€‹

CommandDescription
make sqlcGenerate MySQL SQLC code
make sqlc-sqliteGenerate SQLite SQLC code
make sqlc-postgresGenerate PostgreSQL SQLC code
make sqlc-allGenerate code for all databases

Schema Extraction โ€‹

CommandDescription
make dump-schema-watchDump watch schema from MySQL
make dump-schema-keygenDump keygen schema from MySQL
make dump-schema-signDump sign schema from MySQL
make dump-schema-allDump all schemas from MySQL
make extract-sqlc-schema-allExtract SQLC-compatible schema files

Database Operations โ€‹

CommandDescription
docker compose up -d wallet-mysqlStart MySQL database
docker compose down -vStop and remove database (data lost)
docker compose exec wallet-mysql mysql -uroot -proot watchAccess watch schema
docker compose logs wallet-mysqlView database logs

๐Ÿ—„๏ธ Database Configuration โ€‹

MySQL (Production) โ€‹

toml
[database]
type = "mysql"

[database.mysql]
host = "127.0.0.1:3306"
dbname = "watch"  # or "keygen", "sign"
user = "hiromaily"
pass = "hiromaily"

SQLite (E2E Testing) โ€‹

toml
[database]
type = "sqlite"

[database.sqlite]
path = "./data/sqlite/btc/e2e.db"
debug = true

PostgreSQL (Coming Soon) โ€‹

toml
[database]
type = "postgres"

[database.postgres]
host = "127.0.0.1"
port = 5432
dbname = "watch"  # or "keygen", "sign"
user = "hiromaily"
pass = "hiromaily"
sslmode = "prefer"

๐Ÿ”„ Data Type Mapping โ€‹

ConceptMySQLSQLitePostgreSQL
Auto IDBIGINT AUTO_INCREMENTINTEGER AUTOINCREMENTBIGSERIAL
BooleanTINYINT(1)INTEGER (0/1)BOOLEAN
EnumENUM('a','b')TEXT CHECK(...)TEXT CHECK(...)
DecimalDECIMAL(26,10)TEXTNUMERIC(26,10)
TimestampDATETIMETEXT (ISO8601)TIMESTAMP
Text (sized)VARCHAR(255)TEXTVARCHAR(255)
Text (large)TEXTTEXTTEXT

๐Ÿงช Testing Commands โ€‹

bash
# Unit tests
make go-test

# Integration tests (MySQL)
make integration-test

# E2E tests (SQLite)
make btc-e2e-reset P=1

# E2E tests (MySQL)
make btc-e2e-reset P=1 DB=mysql

# Verify build
make go-lint
make check-build

๐Ÿ” Useful SQL Queries โ€‹

List All Tables โ€‹

bash
# MySQL
docker compose exec wallet-mysql mysql -uroot -proot watch -e "SHOW TABLES;"

# SQLite
sqlite3 ./data/sqlite/btc/e2e.db ".tables"

# PostgreSQL
docker compose exec wallet-db-postgres psql -U hiromaily -d watch -c "\dt"

Describe Table Structure โ€‹

bash
# MySQL
docker compose exec wallet-mysql mysql -uroot -proot watch -e "DESCRIBE address;"

# SQLite
sqlite3 ./data/sqlite/btc/e2e.db "PRAGMA table_info(address);"

# PostgreSQL
docker compose exec wallet-db-postgres psql -U hiromaily -d watch -c "\d address"

Check Migration Status โ€‹

bash
# MySQL
docker compose exec wallet-mysql mysql -uroot -proot watch \
  -e "SELECT * FROM atlas_schema_revisions ORDER BY version DESC LIMIT 5;"

# SQLite
sqlite3 ./data/sqlite/btc/e2e.db \
  "SELECT * FROM atlas_schema_revisions ORDER BY version DESC LIMIT 5;"

โŒ Common Mistakes to Avoid โ€‹

โŒ Don't Do Thisโœ… Do This Instead
Edit migration SQL filesEdit HCL schemas, regenerate migrations
Edit generated SQLC codeModify queries or schemas, regenerate code
Create MySQL-only schemasEnsure SQLite/PostgreSQL equivalents exist
Skip atlas-fmt and atlas-lintAlways format and validate before regenerating
Commit without testingRun full test cycle before commit
Use different column namesMaintain identical names across all databases
Modify only one databaseUpdate all three databases (MySQL, SQLite, PostgreSQL)

๐Ÿ†˜ Troubleshooting โ€‹

Migration Fails โ€‹

bash
# Check lint errors
make atlas-lint

# Reset and retry
docker compose down -v
docker compose up -d wallet-mysql
make atlas-dev-reset
make atlas-migrate-docker

SQLC Generation Fails โ€‹

bash
# Check schema syntax
docker compose exec wallet-mysql mysql -uroot -proot watch < tools/sqlc/schemas/mysql/01_watch.sql

# Run sqlc with verbose output
cd tools/sqlc && sqlc generate --experimental

Build Fails After Schema Change โ€‹

bash
# Ensure all code is regenerated
make sqlc
make sqlc-sqlite

# Verify imports and types
make go-lint
make check-build

Schema Mismatch Between Databases โ€‹

bash
# Compare schemas
diff -u tools/sqlc/schemas/mysql/01_watch.sql tools/sqlc/schemas/sqlite/01_watch.sql

# Verify data type conversions match the mapping table above

๐Ÿ“˜ For detailed explanations and complete workflows, see Database Schema Changes Guide