Skip to content

Commit 94859da

Browse files
authored
Merge pull request #78 from dolthub/daylon/init-changes
Changed default database creation behavior
2 parents 77acc5c + 2780fef commit 94859da

File tree

9 files changed

+168
-44
lines changed

9 files changed

+168
-44
lines changed

.github/workflows/ci-staticcheck.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ jobs:
2222
working-directory: ./postgres/parser
2323
shell: bash
2424
- name: Run check
25-
run: |
26-
go install honnef.co/go/tools/cmd/staticcheck@2023.1.6
27-
staticcheck ./...
25+
run: ./run_staticcheck.sh
26+
working-directory: ./scripts

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` environment variable to a different directory. A value of `.` 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.
34+
2. Specify the directory in the `--data-dir` argument. This overrides the environment variable if it is present.
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` (add the `--data-dir` argument or 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: 118 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,12 @@ func main() {
8083

8184
warnIfMaxFilesTooLow()
8285

83-
fs := filesys.LocalFS
86+
var fs filesys.Filesys
87+
fs, args, err = loadFileSystem(args)
88+
if err != nil {
89+
cli.PrintErrln(err.Error())
90+
os.Exit(1)
91+
}
8492
dEnv := env.Load(ctx, env.GetCurrentUserHomeDir, fs, doltdb.LocalDirDoltDB, server.Version)
8593

8694
globalConfig, _ := dEnv.Config.GetConfig(env.GlobalConfig)
@@ -118,6 +126,107 @@ func main() {
118126
os.Exit(exitCode)
119127
}
120128

129+
// loadFileSystem loads the file system from a combination of the given arguments and environment variables.
130+
func loadFileSystem(args []string) (fs filesys.Filesys, outArgs []string, err error) {
131+
// We can't use the argument parser yet since it relies on the environment, so we'll handle the data directory
132+
// argument here. This will also remove it from the args, so that the Dolt layer doesn't try to move the directory
133+
// again (in the case of relative paths).
134+
var dataDir string
135+
var hasDataDirArgument bool
136+
for i, arg := range args {
137+
arg = strings.ToLower(arg)
138+
if arg == "--data-dir" {
139+
if len(args) <= i+1 {
140+
return nil, args, fmt.Errorf("--data-dir is missing the directory")
141+
}
142+
dataDir = args[i+1]
143+
hasDataDirArgument = true
144+
// os.Args is read from Dolt, so we have to update it, else we'll apply the move twice
145+
newArgs := make([]string, len(args)-2)
146+
copy(newArgs, args[:i])
147+
copy(newArgs[i:], args[i+2:])
148+
args = newArgs
149+
os.Args = os.Args[:len(args)+1]
150+
copy(os.Args[1:], args)
151+
break
152+
} else if strings.HasPrefix(arg, "--data-dir=") {
153+
dataDir = arg[11:]
154+
hasDataDirArgument = true
155+
// os.Args is read from Dolt, so we have to update it, else we'll apply the move twice
156+
newArgs := make([]string, len(args)-1)
157+
copy(newArgs, args[:i])
158+
copy(newArgs[i:], args[i+1:])
159+
args = newArgs
160+
os.Args = os.Args[:len(args)+1]
161+
copy(os.Args[1:], args)
162+
}
163+
}
164+
165+
if len(args) >= 1 && args[0] == "init" {
166+
// If "init" is passed as the command, then we use the current directory
167+
fs = filesys.LocalFS
168+
} else if hasDataDirArgument {
169+
// If the --data-dir argument was used, then we'll use the directory that it points to
170+
fileInfo, err := os.Stat(dataDir)
171+
if os.IsNotExist(err) {
172+
if err = os.MkdirAll(dataDir, 0755); err != nil {
173+
return nil, args, err
174+
}
175+
} else if err != nil {
176+
return nil, args, err
177+
} else if !fileInfo.IsDir() {
178+
return nil, args, fmt.Errorf("Attempted to use the directory `%s` as the DoltgreSQL database directory, "+
179+
"however the preceding is a file and not a directory. Please change the `--data-dir` argument so "+
180+
"that it points to a directory.", dataDir)
181+
}
182+
fs, err = filesys.LocalFilesysWithWorkingDir(dataDir)
183+
if err != nil {
184+
return nil, args, err
185+
}
186+
} else {
187+
// We should use the directory as pointed to by "DOLTGRES_DATA_DIR", if has been set, otherwise we'll use the default
188+
var dbDir string
189+
if envDir := os.Getenv(server.DOLTGRES_DATA_DIR); len(envDir) > 0 {
190+
dbDir = envDir
191+
fileInfo, err := os.Stat(dbDir)
192+
if os.IsNotExist(err) {
193+
if err = os.MkdirAll(dbDir, 0755); err != nil {
194+
return nil, args, err
195+
}
196+
} else if err != nil {
197+
return nil, args, err
198+
} else if !fileInfo.IsDir() {
199+
return nil, args, fmt.Errorf("Attempted to use the directory `%s` as the DoltgreSQL database directory, "+
200+
"however the preceding is a file and not a directory. Please change the environment variable `%s` so "+
201+
"that it points to a directory.", dbDir, server.DOLTGRES_DATA_DIR)
202+
}
203+
} else {
204+
homeDir, err := env.GetCurrentUserHomeDir()
205+
if err != nil {
206+
return nil, args, err
207+
}
208+
dbDir = filepath.Join(homeDir, server.DOLTGRES_DATA_DIR_DEFAULT)
209+
fileInfo, err := os.Stat(dbDir)
210+
if os.IsNotExist(err) {
211+
if err = os.MkdirAll(dbDir, 0755); err != nil {
212+
return nil, args, err
213+
}
214+
} else if err != nil {
215+
return nil, args, err
216+
} else if !fileInfo.IsDir() {
217+
return nil, args, fmt.Errorf("Attempted to use the directory `%s` as the DoltgreSQL database directory, "+
218+
"however the preceding is a file and not a directory. Please change the environment variable `%s` so "+
219+
"that it points to a directory.", dbDir, server.DOLTGRES_DATA_DIR)
220+
}
221+
}
222+
fs, err = filesys.LocalFilesysWithWorkingDir(dbDir)
223+
if err != nil {
224+
return nil, args, err
225+
}
226+
}
227+
return fs, args, nil
228+
}
229+
121230
func configureCliCtx(subcommand string, apr *argparser.ArgParseResults, fs filesys.Filesys, dEnv *env.DoltEnv, ctx context.Context) (cli.CliContext, error) {
122231
dataDir, hasDataDir := apr.GetValue(commands.DataDirFlag)
123232
if hasDataDir {
@@ -136,8 +245,9 @@ func configureCliCtx(subcommand string, apr *argparser.ArgParseResults, fs files
136245
}
137246

138247
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.")
248+
cwd, _ := dEnv.FS.Abs(".")
249+
return nil, fmt.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database. "+
250+
"To use the current directory (%s) as a database, start the server from the parent directory.", cwd)
141251
}
142252

143253
err := reconfigIfTempFileMoveFails(dEnv)

scripts/run_staticcheck.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Set the working directory to the directory of the script's location
4+
cd "$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
5+
cd ..
6+
7+
go run honnef.co/go/tools/cmd/staticcheck@2023.1.6 ./...

server/server.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ 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"
4146
)
4247

4348
var sqlServerDocs = cli.CommandDocumentationContent{
@@ -52,7 +57,7 @@ var sqlServerDocs = cli.CommandDocumentationContent{
5257
indentLines(sqlserver.ServerConfigAsYAMLConfig(sqlserver.DefaultServerConfig()).String()) + "\n\n" + `
5358
SUPPORTED CONFIG FILE FIELDS:
5459
55-
{{.EmphasisLeft}}data_dir{{.EmphasisRight}}: A directory where the server will load dolt databases to serve, and create new ones. Defaults to the current directory.
60+
{{.EmphasisLeft}}data_dir{{.EmphasisRight}}: A directory where the server will load dolt databases to serve, and create new ones. Defaults to the DOLTGRES_DATA_DIR environment variable, or {{.EmphasisLeft}}~/doltgres/databases{{.EmphasisRight}}.
5661
5762
{{.EmphasisLeft}}cfg_dir{{.EmphasisRight}}: A directory where the server will load and store non-database configuration data, such as permission information. Defaults {{.EmphasisLeft}}$data_dir/.doltcfg{{.EmphasisRight}}.
5863
@@ -152,8 +157,9 @@ func runServer(ctx context.Context, args []string, dEnv *env.DoltEnv) (*svcs.Con
152157
}
153158

154159
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.")
160+
cwd, _ := dEnv.FS.Abs(".")
161+
return nil, fmt.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database. "+
162+
"To use the current directory (%s) as a database, start the server from the parent directory.", cwd)
157163
}
158164

159165
defer tempfiles.MovableTempFileProvider.Clean()
@@ -206,11 +212,12 @@ func runServer(ctx context.Context, args []string, dEnv *env.DoltEnv) (*svcs.Con
206212
return nil, fmt.Errorf("failed to initialize doltgres database")
207213
}
208214
} else if !isDirectory {
215+
workingDir, _ := dEnv.FS.Abs(".")
209216
// The else branch means that there's a Doltgres item, so we need to error if it's a file since we
210217
// 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")
218+
return nil, fmt.Errorf("Attempted to create the default `doltgres` database at `%s`, but a file with "+
219+
"the same name was found. Either remove the file, change the directory using the `--data-dir` argument, "+
220+
"or change the environment variable `%s` so that it points to a different directory.", workingDir, DOLTGRES_DATA_DIR)
214221
}
215222
}
216223

testing/bats/setup/common.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ 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
5960

6061
mkdir "postgres"
6162
cd "postgres"

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ wait_for_connection() {
2929
start_sql_server() {
3030
DEFAULT_DB="$1"
3131
DEFAULT_DB="${DEFAULT_DB:=postgres}"
32+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
3233
logFile="$2"
3334
PORT=$( definePORT )
3435
if [[ $logFile ]]
3536
then
36-
doltgresql --host 0.0.0.0 --port=$PORT --user "${SQL_USER:-postgres}" > $logFile 2>&1 &
37+
doltgresql --host 0.0.0.0 --port=$PORT --data-dir=. --user "${SQL_USER:-postgres}" > $logFile 2>&1 &
3738
else
38-
doltgresql --host 0.0.0.0 --port=$PORT --user "${SQL_USER:-postgres}" &
39+
doltgresql --host 0.0.0.0 --port=$PORT --data-dir=. --user "${SQL_USER:-postgres}" &
3940
fi
4041
SERVER_PID=$!
4142
wait_for_connection $PORT 7500
@@ -46,6 +47,8 @@ start_sql_server() {
4647
# this func)
4748
start_sql_server_with_args() {
4849
DEFAULT_DB=""
50+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
51+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p
4952
PORT=$( definePORT )
5053
doltgresql "$@" --port=$PORT &
5154
SERVER_PID=$!
@@ -55,6 +58,8 @@ start_sql_server_with_args() {
5558
start_sql_server_with_config() {
5659
DEFAULT_DB="$1"
5760
DEFAULT_DB="${DEFAULT_DB:=postgres}"
61+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
62+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p
5863
PORT=$( definePORT )
5964
echo "
6065
log_level: debug
@@ -79,6 +84,8 @@ behavior:
7984
start_sql_multi_user_server() {
8085
DEFAULT_DB="$1"
8186
DEFAULT_DB="${DEFAULT_DB:=postgres}"
87+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
88+
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p
8289
PORT=$( definePORT )
8390
echo "
8491
log_level: debug
@@ -102,6 +109,7 @@ behavior:
102109
start_multi_db_server() {
103110
DEFAULT_DB="$1"
104111
DEFAULT_DB="${DEFAULT_DB:=postgres}"
112+
nativevar DEFAULT_DB "$DEFAULT_DB" /w
105113
PORT=$( definePORT )
106114
doltgresql --host 0.0.0.0 --port=$PORT --user postgres --data-dir ./ &
107115
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)