Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Jun 8, 2021
0 parents commit cf5e093
Show file tree
Hide file tree
Showing 26 changed files with 5,078 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Editors
.idea

# sqlite databases
*.db
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Jacob LeGrone

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Temporalite

> ⚠️ This project is experimental and not suitable for production use. ⚠️
Temporalite is a distribution of [Temporal](https://github.com/temporalio/temporal) that runs as a single process with zero runtime dependencies.

Persistence to disk and an in-memory mode are both supported via SQLite.

## Why

The primary goal of Temporalite is to make it simple and fast to run Temporal locally or in testing environments.

Features that align with this goal:
- Easy setup and teardown
- Fast startup time
- Minimal resource overhead: no dependencies on a container runtime or database server
- Support for Windows, Linux, and macOS

## Getting Started

### Download and Start Temporal Server Locally

Build from source using [go install](https://golang.org/ref/mod#go-install):

```bash
go install github.com/DataDog/temporalite@latest
```

Start Temporal server:

```bash
temporalite start
```

### Use CLI

Use [Temporal's command line tool](https://docs.temporal.io/docs/system-tools/tctl) `tctl` to interact with the local Temporalite server.

```bash
tctl namespace list
tctl workflow list
```

## Configuration

Use the help flag to see all available options:

```bash
temporalite start -h
```

### Persistence Modes

#### File on Disk

By default `temporalite` persists state to a file in the [current user's config directory](https://pkg.go.dev/os#UserConfigDir). This path may be overridden:

```bash
temporalite start -f my_test.db
```

#### Ephemeral

An in-memory mode is also available. Note that all data will be lost on each restart.

```bash
temporalite start --ephemeral
```
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/DataDog/temporalite

go 1.16

require (
github.com/iancoleman/strcase v0.1.2
github.com/jmoiron/sqlx v1.2.1-0.20200615141059-0794cb1f47ee
github.com/mattn/go-sqlite3 v1.10.0
github.com/urfave/cli/v2 v2.3.0
go.temporal.io/api v1.4.1-0.20210420220407-6f00f7f98373
go.temporal.io/server v1.9.2
go.uber.org/zap v1.16.0
)
768 changes: 768 additions & 0 deletions go.sum

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions internal/common/persistence/sql/sqlplugin/sqlite/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package sqlite

import (
"fmt"
"time"
)

const (
readSchemaVersionQuery = `SELECT curr_version from schema_version where version_partition=0 and db_name=?`

writeSchemaVersionQuery = `REPLACE into schema_version(version_partition, db_name, creation_time, curr_version, min_compatible_version) VALUES (0,?,?,?,?)`

writeSchemaUpdateHistoryQuery = `INSERT into schema_update_history(version_partition, year, month, update_time, old_version, new_version, manifest_md5, description) VALUES(0,?,?,?,?,?,?,?)`

createSchemaVersionTableQuery = `CREATE TABLE schema_version(version_partition INT not null, ` +
`db_name VARCHAR(255) not null, ` +
`creation_time DATETIME(6), ` +
`curr_version VARCHAR(64), ` +
`min_compatible_version VARCHAR(64), ` +
`PRIMARY KEY (version_partition, db_name));`

createSchemaUpdateHistoryTableQuery = `CREATE TABLE schema_update_history(` +
`version_partition INT not null, ` +
`year int not null, ` +
`month int not null, ` +
`update_time DATETIME(6) not null, ` +
`description VARCHAR(255), ` +
`manifest_md5 VARCHAR(64), ` +
`new_version VARCHAR(64), ` +
`old_version VARCHAR(64), ` +
`PRIMARY KEY (version_partition, year, month, update_time));`

createDatabaseQuery = "CREATE DATABASE IF NOT EXISTS %v CHARACTER SET UTF8"

dropDatabaseQuery = "DROP DATABASE IF EXISTS %v"

listTablesQuery = "SELECT name FROM sqlite_master WHERE type='table'"

dropTableQuery = "DROP TABLE %v"
)

// CreateSchemaVersionTables sets up the schema version tables
func (mdb *db) CreateSchemaVersionTables() error {
if err := mdb.Exec(createSchemaVersionTableQuery); err != nil {
return err
}
return mdb.Exec(createSchemaUpdateHistoryTableQuery)
}

// ReadSchemaVersion returns the current schema version for the keyspace
func (mdb *db) ReadSchemaVersion(database string) (string, error) {
var version string
err := mdb.db.Get(&version, readSchemaVersionQuery, database)
return version, err
}

// UpdateSchemaVersion updates the schema version for the keyspace
func (mdb *db) UpdateSchemaVersion(database string, newVersion string, minCompatibleVersion string) error {
return mdb.Exec(writeSchemaVersionQuery, database, time.Now().UTC(), newVersion, minCompatibleVersion)
}

// WriteSchemaUpdateLog adds an entry to the schema update history table
func (mdb *db) WriteSchemaUpdateLog(oldVersion string, newVersion string, manifestMD5 string, desc string) error {
now := time.Now().UTC()
return mdb.Exec(writeSchemaUpdateHistoryQuery, now.Year(), int(now.Month()), now, oldVersion, newVersion, manifestMD5, desc)
}

// Exec executes a sql statement
func (mdb *db) Exec(stmt string, args ...interface{}) error {
_, err := mdb.db.Exec(stmt, args...)
return err
}

// ListTables returns a list of tables in this database
func (mdb *db) ListTables(database string) ([]string, error) {
var tables []string
err := mdb.db.Select(&tables, listTablesQuery)
return tables, err
}

// DropTable drops a given table from the database
func (mdb *db) DropTable(name string) error {
return mdb.Exec(fmt.Sprintf(dropTableQuery, name))
}

// DropAllTables drops all tables from this database
func (mdb *db) DropAllTables(database string) error {
tables, err := mdb.ListTables(database)
if err != nil {
return err
}
for _, tab := range tables {
if err := mdb.DropTable(tab); err != nil {
return err
}
}
return nil
}

// CreateDatabase creates a database if it doesn't exist
func (mdb *db) CreateDatabase(name string) error {
return mdb.Exec(fmt.Sprintf(createDatabaseQuery, name))
}

// DropDatabase drops a database
func (mdb *db) DropDatabase(name string) error {
return mdb.Exec(fmt.Sprintf(dropDatabaseQuery, name))
}

0 comments on commit cf5e093

Please sign in to comment.