diff --git a/go/cmd/dolt/commands/engine/sqlengine.go b/go/cmd/dolt/commands/engine/sqlengine.go index 217662e98c3..a1f89b712ab 100644 --- a/go/cmd/dolt/commands/engine/sqlengine.go +++ b/go/cmd/dolt/commands/engine/sqlengine.go @@ -48,14 +48,15 @@ type SqlEngine struct { } type SqlEngineConfig struct { - InitialDb string - IsReadOnly bool - PrivFilePath string - ServerUser string - ServerPass string - Autocommit bool - Bulk bool - JwksConfig []JwksConfig + InitialDb string + IsReadOnly bool + IsServerLocked bool + PrivFilePath string + ServerUser string + ServerPass string + Autocommit bool + Bulk bool + JwksConfig []JwksConfig } // NewSqlEngine returns a SqlEngine @@ -67,7 +68,7 @@ func NewSqlEngine( ) (*SqlEngine, error) { if ok, _ := mrEnv.IsLocked(); ok { - config.IsReadOnly = true + config.IsServerLocked = true } parallelism := runtime.GOMAXPROCS(0) @@ -106,7 +107,7 @@ func NewSqlEngine( } // Set up engine - engine := gms.New(analyzer.NewBuilder(pro).WithParallelism(parallelism).Build(), &gms.Config{IsReadOnly: config.IsReadOnly, TemporaryUsers: tempUsers}).WithBackgroundThreads(bThreads) + engine := gms.New(analyzer.NewBuilder(pro).WithParallelism(parallelism).Build(), &gms.Config{IsReadOnly: config.IsReadOnly, TemporaryUsers: tempUsers, IsServerLocked: config.IsServerLocked}).WithBackgroundThreads(bThreads) engine.Analyzer.Catalog.MySQLDb.SetPersister(persister) engine.Analyzer.Catalog.MySQLDb.SetPlugins(map[string]mysql_db.PlaintextAuthPlugin{ "authentication_dolt_jwt": NewAuthenticateDoltJWTPlugin(config.JwksConfig), diff --git a/go/cmd/dolt/commands/sqlserver/server.go b/go/cmd/dolt/commands/sqlserver/server.go index 62b434ddec7..f8c7290021b 100644 --- a/go/cmd/dolt/commands/sqlserver/server.go +++ b/go/cmd/dolt/commands/sqlserver/server.go @@ -79,11 +79,6 @@ func Serve( } logrus.SetFormatter(LogFormat{}) - isReadOnly := false - if serverConfig.ReadOnly() { - isReadOnly = true - } - var mrEnv *env.MultiRepoEnv var err error fs := dEnv.FS @@ -134,7 +129,7 @@ func Serve( // Create SQL Engine with users config := &engine.SqlEngineConfig{ InitialDb: "", - IsReadOnly: isReadOnly, + IsReadOnly: serverConfig.ReadOnly(), PrivFilePath: serverConfig.PrivilegeFilePath(), ServerUser: serverConfig.User(), ServerPass: serverConfig.Password(), diff --git a/go/go.mod b/go/go.mod index de3857843b2..cce02c12bf2 100644 --- a/go/go.mod +++ b/go/go.mod @@ -58,7 +58,7 @@ require ( ) require ( - github.com/dolthub/go-mysql-server v0.12.1-0.20220725181120-4bb5cae174bc + github.com/dolthub/go-mysql-server v0.12.1-0.20220725212100-c3c1731b2a03 github.com/google/flatbuffers v2.0.6+incompatible github.com/gosuri/uilive v0.0.4 github.com/kch42/buzhash v0.0.0-20160816060738-9bdec3dec7c6 diff --git a/go/go.sum b/go/go.sum index 7a083be9385..39f82dc88e6 100644 --- a/go/go.sum +++ b/go/go.sum @@ -173,8 +173,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U= github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0= -github.com/dolthub/go-mysql-server v0.12.1-0.20220725181120-4bb5cae174bc h1:IJDBkqOLPI6CGPG1oQef5QSeOEuX1eQOtO1B1f7tfMs= -github.com/dolthub/go-mysql-server v0.12.1-0.20220725181120-4bb5cae174bc/go.mod h1:JgB3WpY0RMgyAda3YG5VHVncH2B8i1N9Mx9LOp41lIs= +github.com/dolthub/go-mysql-server v0.12.1-0.20220725212100-c3c1731b2a03 h1:ek99hhyjnf3CexTF8Y5Z4XyeOlzQgMlxvFrLKEroNiU= +github.com/dolthub/go-mysql-server v0.12.1-0.20220725212100-c3c1731b2a03/go.mod h1:JgB3WpY0RMgyAda3YG5VHVncH2B8i1N9Mx9LOp41lIs= github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371 h1:oyPHJlzumKta1vnOQqUnfdz+pk3EmnHS3Nd0cCT0I2g= github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371/go.mod h1:dhGBqcCEfK5kuFmeO5+WOx3hqc1k3M29c1oS/R7N4ms= github.com/dolthub/jsonpath v0.0.0-20210609232853-d49537a30474 h1:xTrR+l5l+1Lfq0NvhiEsctylXinUMFhhsqaEcl414p8= diff --git a/go/libraries/doltcore/mvdata/engine_table_writer.go b/go/libraries/doltcore/mvdata/engine_table_writer.go index 97adc93d31d..f7b58aeab34 100644 --- a/go/libraries/doltcore/mvdata/engine_table_writer.go +++ b/go/libraries/doltcore/mvdata/engine_table_writer.go @@ -65,6 +65,10 @@ type SqlEngineTableWriter struct { } func NewSqlEngineTableWriter(ctx context.Context, dEnv *env.DoltEnv, createTableSchema, rowOperationSchema schema.Schema, options *MoverOptions, statsCB noms.StatsCB) (*SqlEngineTableWriter, error) { + if dEnv.IsLocked() { + return nil, env.ErrActiveServerLock.New(dEnv.LockFile()) + } + mrEnv, err := env.MultiEnvForDirectory(ctx, dEnv.Config.WriteableConfig(), dEnv.FS, dEnv.Version, dEnv.IgnoreLockFile, dEnv) if err != nil { return nil, err diff --git a/integration-tests/bats/sql-server.bats b/integration-tests/bats/sql-server.bats index 10f3fdb1128..c04ab108607 100644 --- a/integration-tests/bats/sql-server.bats +++ b/integration-tests/bats/sql-server.bats @@ -167,7 +167,7 @@ SQL # attempt to create table (autocommit on), expect either some exception server_query repo1 1 "CREATE TABLE i_should_not_exist ( c0 INT - )" "" "not authorized" + )" "" "database server is set to read only mode" # Expect that there are still no tables run dolt ls @@ -205,7 +205,7 @@ SQL # make a dolt_commit query skip "read-only flag does not prevent dolt_commit" - server_query repo1 1 "select dolt_commit('--allow-empty', '-m', 'msg')" "" "not authorized: user does not have permission: write" + server_query repo1 1 "select dolt_commit('--allow-empty', '-m', 'msg')" "" "database server is set to read only mode: user does not have permission: write" } @test "sql-server: test command line modification" { @@ -1425,4 +1425,16 @@ databases: let PORT="$$ % (65536-1024) + 1024" run dolt sql-server -P $PORT [ "$status" -eq 1 ] -} \ No newline at end of file +} + +@test "sql-server: sql-server locks database to writes" { + cd repo2 + dolt sql -q "create table a (x int primary key)" + start_sql_server + run dolt sql -q "create table b (x int primary key)" + [ "$status" -eq 1 ] + [[ "$output" =~ "database is locked to writes" ]] || false + run dolt sql -q "insert into b values (0)" + [ "$status" -eq 1 ] + [[ "$output" =~ "database is locked to writes" ]] || false +}