tests:
- name: persist global variable before server startup
  repos:
  - name: repo1
    with_files:
      - name: ".dolt/config.json"
        contents: |
          {"sqlserver.global.max_connections":"1000"}
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["1000"]]
- name: invalid persisted global variable name throws warning on server startup, but does not crash
  repos:
  - name: repo1
    with_files:
      - name: ".dolt/config.json"
        contents: |
          {"sqlserver.global.unknown":"1000"}
    server:
      args: ["-l", "trace"]
      log_matches:
      - "warning: persisted system variable unknown was not loaded since its definition does not exist."
  connections:
  - on: repo1
- name: invalid persisted global variable value throws warning on server startup, but does not crash
  repos:
  - name: repo1
    with_files:
      - name: ".dolt/config.json"
        contents: |
          {"sqlserver.global.max_connections":"string"}
    server:
      log_matches:
      - "error: failed to load persisted global variables: key: 'max_connections'; strconv.ParseInt: parsing \"string\": invalid syntax"
  connections:
  - on: repo1
- name: persisted global variable in server
  repos:
  - name: repo1
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - exec: "SET @@PERSIST.max_connections = 1000"
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["1000"]]
    restart_server: {}
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["1000"]]
- name: dolt_replicate_heads is global variable
  repos:
  - name: repo1
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - exec: "SET @@GLOBAL.dolt_replicate_heads = main"
    - query: "select @@GLOBAL.dolt_replicate_heads"
      result:
        columns: ["@@GLOBAL.dolt_replicate_heads"]
        rows: [["main"]]
    - query: "select @@SESSION.dolt_replicate_heads"
      result:
        columns: ["@@SESSION.dolt_replicate_heads"]
        # XXX: Is this OK? The session variable does not see the new global value; only new connections see it.
        rows: [[""]]
  - on: repo1
    queries:
    - query: "select @@SESSION.dolt_replicate_heads"
      result:
        columns: ["@@SESSION.dolt_replicate_heads"]
        rows: [["main"]]
    - query: "select @@dolt_replicate_heads"
      result:
        columns: ["@@dolt_replicate_heads"]
        rows: [["main"]]
- name: dolt_replicate_all_heads is global variable
  repos:
  - name: repo1
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - exec: "SET @@GLOBAL.dolt_replicate_all_heads = 1"
    - query: "select @@GLOBAL.dolt_replicate_all_heads"
      result:
        columns: ["@@GLOBAL.dolt_replicate_all_heads"]
        rows: [["1"]]
    - query: "select @@SESSION.dolt_replicate_all_heads"
      result:
        columns: ["@@SESSION.dolt_replicate_all_heads"]
        # XXX: Is this OK? The session variable does not see the new global value; only new connections see it.
        rows: [["0"]]
  - on: repo1
    queries:
    - query: "select @@SESSION.dolt_replicate_all_heads"
      result:
        columns: ["@@SESSION.dolt_replicate_all_heads"]
        rows: [["1"]]
    - query: "select @@dolt_replicate_all_heads"
      result:
        columns: ["@@dolt_replicate_all_heads"]
        rows: [["1"]]
- name: dolt_transaction_commit is global variable
  repos:
  - name: repo1
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - exec: "SET @@GLOBAL.dolt_transaction_commit = 1"
    - query: "select @@GLOBAL.dolt_transaction_commit"
      result:
        columns: ["@@GLOBAL.dolt_transaction_commit"]
        rows: [["1"]]
    - query: "select @@SESSION.dolt_transaction_commit"
      result:
        columns: ["@@SESSION.dolt_transaction_commit"]
        # XXX: Is this OK? The session variable does not see the new global value; only new connections see it.
        rows: [["0"]]
  - on: repo1
    queries:
    - query: "select @@SESSION.dolt_transaction_commit"
      result:
        columns: ["@@SESSION.dolt_transaction_commit"]
        rows: [["1"]]
    - query: "select @@dolt_transaction_commit"
      result:
        columns: ["@@dolt_transaction_commit"]
        rows: [["1"]]
    - query: "select count(*) from dolt_log"
      result:
        columns: ["count(*)"]
        rows: [["1"]]
    - exec: "create table tmp (i int)"
    - query: "select count(*) from dolt_log"
      result:
        columns: ["count(*)"]
        rows: [["2"]]
- name: persist only global variable during server session
  repos:
  - name: repo1
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - exec: "SET PERSIST max_connections = 1000"
    - exec: "SET PERSIST_ONLY max_connections = 7777"
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["1000"]]
    restart_server: {}
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["7777"]]
- name: persist invalid global variable name during server session
  repos:
  - name: repo1
    server:
      args: ["-l", "trace"]
  connections:
  - on: repo1
    queries:
    - exec: "SET @@PERSIST.unknown = 1000"
      error_match: "Unknown system variable"
