Skip to content

Commit

Permalink
updated local test script and fixtures for cockroach based on v21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Sep 9, 2022
1 parent a1d9566 commit c681bf3
Show file tree
Hide file tree
Showing 36 changed files with 1,005 additions and 629 deletions.
45 changes: 5 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,58 +1,23 @@
TAGS ?= "sqlite"
GO_BIN ?= "go"
TAGS ?= sqlite
GO_BIN ?= go

install:
$(GO_BIN) install -tags ${TAGS} -v .
make tidy

tidy:
ifeq ($(GO111MODULE),on)
$(GO_BIN) mod tidy
else
echo skipping go mod tidy
endif

deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
make tidy

build:
$(GO_BIN) build -v .
make tidy

test:
./test.sh
make tidy

ci-deps:
$(GO_BIN) get -tags ${TAGS} -t ./...

ci-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
./test.sh -cover -v

lint:
go get github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run --enable-all
make tidy

update:
ifeq ($(GO111MODULE),on)
rm go.*
$(GO_BIN) mod init
$(GO_BIN) mod tidy
else
$(GO_BIN) get -u -tags ${TAGS}
endif
make test
make install
make tidy

release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
make tidy

release:
$(GO_BIN) get github.com/gobuffalo/release
make tidy
release -y -f version.go --skip-packr
make tidy
$(GO_BIN) mod init github.com/gobuffalo/fizz
$(GO_BIN) mod tidy --go=1.16
57 changes: 37 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
# Fizz

