Skip to content

Commit

Permalink
doc/md: document project files (#810)
Browse files Browse the repository at this point in the history
* doc/md: document project files

* h3

* Update doc/md/cli/projects.md

Co-authored-by: Ariel Mashraki <[email protected]>

* Update doc/md/cli/projects.md

Co-authored-by: Ariel Mashraki <[email protected]>

* Update doc/md/cli/projects.md

Co-authored-by: Ariel Mashraki <[email protected]>

* Update doc/md/cli/projects.md

Co-authored-by: Ariel Mashraki <[email protected]>

Co-authored-by: Ariel Mashraki <[email protected]>
  • Loading branch information
rotemtam and a8m authored May 19, 2022
1 parent 2c66a96 commit 634540f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
124 changes: 124 additions & 0 deletions doc/md/cli/projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: Project Structure
id: projects
slug: /cli/projects
---
### Project Files

Project files provide a convenient way to describe and interact with multiple
environments when working with Atlas. A project file is a file named
`atlas.hcl` and contains one or more `env` blocks. For example:

```hcl
// Define an environment named "local"
env "local" {
// Declare where the schema definition file resides.
src = "./schema/project.hcl"
// Define the URL of the database which is managed in
// this environment.
url = "mysql://localhost:3306"
// Define the URL of the Dev Database for this environment
// See: https://atlasgo.io/dev-database
dev = "mysql://localhost:3307"
// The schemas in the database that are managed by Atlas.
schemas = ["users", "admin"]
}
env "dev" {
// ... a different env
}
```

Once defined, a project's environment can be worked against using the `--env` flag.
For example:

```shell
atlas schema apply --env local
```

Will run the `schema apply` command against the database that is defined for the `local`
environment.

### Projects with Versioned Migrations

Environments may declare a `migration_dir` block to configure how versioned migrations
work in the specific environment:

```hcl
env "local" {
// ..
migration_dir {
// URL where the migration directory resides. Only filesystem directories
// are currently supported but more options will be added in the future.
url = "file://migrations"
// Format of the migration directory: atlas | flyway | liquibase | goose | golang-migrate
format = atlas
}
}
```

Once defined, `migrate` commands can use this configuration, for example:
```shell
$ atlas migrate validate --env local
```
Will run the `migrate validate` command against the Dev Database defined in the
`local` environment.

### Passing Input Values

Project files may pass [input values](/ddl/input-variables) to variables defined in
the Atlas schema of the environment. To do this simply provide additional attributes
to the environment block:
```hcl
env "local" {
url = "sqlite://test?mode=memory&_fk=1"
src = "./schema.hcl"
// Other attributes are passed as input values to "schema.hcl":
tenant = "rotemtam"
}
```

These values can then be consumed by variables defined in the schema file:

```hcl
variable "tenant" {
type = string
}
schema "main" {
name = var.tenant
}
```

### Project Input Variables

Project files may also declare [input variables](../ddl/input.md) that can be supplied to the CLI
at runtime. For example:

```hcl
variable "tenant" {
type = string
}
env "local" {
url = "sqlite://test?mode=memory&_fk=1"
src = "./schema.hcl"
// The value for "tenant" is determined by the user at runtime.
tenant = var.tenant
}
```
To set the value for this variable at runtime, use the `--var` flag:

```shell
$ atlas schema apply --env local --var tenant=rotemtam
```

It is worth mentioning that when running Atlas commands within a project using
the `--env` flag, all input values supplied at the command-line are passed only
to the project file, and not propagated automatically to children schema files.
This is done with the purpose of creating an explicit contract between the environment
and the schema file.
1 change: 1 addition & 0 deletions doc/website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
items: [
{type: 'doc', id: 'cli/cli-reference', label: 'Reference'},
{type: 'doc', id: 'cli/cli-url', label: 'URLs'},
{type: 'doc', id: 'cli/projects'},
]
},
{
Expand Down

0 comments on commit 634540f

Please sign in to comment.