|
| 1 | +# Devcontainer Setup for dm Package |
| 2 | + |
| 3 | +This directory contains the devcontainer configuration for the dm package development environment. |
| 4 | + |
| 5 | +## Files |
| 6 | + |
| 7 | +- `devcontainer.json` - Main devcontainer configuration |
| 8 | +- `docker-compose.yml` - Docker Compose file defining the development services |
| 9 | +- `test-postgres.R` - Script to test PostgreSQL connectivity |
| 10 | + |
| 11 | +## Services |
| 12 | + |
| 13 | +### PostgreSQL Database |
| 14 | + |
| 15 | +- **Image**: `postgres:latest` (latest PostgreSQL version) |
| 16 | +- **Container**: `devcontainer-postgres` |
| 17 | +- **Port**: 5432 |
| 18 | +- **Database**: test |
| 19 | +- **User**: compose |
| 20 | +- **Password**: YourStrong!Passw0rd |
| 21 | + |
| 22 | +### MariaDB Database |
| 23 | + |
| 24 | +- **Image**: `mariadb:latest` (latest MariaDB version) |
| 25 | +- **Container**: `devcontainer-mariadb` |
| 26 | +- **Port**: 3306 |
| 27 | +- **Database**: test |
| 28 | +- **User**: compose |
| 29 | +- **Password**: YourStrong!Passw0rd |
| 30 | + |
| 31 | +### SQL Server Database |
| 32 | + |
| 33 | +- **Image**: `mcr.microsoft.com/mssql/server:2022-latest` (SQL Server 2022 Express) |
| 34 | +- **Container**: `devcontainer-mssql` |
| 35 | +- **Port**: 1433 |
| 36 | +- **Database**: test (created automatically) |
| 37 | +- **User**: SA |
| 38 | +- **Password**: YourStrong!Passw0rd |
| 39 | + |
| 40 | +### Features |
| 41 | + |
| 42 | +- Socket connections enabled via shared volume (`/var/run/postgresql`) |
| 43 | +- Network connections available on standard ports (5432 for PostgreSQL, 3306 for MariaDB, 1433 for SQL Server) |
| 44 | +- Environment variables pre-configured for easy R connections |
| 45 | +- Health checks ensure databases are ready before starting dev environment |
| 46 | +- Service-specific host environment variables for targeted testing |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +1. Open the project in VS Code |
| 51 | +2. When prompted, reopen in devcontainer (or use Command Palette > "Dev Containers: Reopen in Container") |
| 52 | +3. All database services (PostgreSQL, MariaDB, SQL Server) will start automatically |
| 53 | +4. Test connectivity by running: `Rscript .devcontainer/test-postgres.R` |
| 54 | + |
| 55 | +### Running Tests with Different Backends |
| 56 | + |
| 57 | +```bash |
| 58 | +# Test with PostgreSQL (default) |
| 59 | +DM_TEST_SRC=postgres R -e 'testthat::test_local()' |
| 60 | + |
| 61 | +# Test with MariaDB |
| 62 | +DM_TEST_SRC=maria R -e 'testthat::test_local()' |
| 63 | + |
| 64 | +# Test with SQL Server (requires ODBC drivers) |
| 65 | +DM_TEST_SRC=mssql R -e 'testthat::test_local()' |
| 66 | + |
| 67 | +# Test with data frames (no database) |
| 68 | +DM_TEST_SRC=df R -e 'testthat::test_local()' |
| 69 | +``` |
| 70 | + |
| 71 | +## Database Connection in R |
| 72 | + |
| 73 | +The devcontainer environment supports multiple connection methods: |
| 74 | + |
| 75 | +### PostgreSQL |
| 76 | + |
| 77 | +```r |
| 78 | +# Using environment variables (recommended) |
| 79 | +con <- DBI::dbConnect(RPostgres::Postgres()) |
| 80 | + |
| 81 | +# Using explicit parameters |
| 82 | +con <- DBI::dbConnect( |
| 83 | + RPostgres::Postgres(), |
| 84 | + host = "postgres", |
| 85 | + port = 5432, |
| 86 | + user = "compose", |
| 87 | + password = "YourStrong!Passw0rd", |
| 88 | + dbname = "test" |
| 89 | +) |
| 90 | + |
| 91 | +# Using socket connection (if available) |
| 92 | +con <- DBI::dbConnect( |
| 93 | + RPostgres::Postgres(), |
| 94 | + host = "/var/run/postgresql", |
| 95 | + user = "compose", |
| 96 | + dbname = "test" |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +### MariaDB |
| 101 | + |
| 102 | +```r |
| 103 | +# Using explicit parameters |
| 104 | +con <- DBI::dbConnect( |
| 105 | + RMariaDB::MariaDB(), |
| 106 | + host = "mariadb", |
| 107 | + port = 3306, |
| 108 | + username = "compose", |
| 109 | + password = "YourStrong!Passw0rd", |
| 110 | + dbname = "test" |
| 111 | +) |
| 112 | +``` |
| 113 | + |
| 114 | +### SQL Server |
| 115 | + |
| 116 | +```r |
| 117 | +# Using explicit parameters (requires ODBC drivers) |
| 118 | +con <- DBI::dbConnect( |
| 119 | + odbc::odbc(), |
| 120 | + driver = "ODBC Driver 18 for SQL Server", |
| 121 | + server = "mssql", |
| 122 | + database = "test", |
| 123 | + uid = "SA", |
| 124 | + pwd = "YourStrong!Passw0rd", |
| 125 | + port = 1433, |
| 126 | + TrustServerCertificate = "yes" |
| 127 | +) |
| 128 | +``` |
| 129 | + |
| 130 | +## Environment Variables |
| 131 | + |
| 132 | +The following environment variables are set in the devcontainer: |
| 133 | + |
| 134 | +### PostgreSQL |
| 135 | + |
| 136 | +- `PGHOST=postgres` |
| 137 | +- `PGPORT=5432` |
| 138 | +- `PGUSER=compose` |
| 139 | +- `PGPASSWORD=YourStrong!Passw0rd` |
| 140 | +- `PGDATABASE=test` |
| 141 | +- `PGSOCKET=/var/run/postgresql` |
| 142 | + |
| 143 | +### MariaDB |
| 144 | + |
| 145 | +- `MYSQL_HOST=mariadb` |
| 146 | +- `MYSQL_PORT=3306` |
| 147 | +- `MYSQL_USER=compose` |
| 148 | +- `MYSQL_PASSWORD=YourStrong!Passw0rd` |
| 149 | +- `MYSQL_DATABASE=test` |
| 150 | + |
| 151 | +### SQL Server |
| 152 | + |
| 153 | +- `MSSQL_HOST=mssql` |
| 154 | +- `MSSQL_PORT=1433` |
| 155 | +- `MSSQL_USER=SA` |
| 156 | +- `MSSQL_PASSWORD=YourStrong!Passw0rd` |
| 157 | +- `MSSQL_DATABASE=test` |
| 158 | + |
| 159 | +### Testing |
| 160 | + |
| 161 | +- `DM_TEST_POSTGRES_HOST=postgres` (service-specific, takes precedence) |
| 162 | +- `DM_TEST_MARIA_HOST=mariadb` (service-specific, takes precedence) |
| 163 | +- `DM_TEST_MSSQL_HOST=mssql` (service-specific, takes precedence) |
| 164 | + |
| 165 | +## Troubleshooting |
| 166 | + |
| 167 | +1. **Database not starting**: Check if ports 5432 (PostgreSQL) or 3306 (MariaDB) are already in use on your host |
| 168 | +2. **Connection failures**: Run the test script to diagnose connection issues |
| 169 | +3. **Permission issues**: The PostgreSQL socket directory is configured with 0777 permissions |
| 170 | +4. **Container conflicts**: Ensure no other database containers are running with the same names |
| 171 | +5. **Environment variables not updated**: Use `devcontainer up --workspace-folder . --remove-existing-container` to recreate containers |
| 172 | + |
| 173 | +## Notes |
| 174 | + |
| 175 | +- Database data is persisted in Docker volumes (`postgres-data`, `mariadb-data`) |
| 176 | +- Socket connections are available for PostgreSQL through the shared `postgres-socket` volume |
| 177 | +- PostgreSQL includes `pg_stat_statements` extension for performance monitoring |
| 178 | +- All PostgreSQL SQL statements are logged for development debugging |
| 179 | +- MariaDB is configured with health checks to ensure availability before tests run |
| 180 | +- Service-specific environment variables allow targeted testing of individual database backends |
0 commit comments