spicedb

package module
v0.0.0-...-58216e3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 17 Imported by: 0

README

go-migrate-spicedb

A go-migrate driver for spicedb. Allows using the go-migrate framework to push data or schema migrations.

Documentation

Index

Constants

View Source
const PresharedKey = "pre-shared-key"
View Source
const Scheme = "spicedb"
View Source
const WrappedConnStr = "wrapped-conn-str"

Variables

This section is empty.

Functions

func NewSpiceDBDriver

func NewSpiceDBDriver(wrappedDriver database.Driver, client client.Client, logger *log.Logger) database.Driver

Types

type SpiceDBDriver

type SpiceDBDriver struct {
	// contains filtered or unexported fields
}

SpiceDBDriver is a custom driver that wraps the PSQL driver. It reads/writes data to SpiceDB while also keeping migration state in PSQL. There are two types of migrations: Data migrations and Schema migrations.

Data migrations should be in CSV format of the form:

<operation>, <resource>, <relation>, <subject>

Both resource and subject strings should of the form: `<type>:<id>`. Valid data migration operations are `add` and `remove`.

Schema migrations can be intermixed with data migrations. A special operation `schema` will call WriteSchema:

schema, <path-to-schema>, <hex-encoded-md5sum>

A checksum is performed on the given schema file and it must match. To generate an md5 for a schema file:

  • `python3 -c 'import hashlib; print(hashlib.md5(open(input("File to encode: ")).read().encode()).hexdigest())'`

To instantiate the driver via the `Open(conn string)` API, you must pass two query args:

  1. `pre-shared-key` for authenticating with spicedb
  2. `wrapped-conn-str` which is a url encoded query string to pass to the wrapped database.Driver#Open constructor.

Additionally, you'll need to set the wrappedDriver to an empty struct via NewSpiceDBDriver. Ex: (encoded) spicedb://localhost:50051/?pre-shared-key=123&wrapper-conn-str=postgresql%3A%2F%2Fuser%3Apass%40localhost%3A50052%2Fdb

func (*SpiceDBDriver) Close

func (d *SpiceDBDriver) Close() error

Close closes the underlying database instance managed by the driver. Migrate will call this function only once per instance.

func (*SpiceDBDriver) Drop

func (d *SpiceDBDriver) Drop() error

Drop deletes everything in the database. Note that this is a breaking action, a new call to Open() is necessary to ensure subsequent calls work as expected.

func (*SpiceDBDriver) Lock

func (d *SpiceDBDriver) Lock() error

Lock should acquire a database lock so that only one migration process can run at a time. Migrate will call this function before Run is called. If the implementation can't provide this functionality, return nil. Return database.ErrLocked if database is already locked.

func (*SpiceDBDriver) Open

func (d *SpiceDBDriver) Open(connurl string) (database.Driver, error)

Open returns a new driver instance configured with parameters coming from the URL string. Migrate will call this function only once per instance.

func (*SpiceDBDriver) Run

func (d *SpiceDBDriver) Run(migration io.Reader) error

Run applies a migration to the database. migration is guaranteed to be not nil.

func (*SpiceDBDriver) RunMigration

func (d *SpiceDBDriver) RunMigration(line string) error

Exposed for testing

func (*SpiceDBDriver) SetVersion

func (d *SpiceDBDriver) SetVersion(version int, dirty bool) error

SetVersion saves version and dirty state. Migrate will call this function before and after each call to Run. version must be >= -1. -1 means NilVersion.

func (*SpiceDBDriver) Unlock

func (d *SpiceDBDriver) Unlock() error

Unlock should release the lock. Migrate will call this function after all migrations have been run.

func (*SpiceDBDriver) Version

func (d *SpiceDBDriver) Version() (version int, dirty bool, err error)

Version returns the currently active version and if the database is dirty. When no migration has been applied, it must return version -1. Dirty means, a previous migration failed and user interaction is required.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL