Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change id to id_name in sources/crosswalk, Change ref to id_reference… #50

Merged
merged 10 commits into from
Mar 21, 2023
374 changes: 374 additions & 0 deletions 2023-02-16_2018_sources.sql
zoobot marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ These files should be run against a local version of postgres for testing.

After running the migration, we want to keep the database table files up-to-date.
All schema changes and pull requests with migrations should run the
`scipts/fetch-tables.sh` script. This will update all table files in the `tables/` directory.
`scripts/fetch-tables.sh` script. This will update all table files in the `tables/` directory.
The script can be run against a local postgres instance.

```shell
$ ./scripts/fetch-tables.sh
```
30 changes: 30 additions & 0 deletions migrations/2023-02-14-migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ALTER TABLE "sources" RENAME COLUMN "id" to id_source_name;
ALTER TABLE "crosswalk" RENAME COLUMN "id" to id_source_name;
ALTER TABLE "cities" RENAME COLUMN "id" to id_source_name;
ALTER TABLE "crosswalk" RENAME COLUMN "ref" to id_reference;

ALTER TABLE "sources" DROP COLUMN id_city_name;
zoobot marked this conversation as resolved.
Show resolved Hide resolved
ALTER TABLE "cities" DROP COLUMN id_source;
ALTER TABLE "sources" DROP COLUMN id_name2;

ALTER TABLE "treedata" ADD dbh_min INTEGER;
ALTER TABLE "treedata" ADD dbh_max INTEGER;
ALTER TABLE "treedata" ADD height_min INTEGER;
ALTER TABLE "treedata" ADD height_max INTEGER;
ALTER TABLE "treedata" ADD crown_min INTEGER;
ALTER TABLE "treedata" ADD crown_max INTEGER;
ALTER TABLE "crosswalk" ADD class VARCHAR(255);


ALTER TABLE "treedata" DROP COLUMN iso_alpha_2;
ALTER TABLE "treedata" DROP COLUMN iso_alpha_3;
ALTER TABLE "treedata" DROP COLUMN numeric_country_code;
ALTER TABLE "treedata" DROP COLUMN info;
ALTER TABLE "treedata" DROP COLUMN download;
ALTER TABLE "treedata" RENAME COLUMN "source_id" to id_source_name;


-- MAKE SURE YOU RUN THIS BEFORE YOU RUN THE NEXT ONE
UPDATE "treedata" SET id_reference = ref WHERE ref IS NOT NULL;
ALTER TABLE "treedata" DROP COLUMN ref;
ALTER TABLE "treedata" DROP COLUMN red;
43 changes: 43 additions & 0 deletions migrations/2023-02-16-migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ALTER TABLE "sources" RENAME COLUMN "broken_reason" to notes;
ALTER TABLE "crosswalk" RENAME COLUMN "longitude" to longitude;
zoobot marked this conversation as resolved.
Show resolved Hide resolved
ALTER TABLE "crosswalk" RENAME COLUMN "lat" to latitude;
ALTER TABLE "crosswalk" ADD id_reference VARCHAR(255);
ALTER TABLE "crosswalk" ADD class VARCHAR(255);
ALTER TABLE "crosswalk" ADD who VARCHAR(255);
ALTER TABLE "crosswalk" ADD common VARCHAR(255);
ALTER TABLE "crosswalk" ADD scientific VARCHAR(255);
ALTER TABLE "crosswalk" ADD genus VARCHAR(255);
ALTER TABLE "crosswalk" ADD address VARCHAR(255);
ALTER TABLE "crosswalk" ADD city VARCHAR(255);
ALTER TABLE "crosswalk" ADD state VARCHAR(255);
ALTER TABLE "crosswalk" ADD zip VARCHAR(255);
ALTER TABLE "crosswalk" ADD country VARCHAR(255);
ALTER TABLE "crosswalk" ADD neighborhood VARCHAR(255);
ALTER TABLE "crosswalk" ADD health VARCHAR(255);
ALTER TABLE "crosswalk" ADD dbh VARCHAR(255);
ALTER TABLE "crosswalk" ADD height VARCHAR(255);
ALTER TABLE "crosswalk" ADD owner VARCHAR(255);
ALTER TABLE "crosswalk" ADD url VARCHAR(255);
ALTER TABLE "crosswalk" ADD urlimage VARCHAR(255);
ALTER TABLE "crosswalk" ADD status VARCHAR(255);
ALTER TABLE "crosswalk" ADD notes VARCHAR(255);
ALTER TABLE "crosswalk" ADD email VARCHAR(255);
ALTER TABLE "crosswalk" ADD volunteer VARCHAR(255);
ALTER TABLE "crosswalk" ADD legal_status VARCHAR(255);
ALTER TABLE "crosswalk" ADD irrigation VARCHAR(255);
ALTER TABLE "crosswalk" ADD water_freq VARCHAR(255);
ALTER TABLE "crosswalk" ADD watering_end_date VARCHAR(255);
ALTER TABLE "crosswalk" ADD last_watered VARCHAR(255);
ALTER TABLE "crosswalk" ADD species VARCHAR(255);
ALTER TABLE "crosswalk" ADD family VARCHAR(255);
ALTER TABLE "crosswalk" ADD class VARCHAR(255);
ALTER TABLE "crosswalk" ADD count VARCHAR(255);
ALTER TABLE "crosswalk" ADD planted VARCHAR(255);
ALTER TABLE "crosswalk" ADD variety VARCHAR(255);
ALTER TABLE "crosswalk" ADD location VARCHAR(255);
ALTER TABLE "crosswalk" ADD dbh_min VARCHAR(255);
ALTER TABLE "crosswalk" ADD dbh_max VARCHAR(255);
ALTER TABLE "crosswalk" ADD height_min VARCHAR(255);
ALTER TABLE "crosswalk" ADD height_max VARCHAR(255);
ALTER TABLE "crosswalk" ADD crown_min VARCHAR(255);
ALTER TABLE "crosswalk" ADD crown_max VARCHAR(255);
7 changes: 4 additions & 3 deletions scripts/fetch-tables.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

