Skip to content

Commit

Permalink
Add initial sql_migrate resource
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Nov 18, 2020
1 parent 87c1c74 commit 4b9e833
Show file tree
Hide file tree
Showing 8 changed files with 835 additions and 44 deletions.
68 changes: 68 additions & 0 deletions docs/resources/migrate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
page_title: "sql_migrate Resource - terraform-provider-sql"
subcategory: ""
description: |-
---

# Resource `sql_migrate`



## Example Usage

```terraform
resource "sql_migrate" "users" {
migration {
up = <<SQL
CREATE TABLE users (
user_id integer unique,
name varchar(40),
email varchar(40)
);
SQL
down = "DROP TABLE IF EXISTS users;"
}
migration {
up = "INSERT INTO users VALUES (1, 'Paul Tyng', '[email protected]');"
down = "DELETE FROM users WHERE user_id = 1;"
}
}
data "sql_query" "users" {
# run this query after the migration
depends_on = [sql_migrate.users]
query = "select * from users"
}
output "rowcount" {
value = length(data.sql_query.users.result)
}
```

## Schema

### Optional

- **migration** (Block List) (see [below for nested schema](#nestedblock--migration))

### Read-only

- **id** (String, Read-only, Deprecated) The ID of this resource.

<a id="nestedblock--migration"></a>
### Nested Schema for `migration`

Required:

- **down** (String, Required)
- **up** (String, Required)

Optional:

- **id** (String, Optional) Identifier can be any string to help identifying the migration in the source.


11 changes: 11 additions & 0 deletions examples/resources/sql_migrate/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
sql = {
source = "paultyng/sql"
}
}
}

provider "sql" {
url = "postgres://postgres:tf@localhost:5432/tftest?sslmode=disable"
}
29 changes: 29 additions & 0 deletions examples/resources/sql_migrate/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
resource "sql_migrate" "users" {
migration {
up = <<SQL
CREATE TABLE users (
user_id integer unique,
name varchar(40),
email varchar(40)
);
SQL

down = "DROP TABLE IF EXISTS users;"
}

migration {
up = "INSERT INTO users VALUES (1, 'Paul Tyng', '[email protected]');"
down = "DELETE FROM users WHERE user_id = 1;"
}
}

data "sql_query" "users" {
# run this query after the migration
depends_on = [sql_migrate.users]

query = "select * from users"
}

output "rowcount" {
value = length(data.sql_query.users.result)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/ory/dockertest v3.3.5+incompatible
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gotest.tools v2.2.0+incompatible // indirect
Expand Down
Loading

0 comments on commit 4b9e833

Please sign in to comment.