Skip to content

Commit

Permalink
Merge pull request #71 from schemahero/set-defualt
Browse files Browse the repository at this point in the history
Set column default value
  • Loading branch information
emosbaugh committed Aug 9, 2019
2 parents a4660a9 + 08ad138 commit ba48a4e
Show file tree
Hide file tree
Showing 53 changed files with 739 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.schemahero
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ ENV GO111MODULE=on
RUN go mod vendor

# Build
COPY pkg/ pkg/
COPY cmd/ cmd/
COPY pkg/ pkg/
RUN make bin/schemahero


Expand Down
4 changes: 2 additions & 2 deletions integration/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export GO111MODULE=on
run: build postgres mysql

.PHONY: postgres
postgres: build
postgres:
make -C tests/postgres/create-table run
make -C tests/postgres/foreign-key-create run
make -C tests/postgres/foreign-key-action run
Expand All @@ -21,7 +21,7 @@ postgres: build
make -C tests/postgres/primary-key-drop run

.PHONY: mysql
mysql: build
mysql:
make -C tests/mysql/create-table run
make -C tests/mysql/foreign-key-create run
make -C tests/mysql/foreign-key-action run
Expand Down
9 changes: 9 additions & 0 deletions integration/tests/mysql/column-set-default/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM mysql:8.0

ENV MYSQL_USER=schemahero
ENV MYSQL_PASSWORD=password
ENV MYSQL_DATABASE=schemahero
ENV MYSQL_RANDOM_ROOT_PASSWORD=1

## Insert fixtures
COPY ./fixtures.sql /docker-entrypoint-initdb.d/
57 changes: 57 additions & 0 deletions integration/tests/mysql/column-set-default/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
SHELL := /bin/bash
TEST_NAME := mysql-column-set-default
DATABASE_IMAGE_NAME := schemahero/database
DATABASE_CONTAINER_NAME := schemahero-database
DRIVER := mysql
URI := schemahero:password@tcp($(DATABASE_CONTAINER_NAME):3306)/schemahero?tls=false
SPEC_FILE := /specs/users.yaml

.PHONY: run
run:
@rm -rf ./out
@mkdir ./out
@-docker rm -f $(DATABASE_CONTAINER_NAME) > /dev/null 2>&1 ||:
@-docker rm -f $(TEST_NAME) > /dev/null 2>&1 ||:
@-docker network rm $(TEST_NAME) > /dev/null 2>&1 ||:
docker network create $(TEST_NAME)

# Fixtures
docker pull mysql:8.0
docker build -t $(DATABASE_IMAGE_NAME) .
@-docker rm -f $(DATABASE_CONTAINER_NAME) > /dev/null 2>&1 ||:
docker run --network $(TEST_NAME) --rm -d --name $(DATABASE_CONTAINER_NAME) $(DATABASE_IMAGE_NAME)
while ! docker exec -it $(DATABASE_CONTAINER_NAME) mysqladmin ping -h$(DATABASE_CONTAINER_NAME) --silent; do sleep 1; done

# Test
docker tag $(IMAGE) schemahero/schemahero:test
docker run -v `pwd`/specs:/specs \
--network $(TEST_NAME) \
--name $(TEST_NAME) \
--rm \
schemahero/schemahero:test \
apply \
--driver $(DRIVER) \
--uri "$(URI)" \
--spec-file $(SPEC_FILE)

# Verify
docker run \
--rm \
--network $(TEST_NAME) \
-v `pwd`/out:/out \
-e uid=$${UID} \
schemahero/schemahero:test \
generate \
--dbname schemahero \
--namespace default \
--driver $(DRIVER) \
--output-dir /out \
--uri "$(URI)"
@echo Verifying results for $(TEST_NAME)
diff expect out

