github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/sorted/sqlite/dbschema.go (about)

     1  /*
     2  Copyright 2012 The Camlistore Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlite
    18  
    19  import (
    20  	"bytes"
    21  	"log"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"runtime"
    26  	"strings"
    27  )
    28  
    29  const requiredSchemaVersion = 1
    30  
    31  func SchemaVersion() int {
    32  	return requiredSchemaVersion
    33  }
    34  
    35  func SQLCreateTables() []string {
    36  	return []string{
    37  		`CREATE TABLE rows (
    38   k VARCHAR(255) NOT NULL PRIMARY KEY,
    39   v VARCHAR(255))`,
    40  
    41  		`CREATE TABLE meta (
    42   metakey VARCHAR(255) NOT NULL PRIMARY KEY,
    43   value VARCHAR(255) NOT NULL)`,
    44  	}
    45  }
    46  
    47  // IsWALCapable checks if the installed sqlite3 library can
    48  // use Write-Ahead Logging (i.e version >= 3.7.0)
    49  func IsWALCapable() bool {
    50  	// TODO(mpl): alternative to make it work on windows
    51  	cmdPath, err := exec.LookPath("pkg-config")
    52  	if err != nil {
    53  		log.Printf("Could not find pkg-config to check sqlite3 lib version: %v", err)
    54  		return false
    55  	}
    56  	var stderr bytes.Buffer
    57  	cmd := exec.Command(cmdPath, "--modversion", "sqlite3")
    58  	cmd.Stderr = &stderr
    59  	if runtime.GOOS == "darwin" && os.Getenv("PKG_CONFIG_PATH") == "" {
    60  		matches, err := filepath.Glob("/usr/local/Cellar/sqlite/*/lib/pkgconfig/sqlite3.pc")
    61  		if err == nil && len(matches) > 0 {
    62  			cmd.Env = append(os.Environ(), "PKG_CONFIG_PATH="+filepath.Dir(matches[0]))
    63  		}
    64  	}
    65  
    66  	out, err := cmd.Output()
    67  	if err != nil {
    68  		log.Printf("Could not check sqlite3 version: %v\n", stderr.String())
    69  		return false
    70  	}
    71  	version := strings.TrimRight(string(out), "\n")
    72  	return version >= "3.7.0"
    73  }
    74  
    75  // EnableWAL returns the statement to enable Write-Ahead Logging,
    76  // which improves SQLite concurrency.
    77  // Requires SQLite >= 3.7.0
    78  func EnableWAL() string {
    79  	return "PRAGMA journal_mode = WAL"
    80  }