OUTPUT=tables
USER=postgres
USER=trees
DATABASE=treedb
SCHEMA=public
HOSTNAME=localhost

Expand Down Expand Up @@ -49,12 +50,12 @@ done

read -s -p "Password for postgres: " passwd

for table in $(PGPASSWORD=$passwd psql -U $USER -h $HOSTNAME -t -c "select schemaname || '.' || tablename as table from pg_catalog.pg_tables where schemaname = '$SCHEMA'");
for table in $(PGPASSWORD=$passwd psql -U $USER -d $DATABASE -h $HOSTNAME -t -c "select schemaname || '.' || tablename as table from pg_catalog.pg_tables where schemaname = '$SCHEMA'");
do
echo "Fetching schema for $table";
# filter out all the pg_dump comments and SET statements, then
# ignore the first 6 lines which are all blank because we skipped so much stuff
PGPASSWORD=$passwd pg_dump -U $USER -h $HOSTNAME -s --table $table \
PGPASSWORD=$passwd pg_dump -U $USER -d $DATABASE -h $HOSTNAME -s --table $table \
| grep -v "^--" | grep -v "^SET " | grep -v "^SELECT pg_catalog.set_config" \
| tail +6 \
> $OUTPUT/$table.sql;
Expand Down
2 changes: 1 addition & 1 deletion tables/public.cities.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CREATE TABLE public.cities (
country text,
city_count_trees integer DEFAULT 0,
modified timestamp without time zone,
id character varying(255),
id_source_name character varying(255),
short character varying(255),
info text,
download text,
Expand Down
35 changes: 31 additions & 4 deletions tables/public.crosswalk.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

CREATE TABLE public.crosswalk (
id_crosswalk integer NOT NULL,
id character varying(255),
id_source_name character varying(255),
common character varying(255),
species character varying(255),
genus character varying(255),
Expand All @@ -21,7 +21,7 @@ CREATE TABLE public.crosswalk (
location character varying(255),
note character varying(255),
address character varying(255),
ref character varying(255),
id_reference character varying(255),
owner character varying(255),
ule character varying(255),
ule_min character varying(255),
Expand All @@ -30,7 +30,34 @@ CREATE TABLE public.crosswalk (
gps_date character varying(255),
audited character varying(255),
modified timestamp without time zone,
created timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
created timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
class character varying(255),
longitude character varying(255),
latitude character varying(255),
who character varying(255),
city character varying(255),
state character varying(255),
zip character varying(255),
country character varying(255),
neighborhood character varying(255),
url character varying(255),
urlimage character varying(255),
status character varying(255),
email character varying(255),
volunteer character varying(255),
notes character varying(255),
legal_status character varying(255),
irrigation character varying(255),
water_freq character varying(255),
watering_end_date character varying(255),
last_watered character varying(255),
count character varying(255),
dbh_min character varying(255),
dbh_max character varying(255),
height_min character varying(255),
height_max character varying(255),
crown_min character varying(255),
crown_max character varying(255)
);


Expand All @@ -49,7 +76,7 @@ ALTER TABLE public.crosswalk ALTER COLUMN id_crosswalk ADD GENERATED ALWAYS AS I


ALTER TABLE ONLY public.crosswalk
ADD CONSTRAINT crosswalk_id_fkey FOREIGN KEY (id) REFERENCES public.sources(id) ON UPDATE CASCADE ON DELETE CASCADE;
ADD CONSTRAINT crosswalk_id_fkey FOREIGN KEY (id_source_name) REFERENCES public.sources(id_source_name) ON UPDATE CASCADE ON DELETE CASCADE;



7 changes: 3 additions & 4 deletions tables/public.sources.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

CREATE TABLE public.sources (
id_sources integer NOT NULL,
id character varying(255),
id_source_name character varying(255),
short character varying(255),
city character varying(255),
state character varying(255),
Expand All @@ -10,7 +10,6 @@ CREATE TABLE public.sources (
iso_alpha_3 character varying(3),
numeric_country_code integer DEFAULT 0,
long character varying(255),
id_city_name character varying(255),
main character varying(255),
center character varying(255),
longitude double precision,
Expand All @@ -22,7 +21,7 @@ CREATE TABLE public.sources (
trees_per_source_count integer DEFAULT 0,
country_code character varying(10),
broken boolean DEFAULT false,
broken_reason character varying(255),
notes character varying(255),
info text,
download text,
format character varying(255),
Expand All @@ -48,7 +47,7 @@ ALTER TABLE public.sources ALTER COLUMN id_sources ADD GENERATED ALWAYS AS IDENT



CREATE UNIQUE INDEX id_idx ON public.sources USING btree (id);
CREATE UNIQUE INDEX id_idx ON public.sources USING btree (id_source_name);



15 changes: 14 additions & 1 deletion tables/public.treedata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ CREATE TABLE public.treedata (
watering_end_date timestamp without time zone,
last_watered timestamp without time zone,
id bigint NOT NULL,
source_id character varying(255)
id_source_name character varying(255),
species character varying(255),
family character varying(255),
class character varying(255),
count integer DEFAULT 1,
zoobot marked this conversation as resolved.
Show resolved Hide resolved
planted character varying(255),
variety character varying(255),
location character varying(255),
dbh_min integer,
dbh_max integer,
height_min integer,
height_max integer,
crown_min integer,
crown_max integer
);


Expand Down