# Cleanup
@-sleep 5
rm -rf ./out
@-docker rm -f $(DATABASE_CONTAINER_NAME)
@-docker network rm $(TEST_NAME)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- ./users.yaml
30 changes: 30 additions & 0 deletions integration/tests/mysql/column-set-default/expect/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: schemas.schemahero.io/v1alpha2
kind: Table
metadata:
name: users
spec:
database: schemahero
name: users
schema:
mysql:
primaryKey:
- id
columns:
- name: id
type: int
constraints:
notNull: true
- name: email
type: varchar (255)
constraints:
notNull: true
- name: account_type
type: varchar (10)
constraints:
notNull: true
default: trial
- name: num_seats
type: int
constraints:
notNull: true
default: "5"
6 changes: 6 additions & 0 deletions integration/tests/mysql/column-set-default/fixtures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table users (
id integer primary key not null,
email varchar(255) not null,
account_type varchar(10),
num_seats integer
);
23 changes: 23 additions & 0 deletions integration/tests/mysql/column-set-default/specs/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
database: schemahero
name: users
requires: []
schema:
mysql:
primaryKey: [id]
columns:
- name: id
type: integer
- name: email
type: varchar(255)
constraints:
notNull: true
- name: account_type
type: varchar(10)
constraints:
notNull: true
default: trial
- name: num_seats
type: integer
constraints:
notNull: true
default: "5"
9 changes: 9 additions & 0 deletions integration/tests/mysql/column-unset-default/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM mysql:8.0

ENV MYSQL_USER=schemahero
ENV MYSQL_PASSWORD=password
ENV MYSQL_DATABASE=schemahero
ENV MYSQL_RANDOM_ROOT_PASSWORD=1

## Insert fixtures
COPY ./fixtures.sql /docker-entrypoint-initdb.d/
57 changes: 57 additions & 0 deletions integration/tests/mysql/column-unset-default/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
SHELL := /bin/bash
TEST_NAME := mysql-column-unset-default
DATABASE_IMAGE_NAME := schemahero/database
DATABASE_CONTAINER_NAME := schemahero-database
DRIVER := mysql
URI := schemahero:password@tcp($(DATABASE_CONTAINER_NAME):3306)/schemahero?tls=false
SPEC_FILE := /specs/users.yaml

.PHONY: run
run:
@rm -rf ./out
@mkdir ./out
@-docker rm -f $(DATABASE_CONTAINER_NAME) > /dev/null 2>&1 ||:
@-docker rm -f $(TEST_NAME) > /dev/null 2>&1 ||:
@-docker network rm $(TEST_NAME) > /dev/null 2>&1 ||:
docker network create $(TEST_NAME)

# Fixtures
docker pull mysql:8.0
docker build -t $(DATABASE_IMAGE_NAME) .
@-docker rm -f $(DATABASE_CONTAINER_NAME) > /dev/null 2>&1 ||:
docker run --network $(TEST_NAME) --rm -d --name $(DATABASE_CONTAINER_NAME) $(DATABASE_IMAGE_NAME)
while ! docker exec -it $(DATABASE_CONTAINER_NAME) mysqladmin ping -h$(DATABASE_CONTAINER_NAME) --silent; do sleep 1; done

# Test
docker tag $(IMAGE) schemahero/schemahero:test
docker run -v `pwd`/specs:/specs \
--network $(TEST_NAME) \
--name $(TEST_NAME) \
--rm \
schemahero/schemahero:test \
apply \
--driver $(DRIVER) \
--uri "$(URI)" \
--spec-file $(SPEC_FILE)

# Verify
docker run \
--rm \
--network $(TEST_NAME) \
-v `pwd`/out:/out \
-e uid=$${UID} \
schemahero/schemahero:test \
generate \
--dbname schemahero \
--namespace default \
--driver $(DRIVER) \
--output-dir /out \
--uri "$(URI)"
@echo Verifying results for $(TEST_NAME)
diff expect out