[![Actions Status](https://github.com/gobuffalo/fizz/workflows/Tests/badge.svg)](https://github.com/gobuffalo/fizz/actions)
[![GoDoc](https://godoc.org/github.com/gobuffalo/fizz?status.svg)](https://godoc.org/github.com/gobuffalo/fizz)
[![Go Reference](https://pkg.go.dev/badge/github.com/gobuffalo/fizz.svg)](https://pkg.go.dev/github.com/gobuffalo/fizz)

A Common DSL for Migrating Databases

## Create a Table

## Supported Database Engines

Fizz supports minimum supported version of all supported database engines.
Currently, the following database engines are officially supported. (Since
Fizz is used with the migration feature of Pop, supported databases and the
versions are correlated with Pop.)

* PostgreSQL 10
* MySQL 5.7 / MariaDB 10.3
* SQLite3 3.22
* CockroachDB v21.1
* MSSQL 2017 (not fully supported)


## Usage

### Create a Table

``` javascript
create_table("users") {
Expand All @@ -28,7 +45,7 @@ create_table("todos") {
}
```

The `id` column don't have to be an integer. For instance, your can use an [`UUID`](https://github.com/gobuffalo/uuid) type instead:
The `id` column don't have to be an integer. For instance, your can use an UUID type instead:

```javascript
create_table("users") {
Expand Down Expand Up @@ -75,89 +92,89 @@ create_table("user_privileges") {

Please note that the `t.PrimaryKey` statement MUST be after the columns definitions.

## Drop a Table
### Drop a Table

``` javascript
drop_table("table_name")
```

## Rename a Table
### Rename a Table

``` javascript
rename_table("old_table_name", "new_table_name")
```

## Add a Column
### Add a Column

``` javascript
add_column("table_name", "column_name", "string", {})
```

See [above](#column-info) for more details on column types and options.

## Alter a column
### Alter a column

``` javascript
change_column("table_name", "column_name", "string", {})
```

## Rename a Column
### Rename a Column

``` javascript
rename_column("table_name", "old_column_name", "new_column_name")
```

## Drop a Column
### Drop a Column

``` javascript
drop_column("table_name", "column_name")
```

## Add an Index
### Add an Index

#### Supported Options:

* `name` - This defaults to `table_name_column_name_idx`
* `unique`

### Simple Index:
#### Simple Index:

``` javascript
add_index("table_name", "column_name", {})
```

### Multi-Column Index:
#### Multi-Column Index:

``` javascript
add_index("table_name", ["column_1", "column_2"], {})
```

### Unique Index:
#### Unique Index:

``` javascript
add_index("table_name", "column_name", {"unique": true})
```

### Index Names:
#### Index Names:

``` javascript
add_index("table_name", "column_name", {}) # name => table_name_column_name_idx
add_index("table_name", "column_name", {"name": "custom_index_name"})
```

## Rename an Index
### Rename an Index

``` javascript
rename_index("table_name", "old_index_name", "new_index_name")
```

## Drop an Index
### Drop an Index

``` javascript
drop_index("table_name", "index_name")
```

## Add a Foreign Key
### Add a Foreign Key

```javascript
add_foreign_key("table_name", "field", {"ref_table_name": ["ref_column"]}, {
Expand All @@ -176,7 +193,7 @@ add_foreign_key("table_name", "field", {"ref_table_name": ["ref_column"]}, {

**Note:** `on_update` and `on_delete` are not supported on CockroachDB yet.

## Drop a Foreign Key
### Drop a Foreign Key

```javascript
drop_foreign_key("table_name", "fk_name", {"if_exists": true})
Expand All @@ -187,13 +204,13 @@ drop_foreign_key("table_name", "fk_name", {"if_exists": true})
* `if_exists` - Adds `IF EXISTS` condition


## Raw SQL
### Raw SQL

``` javascript
sql("select * from users;")
```

## Execute an External Command
### Execute an External Command

Sometimes during a migration you need to shell out to an external command.

Expand Down
63 changes: 42 additions & 21 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,53 @@ services:
- MYSQL_DATABASE=pop_test
- MYSQL_USER=pop
- MYSQL_PASSWORD=pop
volumes:
- ./sql:/docker-entrypoint-initdb.d
#volumes:
#- ./_vol/mysql:/docker-entrypoint-initdb.d
ports:
- "3306:3306"
- "3307:3306"
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 5s
timeout: 5s
retries: 10
start_period: 3s

postgres:
image: postgres:9.6
image: postgres:10
environment:
- POSTGRES_DB=pop_test
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
volumes:
- ./sqldumps:/docker-entrypoint-initdb.d
- "5433:5432"
#volumes:
#- ./_vol/postgres:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD", "pg_isready"]
interval: 5s
timeout: 5s
retries: 10
start_period: 3s

cockroach:
image: cockroachdb/cockroach:v19.2.10
image: cockroachdb/cockroach:latest-v21.1
ports:
- "26257:26257"
- "8080:8080"
volumes:
- "./cockroach-data/roach1:/cockroach/cockroach-data"
command: start --insecure
mssqlserver:
image: "microsoft/mssql-server-linux"
environment:
- SA_PASSWORD=Tt@12345678
- MSSQLSERVER_PASSWORD=Tt@12345678
- ACCEPT_EULA=Y
ports:
- "1433:1433"
- "26258:26257"
- "8081:8080"
#volumes:
#- ./_vol/cockroach:/cockroach/cockroach-data
command: start-single-node --insecure
healthcheck:
test: ["CMD", "curl", "http://localhost:8080/health"]
interval: 5s
timeout: 5s
retries: 10
start_period: 3s

# mssqlserver:
# image: "microsoft/mssql-server-linux"
# environment:
# - SA_PASSWORD=Tt@12345678
# - MSSQLSERVER_PASSWORD=Tt@12345678
# - ACCEPT_EULA=Y
# ports:
# - "1433:1433"
45 changes: 26 additions & 19 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
#!/bin/bash

set -e
clear

verbose=""
# NOTE: See also docker-compose.yml and database.yml to configure database
# properties.
export MYSQL_PORT=3307
export COCKROACH_PORT=26258

echo $@
COMPOSE=docker-compose
which docker-compose || COMPOSE="docker compose"

if [[ "$@" == "-v" ]]
then
verbose="-v"
fi
args=$@

function cleanup {
echo "Cleanup resources..."
docker-compose down
find ./sql_scripts/sqlite -name *.sqlite* -delete || true
$COMPOSE down
docker volume prune -f
find ./tmp -name *.sqlite* -delete || true
}
# defer cleanup, so it will be executed even after premature exit
trap cleanup EXIT

docker-compose up -d
sleep 10 # Ensure mysql is online

go install -tags sqlite github.com/gobuffalo/pop/v6/soda@latest

function test {
echo "!!! Testing $1"
export SODA_DIALECT=$1

echo ""
echo "######################################################################"
echo "### Running unit tests for $SODA_DIALECT"
soda drop -e $SODA_DIALECT
soda create -e $SODA_DIALECT
soda migrate -e $SODA_DIALECT -p ./testdata/migrations
go test -tags sqlite -count=1 $verbose $(go list ./... | grep -v /vendor/)
echo "!!! Resetting $1"
go test -tags sqlite -count=1 $args ./...

echo ""
echo "######################################################################"
echo "### Running e2e tests for $1"
soda drop -e $SODA_DIALECT
soda create -e $SODA_DIALECT
echo "!!! Running e2e tests $1"
cd testdata/e2e; go test -tags sqlite,e2e -count=1 $verbose ./...; cd ..
pushd testdata/e2e; go test -tags sqlite,e2e -count=1 $args ./...; popd
}


$COMPOSE up --wait

go install -tags sqlite github.com/gobuffalo/pop/v6/soda@latest

test "sqlite"
test "postgres"
test "cockroach"
Expand Down
8 changes: 7 additions & 1 deletion testdata/e2e/fixtures/cockroach/down/0.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
CREATE TABLE schema_migration (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # 1 row
Loading

0 comments on commit c681bf3

Please sign in to comment.