|
| 1 | +--- |
| 2 | +title: Project Structure |
| 3 | +id: projects |
| 4 | +slug: /cli/projects |
| 5 | +--- |
| 6 | +### Project Files |
| 7 | + |
| 8 | +Project files provide a convenient way to describe and interact with multiple |
| 9 | +environments when working with Atlas. A project file is a file named |
| 10 | +`atlas.hcl` and contains one or more `env` blocks. For example: |
| 11 | + |
| 12 | +```hcl |
| 13 | +// Define an environment named "local" |
| 14 | +env "local" { |
| 15 | + // Declare where the schema definition file resides. |
| 16 | + src = "./schema/project.hcl" |
| 17 | + |
| 18 | + // Define the URL of the database which is managed in |
| 19 | + // this environment. |
| 20 | + url = "mysql://localhost:3306" |
| 21 | + |
| 22 | + // Define the URL of the Dev Database for this environment |
| 23 | + // See: https://atlasgo.io/dev-database |
| 24 | + dev = "mysql://localhost:3307" |
| 25 | + |
| 26 | + // The schemas in the database that are managed by Atlas. |
| 27 | + schemas = ["users", "admin"] |
| 28 | +} |
| 29 | +
|
| 30 | +env "dev" { |
| 31 | + // ... a different env |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +Once defined, a project's environment can be worked against using the `--env` flag. |
| 36 | +For example: |
| 37 | + |
| 38 | +```shell |
| 39 | +atlas schema apply --env local |
| 40 | +``` |
| 41 | + |
| 42 | +Will run the `schema apply` command against the database that is defined for the `local` |
| 43 | +environment. |
| 44 | + |
| 45 | +### Projects with Versioned Migrations |
| 46 | + |
| 47 | +Environments may declare a `migration_dir` block to configure how versioned migrations |
| 48 | +work in the specific environment: |
| 49 | + |
| 50 | +```hcl |
| 51 | +env "local" { |
| 52 | + // .. |
| 53 | + migration_dir { |
| 54 | + // URL where the migration directory resides. Only filesystem directories |
| 55 | + // are currently supported but more options will be added in the future. |
| 56 | + url = "file://migrations" |
| 57 | + // Format of the migration directory: atlas | flyway | liquibase | goose | golang-migrate |
| 58 | + format = atlas |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Once defined, `migrate` commands can use this configuration, for example: |
| 64 | +```shell |
| 65 | +$ atlas migrate validate --env local |
| 66 | +``` |
| 67 | +Will run the `migrate validate` command against the Dev Database defined in the |
| 68 | +`local` environment. |
| 69 | + |
| 70 | +### Passing Input Values |
| 71 | + |
| 72 | +Project files may pass [input values](/ddl/input-variables) to variables defined in |
| 73 | +the Atlas schema of the environment. To do this simply provide additional attributes |
| 74 | +to the environment block: |
| 75 | +```hcl |
| 76 | +env "local" { |
| 77 | + url = "sqlite://test?mode=memory&_fk=1" |
| 78 | + src = "./schema.hcl" |
| 79 | + |
| 80 | + // Other attributes are passed as input values to "schema.hcl": |
| 81 | + tenant = "rotemtam" |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +These values can then be consumed by variables defined in the schema file: |
| 86 | + |
| 87 | +```hcl |
| 88 | +variable "tenant" { |
| 89 | + type = string |
| 90 | +} |
| 91 | +schema "main" { |
| 92 | + name = var.tenant |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Project Input Variables |
| 97 | + |
| 98 | +Project files may also declare [input variables](../ddl/input.md) that can be supplied to the CLI |
| 99 | +at runtime. For example: |
| 100 | + |
| 101 | +```hcl |
| 102 | +variable "tenant" { |
| 103 | + type = string |
| 104 | +} |
| 105 | +
|
| 106 | +env "local" { |
| 107 | + url = "sqlite://test?mode=memory&_fk=1" |
| 108 | + src = "./schema.hcl" |
| 109 | + |
| 110 | + // The value for "tenant" is determined by the user at runtime. |
| 111 | + tenant = var.tenant |
| 112 | +} |
| 113 | +``` |
| 114 | +To set the value for this variable at runtime, use the `--var` flag: |
| 115 | + |
| 116 | +```shell |
| 117 | +$ atlas schema apply --env local --var tenant=rotemtam |
| 118 | +``` |
| 119 | + |
| 120 | +It is worth mentioning that when running Atlas commands within a project using |
| 121 | +the `--env` flag, all input values supplied at the command-line are passed only |
| 122 | +to the project file, and not propagated automatically to children schema files. |
| 123 | +This is done with the purpose of creating an explicit contract between the environment |
| 124 | +and the schema file. |
0 commit comments