Are all constraints, checks, default values, and indices for database columns installed properly?
Did we have other default values in the old database?
Can we propose default values for every column, so we do not have to define nullable columns within the database (slow access to those)?
Check all of these constraints and check, whether sqlacodegen still generates the same classes as coded in this repository.
All default values that correspond to 'undefined' within controlled vocabulary have to be defined in those tables (related to #3 (closed))!
Default values for stationmeta (from old database):
column
type
default
station_nightlight_5km
real
'-999.0'::numeric
station_climatic_zone
integer
'-1'::integer
station_wheat_production
real
'-999.0'::numeric
station_rice_production
real
'-999.0'::numeric
station_nox_emissions
real
'-999.0'::numeric
station_omi_no2_column
real
'-999.0'::numeric
station_toar_category
integer
'-1'::integer
station_htap_region
integer
'-1'::integer
station_alt_flag
integer
0
station_coordinate_status
integer
'-1'::integer
station_google_alt
real
'-999.0'::numeric
station_etopo_alt
real
'-999.0'::numeric
station_etopo_min_alt_5km
real
'-999.0'::numeric
station_etopo_relative_alt
real
'-999.0'::numeric
station_dominant_landcover
integer
'-1'::integer
station_max_nightlight_25km
real
'-999.0'::numeric
station_max_population_density_25km
integer
'-1'::integer
station_nightlight_1km
real
'-999.0'::numeric
station_population_density
integer
'-1'::integer
google_resolution
real
'-999.0'::numeric
station_max_population_density_5km
integer
'-1'::integer
Default values for timeseries (from old database):
column
type
default
creation_date
timestamp with time zone
now()
modification_date
timestamp with time zone
now()
The above defaults are already established in my local version -- will be uploaded with the next database dump ('toardb_dump.sql'). BTW: The fields population_density, max_population_density changed from integer to double precision!
The above defaults are already established in my local version -- will be uploaded with the next database dump ('toardb_v2_dump.sql').
Of course, they will also appear in source code!
Tables "annotations" (at the moment two: stationmeta_annotations and timeseries_annotations -- these should be reduced to one table later) did not make use of controlled vocabulary for column kind.
Controlled vocabulary was added, models and schemas were adapted, and the following command was interactively executed on the database:
ALTER TABLE stationmeta_annotationsALTER COLUMN kind SET DEFAULT 0,ADD CONSTRAINT stationmeta_annotations_kind_fk_ak_vocabulary_enum_val FOREIGN KEY(kind) REFERENCES ak_vocabulary(enum_val);
The above commands are added to db_changes.psql in the rollout branch (see #22 (closed)).
Commit 870ac743 (and parts of commit 90d30342) contains these changes and adaptions.
Some notes on ON_DELETE_CASCADE
(We do not want to delete any data from the database.)
Although we never really want to use it, we want to introduce it to the database tables. Somehow it can happen that we want to remove old (meta) data, and if you don't have a "cascade", it becomes very tedious.
The data in the "staging database" would actually be deleted if it had been transferred. And that should be as identical as possible with the operational database.
We decided to cascade deletes on contacts(id) (persons(id)/organisations(id)), stationmeta_core(id), and timeseries(id) -- but not on variables(id).
This affects the following tables: 1. stationmeta_core(id):
stationmeta_core_stationmeta_annotations
stationmeta_aux_doc
stationmeta_aux_image
stationmeta_aux_url
stationmeta_changelog
stationmeta_core_stationmeta_roles
stationmeta_global_services (?! is station_id needed at all?!)
stationmeta_global
timeseries 2. timeseries(id):
data_archive
data
timeseries_changelog
timeseries_timeseries_annotations
timeseries_timeseries_roles 3. contacts(id) (persons(id)/organisations(id)):
stationmeta_roles
stationmeta_core_stationmeta_roles (now for stationmeta_roles(id))
timeseries_roles
timeseries_timeseries_roles (now for timeseries_roles(id))
What about stationmeta_annotation, timeseries_annotation? Should it be possible to delete these (which has then also to be cascaded).
Default values / constraints / nullable / on delete cascade for all tables
Changes to the database (including on delete cascade):
ALTER TABLE persons ADD COLUMN orcid CHARACTER(19) DEFAULT '0000-0000-0000-0000' NOT NULL;ALTER TABLE timeseries ADD COLUMN version VARCHAR(28) DEFAULT '' NOT NULL;ALTER TABLE timeseries ADD COLUMN data_license_accepted timestamp with time zone DEFAULT '1900-01-01' NOT NULL;ALTER TABLE timeseries DROP COLUMN date_added;ALTER TABLE timeseries DROP COLUMN date_modified;ALTER TABLE timeseries DROP CONSTRAINT timeseries_station_id_0f4fed9c_fk_stationmeta_core_id, ADD CONSTRAINT timeseries_station_id_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_core_stationmeta_roles DROP CONSTRAINT stationmeta_core_stationmeta_roles_station_id_fkey, ADD CONSTRAINT stationmeta_core_stationmeta_roles_station_id_fkey FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_changelog DROP CONSTRAINT stationmeta_changelog_station_id_fk_stationmeta_core_id, ADD CONSTRAINT stationmeta_changelog_station_id_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_core_stationmeta_annotations DROP CONSTRAINT stationmeta_core_stationmeta_annotations_station_id_fkey, ADD CONSTRAINT stationmeta_core_stationmeta_annotations_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_aux_doc DROP CONSTRAINT stationmeta_aux_doc_station_id_17bdb5f2_fk_stationmeta_core_id, ADD CONSTRAINT stationmeta_aux_doc_station_id_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_aux_image DROP CONSTRAINT stationmeta_aux_imag_station_id_fbfbdb29_fk_stationme, ADD CONSTRAINT stationmeta_aux_image_station_id_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_aux_url DROP CONSTRAINT stationmeta_aux_url_station_id_727571bd_fk_stationmeta_core_id, ADD CONSTRAINT stationmeta_aux_url_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE stationmeta_global DROP CONSTRAINT stationmeta_global_station_id_fk_stationmeta_core_id, ADD CONSTRAINT stationmeta_global_station_id_fk_stationmeta_core_id FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) ON DELETE CASCADE;ALTER TABLE timeseries_changelog DROP CONSTRAINT timeseries_changelog_timeseries_id_fk_timeseries_id, ADD CONSTRAINT timeseries_changelog_timeseries_id_fk_timeseries_id FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) ON DELETE CASCADE;ALTER TABLE data DROP CONSTRAINT data_timeseries_id_a38c5a1a_fk_timeseries_id, ADD CONSTRAINT data_timeseries_id_fk_timeseries_id FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) ON DELETE CASCADE;ALTER TABLE data_archive DROP CONSTRAINT data_archive_timeseries_id_fk_timeseries_id, ADD CONSTRAINT data_archive_timeseries_id_fk_timeseries_id FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) ON DELETE CASCADE;ALTER TABLE timeseries_timeseries_roles DROP CONSTRAINT timeseries_timeseries_roles_timeseries_id_fkey, ADD CONSTRAINT timeseries_timeseries_roles_timeseries_id_fkey FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) ON DELETE CASCADE;ALTER TABLE timeseries_timeseries_annotations DROP CONSTRAINT timeseries_timeseries_annotations_timeseries_id_fkey, ADD CONSTRAINT timeseries_timeseries_annotations_fk_timeseries_id FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) ON DELETE CASCADE;ALTER TABLE contacts DROP CONSTRAINT contacts_organisation_id_fkey, ADD CONSTRAINT contacts_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES organisations(id) ON DELETE CASCADE;ALTER TABLE contacts DROP CONSTRAINT contacts_person_id_fkey, ADD CONSTRAINT contacts_person_id_fkey FOREIGN KEY (person_id) REFERENCES persons(id) ON DELETE CASCADE;ALTER TABLE stationmeta_roles DROP CONSTRAINT stationmeta_roles_contact_id_fk_contacts_id, ADD CONSTRAINT stationmeta_roles_contact_id_fk_contacts_id FOREIGN KEY (contact_id) REFERENCES contacts(id) ON DELETE CASCADE;ALTER TABLE stationmeta_core_stationmeta_roles DROP CONSTRAINT stationmeta_core_stationmeta_roles_role_id_fkey, ADD CONSTRAINT stationmeta_core_stationmeta_roles_role_id_fkey FOREIGN KEY (role_id) REFERENCES stationmeta_roles(id) ON DELETE CASCADE;ALTER TABLE timeseries_roles DROP CONSTRAINT timeseries_roles_contact_id_fk_contacts_id, ADD CONSTRAINT timeseries_roles_contact_id_fk_contacts_id FOREIGN KEY (contact_id) REFERENCES contacts(id) ON DELETE CASCADE;ALTER TABLE timeseries_timeseries_roles DROP CONSTRAINT timeseries_timeseries_roles_role_id_fkey, ADD CONSTRAINT timeseries_timeseries_roles_role_id_fkey FOREIGN KEY (role_id) REFERENCES timeseries_roles(id) ON DELETE CASCADE;ALTER TABLE timeseries RENAME COLUMN source TO data_origin_type;ALTER TABLE timeseries RENAME COLUMN measurement_method TO data_origin;
After upgrading the controlled vocabulary rename constraints:
ALTER TABLE timeseries RENAME CONSTRAINT timeseries_source_fk_ds_vocabulary_enum_val TO timeseries_data_origin_type_fk_ot_vocabulary_enum_val;ALTER TABLE timeseries RENAME CONSTRAINT timeseries_measurement_method_fk_mm_vocabulary_enum_val TO timeseries_data_origin_fk_do_vocabulary_enum_val;
For reasons of consistency the ordering within the controlled vocabulary changed (to always use 0 as default value):
ALTER TABLE timeseries ALTER COLUMN data_origin_type SET DEFAULT 0;ALTER TABLE timeseries ALTER COLUMN data_origin SET DEFAULT 0;