Documentation
  • 👋Welcome to Cerberus
  • Overview
    • 💡What we do
    • ✨Our Features
  • Product Guides
    • 💡Core concepts
    • 🍎Creating an App
    • 📎Modeling your domain
    • 📄Creating policies
  • Tutorial
    • 🛠️Getting set up
      • 🌠Cloning project
      • 👷Setting up workspace
      • 🏃‍♂️Build and run app
    • 📏Creating static rules
      • Create an account
      • Add migrations
    • ✍️Implementation
      • Backend
        • Migrate existing data
        • Routes
        • Services
          • User
          • Project
          • Sprint
          • Story
      • Frontend
        • Settings
        • Projects
        • Sprints
        • Stories
  • APIs
    • 🎨REST API
    • 🖥️Websocket API
  • Migrations
    • 🐧Scripting language
    • 🏃‍♂️Running migrations
Powered by GitBook
On this page
  1. Tutorial
  2. Creating static rules

Add migrations

PreviousCreate an accountNextImplementation

Last updated 2 years ago

At this point, you could add the Resource Types, Actions and Policies by hand, but we'll do it migrations for two reasons:

  • It's easier for this tutorial, and

  • For real apps, that change over time, this is best practice anyway.

Add these two files to a folder called 'cerberusmigrations' under 'backend/go':

Don't worry too much about the details of these files for now. Suffice it to say they act like normal database migrations, creating or deleting data for each migration version.

We always need four things:

  • Resource Types

  • Actions

  • Policies

  • Actions linked to Policies

The precise details of these four elements will depend on your domain and how you want to set up permissions in your product.

Add the following line to the top of the 'go.mod' file:

module cerberus-examples

go 1.18

replace github.com/golang-migrate/migrate/v4 => github.com/a11n-io/migrate/v4 v4.15.13

require (
	github.com/a11n-io/go-cerberus v0.3.39
	.
	.
	.

Next, add the following imports to 'cmd/api/api.go':

// Add imports here
cerberus "github.com/a11n-io/go-cerberus"
cerberusmigrate "github.com/golang-migrate/migrate/v4/database/cerberus"

And the following code to the main function in the same file:


// Add cerberus client and migration code here
cerberusClient := cerberus.NewClient(_env.CERBERUS_HOST, _env.CERBERUS_API_KEY, _env.CERBERUS_API_SECRET)

cdriver, err := cerberusmigrate.WithInstance(cerberusClient, &cerberusmigrate.Config{})
if err != nil {
	log.Fatalf("could not get cerberus driver: %v", err.Error())
}
cm, err := migrate.NewWithDatabaseInstance(
	"file://cerberusmigrations", "cerberus", cdriver)
if err != nil {
	log.Fatalf("could not get cerberus migrate: %v", err.Error())
} else {
	if err := cm.Up(); err != nil {
		log.Println(err)
	}
	log.Println("cerberus migration done")
}

Run the app again:

docker-compose build
docker-compose up

The console should report that the migration has run, and on the dashboard you should see one migration, and all the created Resource Types, Actions and Policies:

It's accomplished with a DSL (domain specific language) detailed in

📏
Migrations