From 4626f29fb8b4ea5aea472de3fc1a0fc844fcedc7 Mon Sep 17 00:00:00 2001 From: rkaneko Date: Wed, 6 Nov 2019 17:56:09 +0900 Subject: [PATCH] misc: Add schema and example data sql --- .gitignore | 1 + README.md | 28 ++++++++ docker-compose.yml | 12 ++++ env/local/.env | 6 ++ sql/organizations.sql | 145 ++++++++++++++++++++++++++++++++++++++++++ sql/schema.sql | 34 ++++++++++ 6 files changed, 226 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 env/local/.env create mode 100644 sql/organizations.sql create mode 100644 sql/schema.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..401fd91 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +# ignore config diff --git a/README.md b/README.md new file mode 100644 index 0000000..44287ce --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +SQL test +=== + +### Usage + +- Run up Postgresql server + +```bash +$ docker-compose up db +``` + +- Stop Postgresql server + +```bash +$ docker-compose down +``` + +- Connect to test database on Postgresql server using psql + +```bash +$ docker-compose exec db psql -U postgres test +``` + +- Execute SQL file + +```bash +$ docker-compose exec db psql -v ON_ERROR_STOP=1 -U postgres test -a -f "sql/schema.sql" +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b711319 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.3" + +services: + db: + container_name: "sql_test_db" + image: "mdillon/postgis:10" + volumes: + - "./sql:/sql" + env_file: + - "env/local/.env" + ports: + - "5438:5432" diff --git a/env/local/.env b/env/local/.env new file mode 100644 index 0000000..d7718c2 --- /dev/null +++ b/env/local/.env @@ -0,0 +1,6 @@ +POSTGRES_USER=postgres +POSTGRES_PASSWORD=password +POSTGRES_DB=test +POSTGRES_HOST=postgres +POSTGRES_PORT=5432 +# POSTGIS_MAJOR=2.3 diff --git a/sql/organizations.sql b/sql/organizations.sql new file mode 100644 index 0000000..1b60476 --- /dev/null +++ b/sql/organizations.sql @@ -0,0 +1,145 @@ +begin; + +INSERT INTO addresses ( + address_line_1, + address_line_2, + city, + region, + country, + zip_code, + phone +) VALUES + ( + 'test_line_1', + 'test_line_2', + 'test_city', + 'test_region', + 'JP', + '103-0023', + '090-0000-1111' + ) + ,( + 'Mars', + 'Moon', + 'Chuo-ku', + 'Tokyo', + 'JP', + '000-0001', + '090-0000-1112' + ) + ,( + 'Saturn', + 'Sun', + 'Shinjuku-ku', + 'Tokyo', + 'JP', + '000-0002', + '090-0000-1113' + ) + ,( + 'S', + 'S', + 'Shinjuku-ku', + 'Tokyo', + 'JP', + '000-0003', + '090-0000-1114' + ) + ,( + 'A', + 'A', + 'Shinjuku-ku', + 'Tokyo', + 'JP', + '000-0004', + '090-0000-1115' + ) + ,( + 'B', + 'B', + 'Shinjuku-ku', + 'Tokyo', + 'JP', + '000-0005', + '090-0000-1116' + ) + ,( + 'D', + 'D', + 'Shinjuku-ku', + 'Tokyo', + 'JP', + '000-0005', + '090-0000-1116' + ) +; + +INSERT INTO organizations ( + name, + address_id, + type +) VALUES + ( + 'Zoozle', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 0), + 'CUSTOMER' + ) + ,( + 'Bmazon', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 1), + 'ENTERPRISE' + ) + ,( + 'Ncrosoft', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 2), + 'ENTERPRISE_CUSTOMER' + ) + ,( + 'Moomle', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 3), + 'ENTERPRISE_CUSTOMER' + ) + ,( + 'Cmazon', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 4), + 'ENTERPRISE_CUSTOMER' + ) + ,( + 'Zcrosoft', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 5), + 'ENTERPRISE' + ) + ,( + 'Ecrosoft', + (SELECT id FROM addresses ORDER BY id ASC LIMIT 1 OFFSET 6), + 'ENTERPRISE_CUSTOMER' + ) +; + +INSERT INTO enterprise_sales_enterprise_customers ( + sales_organization_id, + customer_organization_id +) VALUES + ( + (SELECT id FROM organizations WHERE name = 'Bmazon' ORDER BY id DESC LIMIT 1), + (SELECT id FROM organizations WHERE name = 'Ncrosoft' ORDER BY id DESC LIMIT 1) + ) + ,( + (SELECT id FROM organizations WHERE name = 'Bmazon' ORDER BY id DESC LIMIT 1), + (SELECT id FROM organizations WHERE name = 'Moomle' ORDER BY id DESC LIMIT 1) + ) + ,( + (SELECT id FROM organizations WHERE name = 'Bmazon' ORDER BY id DESC LIMIT 1), + (SELECT id FROM organizations WHERE name = 'Cmazon' ORDER BY id DESC LIMIT 1) + ) + ,( + (SELECT id FROM organizations WHERE name = 'Bmazon' ORDER BY id DESC LIMIT 1), + (SELECT id FROM organizations WHERE name = 'Zcrosoft' ORDER BY id DESC LIMIT 1) + ) + ,( + (SELECT id FROM organizations WHERE name = 'Zcrosoft' ORDER BY id DESC LIMIT 1), + (SELECT id FROM organizations WHERE name = 'Ncrosoft' ORDER BY id DESC LIMIT 1) + ) +; + +commit; diff --git a/sql/schema.sql b/sql/schema.sql new file mode 100644 index 0000000..1b1304f --- /dev/null +++ b/sql/schema.sql @@ -0,0 +1,34 @@ +CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public; +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +CREATE TABLE addresses ( + "id" bigserial PRIMARY KEY NOT NULL, + "address_line_1" character varying(255) NOT NULL, + "address_line_2" character varying(255), + "city" character varying(255) NOT NULL, + "region" character varying(255) NOT NULL, + "country" character varying(255) NOT NULL, + "zip_code" character varying(255) NOT NULL, + "phone" character varying(255) NOT NULL, + "uuid" UUID UNIQUE NOT NULL DEFAULT uuid_generate_v4() +); + +CREATE TYPE organization_type AS ENUM ('CUSTOMER', 'ENTERPRISE', 'ENTERPRISE_CUSTOMER'); + +CREATE TABLE organizations ( + "id" bigserial PRIMARY KEY NOT NULL, + "name" character varying(100) NOT NULL, + "address_id" integer UNIQUE NOT NULL REFERENCES addresses("id"), + "type" organization_type NOT NULL, + "created_at" timestamp with time zone NOT NULL DEFAULT NOW(), + "uuid" UUID UNIQUE NOT NULL DEFAULT uuid_generate_v4() +); + +CREATE TABLE enterprise_sales_enterprise_customers ( + "sales_organization_id" bigint NOT NULL REFERENCES organizations("id"), + "customer_organization_id" bigint NOT NULL REFERENCES organizations("id"), + CONSTRAINT sales_organization_customer_organization_key PRIMARY KEY ( + sales_organization_id, + customer_organization_id + ) +);