-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SurrealDB #917
Open
sam-kleiner
wants to merge
13
commits into
golang-migrate:master
Choose a base branch
from
sam-kleiner:surrealdb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SurrealDB #917
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
be51e63
add surrealdb
sam-kleiner 283c462
add tests for surrealdb
sam-kleiner 2f6d519
Merge branch 'master' into surrealdb
sam-kleiner c0f2656
update readme
sam-kleiner a4b58ec
Merge branch 'master' into surrealdb
sam-kleiner 046411e
bump surrealdb to v0.2.1
sam-kleiner 4a4f5fc
remove beta ref from readme
sam-kleiner bcdd2f1
update surrealdb docker version
sam-kleiner 3c6ec93
fix merge issue in go mod
sam-kleiner 69c3d88
Merge branch 'master' into surrealdb
sam-kleiner 6af7e6b
fix bad query error test
sam-kleiner 4264b2d
update surrealdb docker version
sam-kleiner e5e0692
fix lock race condition
sam-kleiner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# SurrealDB | ||
|
||
`surrealdb://username:password@host:port/namespace/database` (`surreal://` works, too) | ||
|
||
| URL Query | WithInstance Config | Description | | ||
|------------|---------------------|-------------| | ||
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table. Defaults to `schema_migrations`. | | ||
| `namespace` | `Namespace` | The namespace to connect to | | ||
| `database` | `DatabaseName` | The name of the database to connect to | | ||
| `user` | | The user to sign in as | | ||
| `password` | | The user's password | | ||
| `host` | | The host to connect to. | | ||
| `port` | | The port to bind to. (optional) | | ||
| `sslmode` | | Whether or not to use SSL (disable\|require) | | ||
|
||
## Notes | ||
|
||
* Uses the `github.com/surrealdb/surrealdb.go` surrealdb driver |
1 change: 1 addition & 0 deletions
1
database/surrealdb/examples/migrations/33_create_table.down.surql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
REMOVE TABLE user; |
15 changes: 15 additions & 0 deletions
15
database/surrealdb/examples/migrations/33_create_table.up.surql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
BEGIN; | ||
|
||
-- Create schemafull user table. | ||
DEFINE TABLE user SCHEMAFULL; | ||
|
||
-- Define some fields. | ||
DEFINE FIELD firstName ON TABLE user TYPE string | ||
ASSERT $value != NONE; | ||
DEFINE FIELD lastName ON TABLE user TYPE string | ||
ASSERT $value != NONE; | ||
DEFINE FIELD email ON TABLE user TYPE string | ||
ASSERT $value != NONE AND string::is::email($value); | ||
DEFINE INDEX userEmailIndex ON TABLE user COLUMNS email UNIQUE; | ||
|
||
COMMIT; |
1 change: 1 addition & 0 deletions
1
database/surrealdb/examples/migrations/44_alter_table.down.surql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
REMOVE INDEX userEmailIndex ON TABLE user; |
2 changes: 2 additions & 0 deletions
2
database/surrealdb/examples/migrations/44_alter_table.up.surql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- Make sure that email addresses in the user table are always unique | ||
DEFINE INDEX userEmailIndex ON TABLE user COLUMNS email UNIQUE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,299 @@ | ||
package surrealdb | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
nurl "net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/golang-migrate/migrate/v4/database" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/surrealdb/surrealdb.go" | ||
) | ||
|
||
func init() { | ||
database.Register("surreal", &SurrealDB{}) | ||
database.Register("surrealdb", &SurrealDB{}) | ||
} | ||
|
||
var DefaultMigrationsTable = "schema_migrations" | ||
var ( | ||
ErrNilConfig = fmt.Errorf("no config") | ||
) | ||
|
||
type Config struct { | ||
MigrationsTable string | ||
Namespace string | ||
DatabaseName string | ||
} | ||
|
||
func (c *Config) GetVersionDocumentId() (docId string) { | ||
return fmt.Sprintf("%s:version", c.MigrationsTable) | ||
} | ||
|
||
func (c *Config) GetLockDocumentId() (docId string) { | ||
return fmt.Sprintf("%s:lock", c.MigrationsTable) | ||
} | ||
|
||
type SurrealDB struct { | ||
db *surrealdb.DB | ||
config *Config | ||
} | ||
|
||
type DBInfo struct { | ||
DL map[string]string `json:"dl"` | ||
DT map[string]string `json:"dt"` | ||
FC map[string]string `json:"fc"` | ||
PA map[string]string `json:"pa"` | ||
SC map[string]string `json:"sc"` | ||
TB map[string]string `json:"tb"` | ||
} | ||
|
||
type VersionInfo struct { | ||
ID string `json:"id,omitempty"` | ||
Version int `json:"version,omitempty"` | ||
Dirty bool `json:"dirty,omitempty"` | ||
} | ||
|
||
type LockDoc struct { | ||
ID string `json:"id,omitempty"` | ||
Pid int `json:"pid,omitempty"` | ||
Hostname string `json:"hostname,omitempty"` | ||
CreatedAt string `json:"created_at,omitempty"` | ||
} | ||
|
||
func WithInstance(instance *surrealdb.DB, config *Config) (database.Driver, error) { | ||
if config == nil { | ||
return nil, ErrNilConfig | ||
} | ||
|
||
if _, err := instance.Info(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(config.MigrationsTable) == 0 { | ||
config.MigrationsTable = DefaultMigrationsTable | ||
} | ||
|
||
mx := &SurrealDB{ | ||
db: instance, | ||
config: config, | ||
} | ||
if err := mx.ensureVersionTable(); err != nil { | ||
return nil, err | ||
} | ||
return mx, nil | ||
} | ||
|
||
// ensureVersionTable checks if versions table exists and, if not, creates it. | ||
// Note that this function locks the database, which deviates from the usual | ||
// convention of "caller locks" in the SurrealDB type. | ||
func (m *SurrealDB) ensureVersionTable() (err error) { | ||
if err = m.Lock(); err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if e := m.Unlock(); e != nil { | ||
if err == nil { | ||
err = e | ||
} else { | ||
err = multierror.Append(err, e) | ||
} | ||
} | ||
}() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if _, _, err = m.Version(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (m *SurrealDB) Open(url string) (database.Driver, error) { | ||
purl, err := nurl.Parse(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
qv := purl.Query() | ||
|
||
migrationsTable := qv.Get("x-migrations-table") | ||
if len(migrationsTable) == 0 { | ||
migrationsTable = DefaultMigrationsTable | ||
} | ||
|
||
scheme := "wss" | ||
host := purl.Host | ||
path := strings.TrimPrefix(strings.TrimPrefix(purl.Path, "/rpc/"), "/") | ||
username := purl.User.Username() | ||
password, _ := purl.User.Password() | ||
|
||
if len(purl.Query().Get("sslmode")) > 0 { | ||
if purl.Query().Get("sslmode") == "disable" { | ||
scheme = "ws" | ||
} | ||
} | ||
|
||
split_path := strings.SplitN(path, "/", 2) | ||
namespace, database_name := split_path[0], split_path[1] | ||
|
||
if len(namespace) < 1 { | ||
return nil, fmt.Errorf("missing namespace in path: %s", path) | ||
} else if len(database_name) < 1 { | ||
return nil, fmt.Errorf("missing dataspace name in path: %s", path) | ||
} else if strings.Contains(namespace, "/") { | ||
return nil, fmt.Errorf("bad path: %s. Path should be in format '/namespace/database'", path) | ||
} else if strings.Contains(database_name, "/") { | ||
return nil, fmt.Errorf("bad path: %s. Path should be in format '/namespace/database'", path) | ||
} | ||
|
||
connUrl := fmt.Sprintf("%s://%s/rpc", scheme, host) | ||
|
||
db, err := surrealdb.New(connUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = db.Signin(map[string]interface{}{ | ||
"user": username, | ||
"pass": password, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = db.Use(namespace, database_name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mx, err := WithInstance(db, &Config{ | ||
Namespace: namespace, | ||
DatabaseName: database_name, | ||
MigrationsTable: migrationsTable, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return mx, nil | ||
} | ||
|
||
func (m *SurrealDB) Close() error { | ||
m.db.Close() | ||
return nil | ||
} | ||
|
||
func (m *SurrealDB) Drop() (err error) { | ||
query := `INFO FOR DB;` | ||
result, err := surrealdb.SmartUnmarshal[DBInfo](m.db.Query(query, map[string]interface{}{})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for tableName := range result.TB { | ||
query := fmt.Sprintf(`REMOVE TABLE %s;`, tableName) | ||
_, err := m.db.Query(query, map[string]interface{}{}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *SurrealDB) Lock() error { | ||
pid := os.Getpid() | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
hostname = fmt.Sprintf("Could not determine hostname. Error: %s", err.Error()) | ||
} | ||
|
||
lock_doc_id := m.config.GetLockDocumentId() | ||
query := `BEGIN; CREATE $lock_doc_id SET pid = $pid, hostname = $hostname, created_at = $created_at; RETURN AFTER; COMMIT;` | ||
|
||
// using m.db.Query looks to prevent a race condition that can occur when using m.db.Create | ||
// if you use m.db.Create its possible for a second lock call shortly after first to not error as it should | ||
_, err = surrealdb.SmartUnmarshal[[]LockDoc]( | ||
m.db.Query(query, map[string]interface{}{ | ||
"lock_doc_id": lock_doc_id, | ||
"pid": pid, | ||
"hostname": hostname, | ||
"created_at": time.Now().Format(time.RFC3339), | ||
}), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *SurrealDB) Unlock() error { | ||
lock_doc_id := m.config.GetLockDocumentId() | ||
query := `BEGIN; LET $lock = SELECT * FROM $lock_doc_id; DELETE $lock; COMMIT;` | ||
|
||
// Delete will error if lock_doc_id does not exist because $lock ends up as NONE | ||
_, err := m.db.Query(query, map[string]interface{}{"lock_doc_id": lock_doc_id}) | ||
return err | ||
} | ||
|
||
func (m *SurrealDB) Run(migration io.Reader) error { | ||
mig, err := io.ReadAll(migration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
query := string(mig[:]) | ||
_, err = m.db.Query(query, map[string]interface{}{}) | ||
return err | ||
} | ||
|
||
func (m *SurrealDB) SetVersion(version int, dirty bool) error { | ||
version_document_id := m.config.GetVersionDocumentId() | ||
params := map[string]interface{}{"version_document_id": version_document_id} | ||
query := `BEGIN; DELETE $version_document_id; ` | ||
|
||
// Also re-write the schema version for nil dirty versions to prevent | ||
// empty schema version for failed down migration on the first migration | ||
// See: https://github.com/golang-migrate/migrate/issues/330 | ||
if version >= 0 || (version == database.NilVersion && dirty) { | ||
params = map[string]interface{}{ | ||
"version_document_id": version_document_id, | ||
"version": version, | ||
"dirty": dirty, | ||
} | ||
query += `CREATE $version_document_id CONTENT { | ||
version: $version, | ||
dirty: $dirty | ||
}; ` | ||
} | ||
|
||
query += `COMMIT;` | ||
|
||
_, err := m.db.Query(query, params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *SurrealDB) Version() (version int, dirty bool, err error) { | ||
version_document_id := m.config.GetVersionDocumentId() | ||
|
||
query := fmt.Sprintf("SELECT * FROM %s;", version_document_id) | ||
versionInfo, err := surrealdb.SmartUnmarshal[[]VersionInfo](m.db.Query(query, map[string]interface{}{})) | ||
if err != nil { | ||
return database.NilVersion, false, err | ||
} else if len(versionInfo) == 0 { | ||
return database.NilVersion, false, nil | ||
} | ||
|
||
return versionInfo[0].Version, versionInfo[0].Dirty, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if the surrealdb go client supports multiple platforms?
My guess is that it does since it seems to be implemented in pure Go.
You can test this by running
make build-cli
or more comprehensively withDATABASE=$(make echo-database) SOURCE=$(make echo-source) goreleaser release --clean --snapshot --skip-docker
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that yes it supports multiple platforms here is the output of
DATABASE=$(make echo-database) SOURCE=$(make echo-source) goreleaser release --clean --snapshot --skip-docker
. That looks to have succeeded