- name: persist invalid global variable value during server session
  repos:
  - name: repo1
    server: {}
  connections:
  - on: repo1
    queries:
    - exec: "SET @@PERSIST.max_connections = 'string'"
      error_match: "Variable 'max_connections' can't be set to the value of 'string'"
- name: set max_connections with yaml config
  repos:
  - name: repo1
    with_files:
      - name: "server.yaml"
        contents: |
          log_level: trace
          listener:
            max_connections: 999
    server:
      args: ["--config", "server.yaml"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["999"]]
- name: persistence behavior set to load
  repos:
  - name: repo1
    server:
      args: ["--persistence-behavior", "load"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["151"]]
- name: persistence behavior set to ignore
  repos:
  - name: repo1
    server:
      args: ["--persistence-behavior", "ignore"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["100"]]
- name: persisted global variable defined on the command line
  repos:
  - name: repo1
    server:
      args: ["--max-connections", "555"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["555"]]
- name: persist global variable before server startup with persistence behavior with ignore
  repos:
  - name: repo1
    with_files:
      - name: ".dolt/config.json"
        contents: |
          {"sqlserver.global.max_connections":"999"}
    server:
      args: ["--persistence-behavior", "ignore"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["100"]]
- name: persisted global variable defined on the command line with persistence ignored
  repos:
  - name: repo1
    server:
      args: ["--max-connections", "555", "--persistence-behavior", "ignore"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.max_connections"]
        rows: [["555"]]
- name: "@@global.dolt_log_level behavior"
  repos:
  - name: repo1
    server:
      args: ["-l", "warning"]
      log_matches:
      - "Starting query"
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.dolt_log_level"
      result:
        columns: ["@@GLOBAL.dolt_log_level"]
        rows: [["warning"]]
    - exec: "set @@GLOBAL.dolt_log_level = 'trace'"
    - query: "select 2+2 from dual"
      result:
        columns: ["2+2"]
        rows: [["4"]]
    - query: "select @@GLOBAL.dolt_log_level"
      result:
        columns: ["@@GLOBAL.dolt_log_level"]
        rows: [["trace"]]
    restart_server:
      args: ["-l", "info"]
  - on: repo1
    queries:
    - query: "select @@GLOBAL.dolt_log_level"
      result:
        columns: ["@@GLOBAL.dolt_log_level"]
        rows: [["info"]]
- name: system variables in config.yaml can be read in show variables
  repos:
  - name: repo1
    with_files:
      - name: "config.yaml"
        contents: |
          system_variables:
            secure_file_priv: "/dev/null"
            max_connections: 1000
    server:
      args: ["-l", "trace", "--config", "config.yaml"]
  connections:
  - on: repo1
    queries:
    - query: "select @@GLOBAL.secure_file_priv, @@GLOBAL.max_connections"
      result:
        columns: ["@@GLOBAL.secure_file_priv", "@@GLOBAL.max_connections"]
        rows: [["/dev/null", "1000"]]
- name: secure_file_priv set to /dev/null prevents loading files
  repos:
  - name: repo1
    with_files:
      - name: "config.yaml"
        contents: |
          system_variables:
            secure_file_priv: "/dev/null"
    server:
      args: ["-l", "trace", "--config", "config.yaml"]
  connections:
  - on: repo1
    queries:
    - query: "select LOAD_FILE('config.yaml')"
      result:
        columns: ["LOAD_FILE('config.yaml')"]
        rows: [["NULL"]]
    - query: "select LOAD_FILE('/etc/passwd')"
      result:
        columns: ["LOAD_FILE('/etc/passwd')"]
        rows: [["NULL"]]
    - exec: "create table loaded (contents text)"
    - exec: "load data infile \"config.yaml\" into table loaded"
      error_match: "LOAD DATA is unable to open file: open /dev/null/config.yaml"
    - exec: "load data infile \"/config.yaml\" into table loaded"
      error_match: "LOAD DATA is unable to open file: open /dev/null/config.yaml"
- name: secure_file_priv set to empty strings allows loading files
  repos:
  - name: repo1
    with_files:
      - name: "config.yaml"
        contents: |
          system_variables:
            secure_file_priv: ""
    server:
      args: ["-l", "trace", "--config", "config.yaml"]
  connections:
  - on: repo1
    queries:
    - query: "select LOAD_FILE('config.yaml')"
      result:
        columns: ["LOAD_FILE('config.yaml')"]
        rows: [["system_variables:\n  secure_file_priv: \"\"\n"]]
    - query: "select LOAD_FILE('./.dolt/../config.yaml')"
      result:
        columns: ["LOAD_FILE('./.dolt/../config.yaml')"]
        rows: [["system_variables:\n  secure_file_priv: \"\"\n"]]
    - exec: "create table loaded (contents text)"
    - exec: "load data infile \"config.yaml\" into table loaded lines terminated by \"\\0\""
    - query: "select contents from loaded"
      result:
        columns: ["contents"]
        rows: [["system_variables:\n  secure_file_priv: \"\"\n"]]