# Cleanup
@-sleep 5
rm -rf ./out
@-docker rm -f $(DATABASE_CONTAINER_NAME)
@-docker network rm $(TEST_NAME)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- ./users.yaml
28 changes: 28 additions & 0 deletions integration/tests/mysql/column-unset-default/expect/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: schemas.schemahero.io/v1alpha2
kind: Table
metadata:
name: users
spec:
database: schemahero
name: users
schema:
mysql:
primaryKey:
- id
columns:
- name: id
type: int
constraints:
notNull: true
- name: email
type: varchar (255)
constraints:
notNull: true
- name: account_type
type: varchar (10)
constraints:
notNull: false
- name: num_seats
type: int
constraints:
notNull: false
6 changes: 6 additions & 0 deletions integration/tests/mysql/column-unset-default/fixtures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table users (
id integer primary key not null,
email varchar(255) not null,
account_type varchar(10) not null default 'trial',
num_seats integer not null default 5
);
17 changes: 17 additions & 0 deletions integration/tests/mysql/column-unset-default/specs/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
database: schemahero
name: users
requires: []
schema:
mysql:
primaryKey: [id]
columns:
- name: id
type: integer
- name: email
type: varchar(255)
constraints:
notNull: true
- name: account_type
type: varchar(10)
- name: num_seats
type: integer
1 change: 1 addition & 0 deletions integration/tests/mysql/index-create/expect/users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ spec:
type: varchar (10)
constraints:
notNull: true
default: ""
2 changes: 1 addition & 1 deletion integration/tests/mysql/index-create/fixtures.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create table users (
id integer primary key not null,
email varchar(255) not null,
phone varchar(10) not null
phone varchar(10) not null default ''
);
1 change: 1 addition & 0 deletions integration/tests/mysql/index-create/specs/users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ schema:
type: varchar(10)
constraints:
notNull: true
default: ""
2 changes: 0 additions & 2 deletions integration/tests/mysql/not-null/fixtures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ create table projects (
name varchar(255) not null,
icon_uri varchar(255) not null
);


8 changes: 8 additions & 0 deletions integration/tests/postgres/column-set-default/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM postgres:10.7

ENV POSTGRES_USER=schemahero
ENV POSTGRES_PASSWORD=password
ENV POSTGRES_DB=schemahero

## Insert fixtures
COPY ./fixtures.sql /docker-entrypoint-initdb.d/
57 changes: 57 additions & 0 deletions integration/tests/postgres/column-set-default/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
SHELL := /bin/bash
TEST_NAME := postgres-column-set-default
DATABASE_IMAGE_NAME := schemahero/database
DATABASE_CONTAINER_NAME := schemahero-database
DRIVER := postgres
URI := postgres://schemahero:password@$(DATABASE_CONTAINER_NAME):5432/schemahero?sslmode=disable
SPEC_FILE := /specs/users.yaml

.PHONY: run
run:
@rm -rf ./out
@mkdir ./out
@-docker rm -f $(DATABASE_CONTAINER_NAME) > /dev/null 2>&1 ||:
@-docker rm -f $(TEST_NAME) > /dev/null 2>&1 ||:
@-docker network rm $(TEST_NAME) > /dev/null 2>&1 ||:
docker network create $(TEST_NAME)

# Fixtures
docker pull postgres:10.7
docker build -t $(DATABASE_IMAGE_NAME) .
@-docker rm -f $(DATABASE_CONTAINER_NAME) > /dev/null 2>&1 ||:
docker run --network $(TEST_NAME) --rm -d --name $(DATABASE_CONTAINER_NAME) $(DATABASE_IMAGE_NAME)
while ! docker exec -it $(DATABASE_CONTAINER_NAME) pg_isready -h$(DATABASE_CONTAINER_NAME) --quiet; do sleep 1; done

# Test
docker tag $(IMAGE) schemahero/schemahero:test
docker run -v `pwd`/specs:/specs \
--network $(TEST_NAME) \
--name $(TEST_NAME) \
--rm \
schemahero/schemahero:test \
apply \
--driver $(DRIVER) \
--uri "$(URI)" \
--spec-file $(SPEC_FILE)

# Verify
docker run \
--rm \
--network $(TEST_NAME) \
-v `pwd`/out:/out \
-e uid=$${UID} \
schemahero/schemahero:test \
generate \
--dbname schemahero \
--namespace default \
--driver $(DRIVER) \
--output-dir /out \
--uri "$(URI)"
@echo Verifying results for $(TEST_NAME)
diff expect out

# Cleanup
@-sleep 5
rm -rf ./out
@-docker rm -f $(DATABASE_CONTAINER_NAME)
@-docker network rm $(TEST_NAME)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- ./users.yaml

0 comments on commit ba48a4e

Please sign in to comment.