Skip to content

Commit d62892c

Browse files
committed
Changed directory creation behavior
1 parent 77acc5c commit d62892c

File tree

7 files changed

+99
-38
lines changed

7 files changed

+99
-38
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ An alternative is to use [BashSupport Pro](https://plugins.jetbrains.com/plugin/
2727
Additionally, our [Bats](https://github.com/bats-core/bats-core) tests assume that you have a `doltgresql` (not `doltgres`) binary on your PATH.
2828
For Windows users, this means that the binary should _not_ end with the `.exe` file extension.
2929
Remember to recompile the executable on your PATH whenever you want to re-test any [Bats](https://github.com/bats-core/bats-core) tests.
30+
9. **Change the data directory**: This is optional but recommended.
31+
By default, we create databases within the `~/doltgres/databases` directory.
32+
For developmental purposes, you may want to change this behavior. You have two options:
33+
1. Set the `DOLTGRES_DATA_DIR_CWD` environment variable to `true`. Any value besides `true` will be interpreted as `false`. This causes DoltgreSQL to use the current directory as the data directory, so you can have multiple data directories simply by running the program in different directories. This behavior is more consistent with [Dolt's](https://github.com/dolthub/dolt) behavior. This is the recommended option for development. If this variable is set to `true`, then it overrides all other environment variables.
34+
2. Specify a new directory in the `DOLTGRES_DATA_DIR` environment variable. When this is empty, it uses the default directory.
3035

3136
### Note for Windows Users
3237

README.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,38 @@ If you are interested in using Doltgres now or in the future, please:
3535
* [Try Doltgres](#getting-started)
3636
* Create [issues](https://github.com/dolthub/doltgresql/issues) if you find bugs
3737
* Create [issues](https://github.com/dolthub/doltgresql/issues) for missing functionality you want
38-
* Contribute Code for features you want (see [Building From Source](#building-from-source))
39-
40-
Contribution Guide coming soon.
38+
* Contribute code for features you want (see the [Contribution Guide](https://github.com/dolthub/doltgresql/blob/main/CONTRIBUTING.md))
4139

4240
# Getting Started
4341

4442
1. Download the latest release of `doltgres`
4543

4644
2. Put `doltgres` on your `PATH`
4745

48-
3. Navigate to a directory you want your database data stored (ie. `~/doltgresql`).
49-
```bash
50-
$ mkdir ~/doltgresql
51-
$ cd ~/doltgresql
52-
```
53-
54-
4. Run `doltgres`. This will create a `doltgres` user and a `doltgres` database.
46+
3. Run `doltgres`. This will create a `doltgres` user and a `doltgres` database in `~/doltgres/databases` (change the `DOLTGRES_DATA_DIR` environment variable to use a different directory).
5547
```bash
5648
$ doltgres
5749
Successfully initialized dolt data repository.
5850
Starting server with Config HP="localhost:5432"|T="28800000"|R="false"|L="info"|S="/tmp/mysql.sock"
5951
```
6052

61-
5. Make sure you have Postgres version 15 or higher installed. I used Homebrew to install Postgres on my Mac.
53+
4. Make sure you have Postgres version 15 or higher installed. I used Homebrew to install Postgres on my Mac.
6254
This requires I manually add `/opt/homebrew/opt/postgresql@15/bin` to my path. On Postgres version 14 or lower,
63-
`\` commands (ie. `\d`, `\l`) do not work with Doltgres.
55+
`\` commands (ie. `\d`, `\l`) do not yet work with Doltgres.
6456
```
6557
export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"
6658
```
6759

68-
6. Open a new terminal. Connect with the following command: `psql -h localhost -U doltgres`. This will connect to the `doltgres` database with the `doltgres` user.
60+
5. Open a new terminal. Connect with the following command: `psql -h localhost -U doltgres`. This will connect to the `doltgres` database with the `doltgres` user.
6961
```bash
70-
$ psql -h 127.0.0.1 -U doltgres
62+
$ psql -h 127.0.0.1 -U doltgres
7163
psql (15.4 (Homebrew), server 15.0)
7264
Type "help" for help.
7365

7466
doltgres=>
7567
```
7668

77-
7. Create a `getting_started` database. Create the `getting_started` example tables.
69+
6. Create a `getting_started` database. Create the `getting_started` example tables.
7870
```sql
7971
doltgres=> create database getting_started;
8072
--
@@ -117,7 +109,7 @@ getting_started=> \d
117109
(3 rows)
118110
```
119111

120-
8. Make a Dolt Commit.
112+
7. Make a Dolt Commit.
121113
```sql
122114
getting_started=> select * from dolt_status;
123115
table_name | staged | status
@@ -147,7 +139,7 @@ getting_started=> call dolt_commit('-m', 'Created initial schema');
147139
(1 row)
148140
```
149141

150-
9. View the Dolt log.
142+
8. View the Dolt log.
151143
```
152144
getting_started=> select * from dolt_log;
153145
commit_hash | committer | email | date | message
@@ -157,17 +149,12 @@ getting_started=> select * from dolt_log;
157149
(2 rows)
158150
```
159151

160-
10. Continue with [Dolt Getting Started](https://docs.dolthub.com/introduction/getting-started/database#insert-some-data)
152+
9. Continue with [Dolt Getting Started](https://docs.dolthub.com/introduction/getting-started/database#insert-some-data)
161153
to test out more Doltgres versioning functionality.
162154

163155
# Building From Source
164156

165-
Due to the rapid pace of development at this early stage, building from source will guarantee that you're always working
166-
with the latest improvement and features.
167-
168-
1. Clone the repository to your local drive
169-
2. Run `./postgres/parser/build.sh` to generate the parser
170-
3. Run `go build .` in the root directory
157+
Please follow the [Contributor's Guide](https://github.com/dolthub/doltgresql/blob/main/CONTRIBUTING.md#getting-set-up) to learn how to build from source.
171158

172159
# Limitations
173160

main.go

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"os"
21+
"path/filepath"
2122
"strconv"
2223
"strings"
2324

@@ -58,11 +59,13 @@ func init() {
5859
}
5960
}
6061

61-
const chdirFlag = "--chdir"
62-
const stdInFlag = "--stdin"
63-
const stdOutFlag = "--stdout"
64-
const stdErrFlag = "--stderr"
65-
const stdOutAndErrFlag = "--out-and-err"
62+
const (
63+
chdirFlag = "--chdir"
64+
stdInFlag = "--stdin"
65+
stdOutFlag = "--stdout"
66+
stdErrFlag = "--stderr"
67+
stdOutAndErrFlag = "--out-and-err"
68+
)
6669

6770
func main() {
6871
ctx := context.Background()
@@ -80,7 +83,44 @@ func main() {
8083

8184
warnIfMaxFilesTooLow()
8285

83-
fs := filesys.LocalFS
86+
var fs filesys.Filesys
87+
if (len(args) >= 1 && args[0] == "init") || (strings.ToLower(os.Getenv(server.DOLTGRES_DATA_DIR_CWD)) == "true") {
88+
// If "init" is passed as the command, or DOLTGRES_DATA_DIR_CWD is set to "true", then we use the current directory
89+
fs = filesys.LocalFS
90+
} else {
91+
// We should use the directory as pointed to by "DOLTGRES_DATA_DIR", if has been set, otherwise we'll use the default
92+
var dbDir string
93+
if envDir := os.Getenv(server.DOLTGRES_DATA_DIR); len(envDir) > 0 {
94+
dbDir = envDir
95+
} else {
96+
homeDir, err := env.GetCurrentUserHomeDir()
97+
if err != nil {
98+
cli.PrintErrln(err.Error())
99+
os.Exit(1)
100+
}
101+
dbDir = filepath.Join(homeDir, server.DOLTGRES_DATA_DIR_DEFAULT)
102+
fileInfo, err := os.Stat(dbDir)
103+
if os.IsNotExist(err) {
104+
if err = os.MkdirAll(dbDir, 0755); err != nil {
105+
cli.PrintErrln(err.Error())
106+
os.Exit(1)
107+
}
108+
} else if err != nil {
109+
cli.PrintErrln(err.Error())
110+
os.Exit(1)
111+
} else if !fileInfo.IsDir() {
112+
cli.PrintErrln(fmt.Sprintf("Attempted to use the directory `%s` as the DoltgreSQL database directory, "+
113+
"however the preceding is a file and not a directory. Please change the environment variable `%s` so "+
114+
"that is points to a directory.", dbDir, server.DOLTGRES_DATA_DIR))
115+
os.Exit(1)
116+
}
117+
}
118+
fs, err = filesys.LocalFilesysWithWorkingDir(dbDir)
119+
if err != nil {
120+
cli.PrintErrln(err.Error())
121+
os.Exit(1)
122+
}
123+
}
84124
dEnv := env.Load(ctx, env.GetCurrentUserHomeDir, fs, doltdb.LocalDirDoltDB, server.Version)
85125

86126
globalConfig, _ := dEnv.Config.GetConfig(env.GlobalConfig)
@@ -136,8 +176,9 @@ func configureCliCtx(subcommand string, apr *argparser.ArgParseResults, fs files
136176
}
137177

138178
if dEnv.HasDoltDataDir() {
139-
return nil, fmt.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database." +
140-
"To use the current directory as a database, start the server from the parent directory.")
179+
cwd, _ := dEnv.FS.Abs(".")
180+
return nil, fmt.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database. "+
181+
"To use the current directory (%s) as a database, start the server from the parent directory.", cwd)
141182
}
142183

143184
err := reconfigIfTempFileMoveFails(dEnv)

server/server.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ import (
3838

3939
const (
4040
Version = "0.2.0"
41+
42+
// DOLTGRES_DATA_DIR is an environment variable that defines the location of DoltgreSQL databases
43+
DOLTGRES_DATA_DIR = "DOLTGRES_DATA_DIR"
44+
// DOLTGRES_DATA_DIR_DEFAULT is the portion to append to the user's home directory if DOLTGRES_DATA_DIR has not been specified
45+
DOLTGRES_DATA_DIR_DEFAULT = "doltgres/databases"
46+
// DOLTGRES_DATA_DIR_CWD is an environment variable that causes DoltgreSQL to use the current directory for the
47+
// location of DoltgreSQL databases, rather than the DOLTGRES_DATA_DIR. This means that it has priority over
48+
// DOLTGRES_DATA_DIR.
49+
DOLTGRES_DATA_DIR_CWD = "DOLTGRES_DATA_DIR_CWD"
4150
)
4251

4352
var sqlServerDocs = cli.CommandDocumentationContent{
@@ -152,8 +161,9 @@ func runServer(ctx context.Context, args []string, dEnv *env.DoltEnv) (*svcs.Con
152161
}
153162

154163
if dEnv.HasDoltDataDir() {
155-
return nil, fmt.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database." +
156-
"To use the current directory as a database, start the server from the parent directory.")
164+
cwd, _ := dEnv.FS.Abs(".")
165+
return nil, fmt.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database. "+
166+
"To use the current directory (%s) as a database, start the server from the parent directory.", cwd)
157167
}
158168

159169
defer tempfiles.MovableTempFileProvider.Clean()
@@ -206,11 +216,12 @@ func runServer(ctx context.Context, args []string, dEnv *env.DoltEnv) (*svcs.Con
206216
return nil, fmt.Errorf("failed to initialize doltgres database")
207217
}
208218
} else if !isDirectory {
219+
workingDir, _ := dEnv.FS.Abs(".")
209220
// The else branch means that there's a Doltgres item, so we need to error if it's a file since we
210221
// enforce the creation of a Doltgres database/directory, which would create a name conflict with the file
211-
return nil, fmt.Errorf("Attempted to create the default `doltgres` database, but a file with" +
212-
" the same name was found. Please run the doltgres command in a directory that does not contain a" +
213-
" file with the name doltgres")
222+
return nil, fmt.Errorf("Attempted to create the default `doltgres` database at `%s`, but a file with "+
223+
"the same name was found. Either remove the file, or change the environment variable `%s` so that it "+
224+
"points to a different directory.", workingDir, DOLTGRES_DATA_DIR)
214225
}
215226
}
216227

testing/bats/setup/common.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ setup_common() {
5656
# multiple tests can be run in parallel on the same machine
5757
mkdir "dolt-repo-$$"
5858
cd "dolt-repo-$$"
59+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p # This has to be set in every function that calls doltgresql
60+
nativevar DOLTGRES_DATA_DIR_CWD "" /w
5961

6062
mkdir "postgres"
6163
cd "postgres"

testing/bats/setup/query-server-common.bash

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ wait_for_connection() {
2929
start_sql_server() {
3030
DEFAULT_DB="$1"
3131
DEFAULT_DB="${DEFAULT_DB:=postgres}"
32+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
33+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p # This has to be set in every function that calls doltgresql
34+
nativevar DOLTGRES_DATA_DIR_CWD "" /w
3235
logFile="$2"
3336
PORT=$( definePORT )
3437
if [[ $logFile ]]
@@ -46,6 +49,9 @@ start_sql_server() {
4649
# this func)
4750
start_sql_server_with_args() {
4851
DEFAULT_DB=""
52+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
53+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p # This has to be set in every function that calls doltgresql
54+
nativevar DOLTGRES_DATA_DIR_CWD "" /w
4955
PORT=$( definePORT )
5056
doltgresql "$@" --port=$PORT &
5157
SERVER_PID=$!
@@ -55,6 +61,9 @@ start_sql_server_with_args() {
5561
start_sql_server_with_config() {
5662
DEFAULT_DB="$1"
5763
DEFAULT_DB="${DEFAULT_DB:=postgres}"
64+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
65+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p # This has to be set in every function that calls doltgresql
66+
nativevar DOLTGRES_DATA_DIR_CWD "" /w
5867
PORT=$( definePORT )
5968
echo "
6069
log_level: debug
@@ -79,6 +88,9 @@ behavior:
7988
start_sql_multi_user_server() {
8089
DEFAULT_DB="$1"
8190
DEFAULT_DB="${DEFAULT_DB:=postgres}"
91+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
92+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p # This has to be set in every function that calls doltgresql
93+
nativevar DOLTGRES_DATA_DIR_CWD "" /w
8294
PORT=$( definePORT )
8395
echo "
8496
log_level: debug
@@ -102,6 +114,9 @@ behavior:
102114
start_multi_db_server() {
103115
DEFAULT_DB="$1"
104116
DEFAULT_DB="${DEFAULT_DB:=postgres}"
117+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
118+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p # This has to be set in every function that calls doltgresql
119+
nativevar DOLTGRES_DATA_DIR_CWD "" /w
105120
PORT=$( definePORT )
106121
doltgresql --host 0.0.0.0 --port=$PORT --user postgres --data-dir ./ &
107122
SERVER_PID=$!

testing/bats/setup/windows-compat.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if [ -d "$WINDOWS_BASE_DIR"/Windows/System32 ] || [ "$IS_WINDOWS" == true ]; th
1717
}
1818
nativevar() {
1919
eval export "$1"="$2"
20-
export WSLENV="$1$3"
20+
export WSLENV="$WSLENV:$1$3"
2121
}
2222
skiponwindows() {
2323
skip "$1"

0 commit comments

Comments
 (0)