diff --git a/.env b/.env index 30faa35..bd57c02 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ VITE_DISCOVER_QUAD_WORD= VITE_FIREBASE_CONFIG= VITE_PROXY_URL= +VITE_DATABASE_CONFIG= diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c03d4df..fb5c822 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,6 +40,7 @@ jobs: env: VITE_DISCOVER: ${{ secrets.DISCOVER }} VITE_FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }} + VITE_DATABASE_CONFIG: ${{ secrets.DATABASE_CONFIG }} deploy-prod: name: Deploy to production @@ -72,6 +73,7 @@ jobs: env: VITE_DISCOVER: ${{ secrets.DISCOVER }} VITE_FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }} + VITE_DATABASE_CONFIG: ${{ secrets.DATABASE_CONFIG }} notify: name: Notifications diff --git a/.vscode/settings.json b/.vscode/settings.json index 82934bf..1859cc0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,7 +35,6 @@ "ugrc", "usgs", "vite", - "WILDADMIN", "wkid", "wrimaps" ], diff --git a/scripts/download.py b/scripts/download.py index 361335c..e39b6f3 100644 --- a/scripts/download.py +++ b/scripts/download.py @@ -15,9 +15,9 @@ import arcpy import pyodbc - import swq_secrets as secrets + def zip_fgdb(path, zip): path = normpath(path) for (dirpath, dirnames, filenames) in walk(path): @@ -87,20 +87,17 @@ def main(ids, type): arcpy.management.CopyFeatures(events_layer, join(fgdb, 'SamplingEvents')) arcpy.AddMessage('copying stations feature class') - stations_where = 'STATION_ID IN (SELECT STATION_ID FROM {}.WILDADMIN.SamplingEvents_evw where {})'.format( - secrets.DATABASE, events_where) + stations_where = f'STATION_ID IN (SELECT STATION_ID FROM {secrets.DATABASE}.{secrets.USERNAME}.SamplingEvents_evw where {events_where})' stations_layer = arcpy.management.MakeFeatureLayer(join(sde, 'Stations'), 'stations_layer', stations_where) arcpy.management.CopyFeatures(stations_layer, join(fgdb, 'Stations')) arcpy.AddMessage('copying streams feature class') - stations_where = 'Permanent_Identifier IN (SELECT WATER_ID FROM {}.WILDADMIN.Stations_evw where {})'.format( - secrets.DATABASE, stations_where) + stations_where = f'Permanent_Identifier IN (SELECT WATER_ID FROM {secrets.DATABASE}.{secrets.USERNAME}.Stations_evw where {stations_where})' streams_layer = arcpy.management.MakeFeatureLayer(join(sde, 'UDWRStreams'), 'streams_layer', stations_where) arcpy.management.CopyFeatures(streams_layer, join(fgdb, 'UDWRStreams')) arcpy.AddMessage('copying lakes feature class') - stations_where = 'Permanent_Identifier IN (SELECT WATER_ID FROM {}.WILDADMIN.Stations_evw where {})'.format( - secrets.DATABASE, stations_where) + stations_where = f'Permanent_Identifier IN (SELECT WATER_ID FROM {secrets.DATABASE}.{secrets.USERNAME}.Stations_evw where {stations_where})' lakes_layer = arcpy.management.MakeFeatureLayer(join(sde, 'UDWRLakes'), 'lakes_layer', stations_where) arcpy.management.CopyFeatures(lakes_layer, join(fgdb, 'UDWRLakes')) @@ -116,8 +113,7 @@ def copy_related_tables(dataset): destination_is_table = arcpy.Describe(join(sde, destination)).datasetType == 'Table' if destination.split('.')[-1] != dataset and destination_is_table: arcpy.AddMessage('copying {} table'.format(destination)) - where = '{} IN (SELECT {} FROM {}.WILDADMIN.{} where {})'.format( - foreign_key, primary_key, secrets.DATABASE, dataset, events_where) + where = f'{foreign_key} IN (SELECT {primary_key} FROM {secrets.DATABASE}.{secrets.USERNAME}.{dataset} where {events_where})' layer = arcpy.management.MakeTableView(join(sde, destination), destination + '_layer', where) arcpy.management.CopyRows(layer, join(fgdb, destination)) @@ -155,7 +151,7 @@ def copy_related_tables(dataset): csv_name = basename(query_file).replace('sql', 'csv') arcpy.AddMessage(csv_name) with open(query_file, 'r') as file: - query = file.read().format(secrets.DATABASE, formatted_ids) + query = file.read().format(secrets.DATABASE, formatted_ids, secrets.USERNAME) cursor.execute(query) csv_file_path = join(arcpy.env.scratchFolder, csv_name) diff --git a/scripts/sql/Equipment1.sql b/scripts/sql/Equipment1.sql index 25e5dfd..d7b4132 100644 --- a/scripts/sql/Equipment1.sql +++ b/scripts/sql/Equipment1.sql @@ -32,18 +32,18 @@ SELECT e.CATHODE_LEN, e.CATHODE_DIAMETER -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Equipment_evw as e +INNER JOIN {0}.{2}.Equipment_evw as e ON e.EVENT_ID = se.EVENT_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Equipment2Anodes.sql b/scripts/sql/Equipment2Anodes.sql index 7901c85..66b4f5f 100644 --- a/scripts/sql/Equipment2Anodes.sql +++ b/scripts/sql/Equipment2Anodes.sql @@ -20,21 +20,21 @@ SELECT a.EQUIPMENT_ID, a.ANODE_SHAPE -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Equipment_evw as e +INNER JOIN {0}.{2}.Equipment_evw as e ON e.EVENT_ID = se.EVENT_ID -INNER JOIN {0}.WILDADMIN.Anodes_evw as a +INNER JOIN {0}.{2}.Anodes_evw as a ON e.EQUIPMENT_ID = a.EQUIPMENT_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Fish1.sql b/scripts/sql/Fish1.sql index 7747f45..ffe307c 100644 --- a/scripts/sql/Fish1.sql +++ b/scripts/sql/Fish1.sql @@ -24,18 +24,18 @@ SELECT f.WEIGHT, f.NOTES -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Fish_evw as f +INNER JOIN {0}.{2}.Fish_evw as f ON f.EVENT_ID = se.EVENT_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Fish2Diet.sql b/scripts/sql/Fish2Diet.sql index 15d8e50..15db1b0 100644 --- a/scripts/sql/Fish2Diet.sql +++ b/scripts/sql/Fish2Diet.sql @@ -21,21 +21,21 @@ SELECT d.MEASUREMENT_TYPE, d.MEASUREMENT -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Fish_evw as f +INNER JOIN {0}.{2}.Fish_evw as f ON f.EVENT_ID = se.EVENT_ID -INNER JOIN {0}.WILDADMIN.Diet_evw as d +INNER JOIN {0}.{2}.Diet_evw as d ON d.FISH_ID = f.FISH_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Fish3Tags.sql b/scripts/sql/Fish3Tags.sql index 0fb6828..5591948 100644 --- a/scripts/sql/Fish3Tags.sql +++ b/scripts/sql/Fish3Tags.sql @@ -25,21 +25,21 @@ SELECT t.TRANSMITTER_FREQ, t.TRANSMITTER_FREQ_TYPE -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Fish_evw as f +INNER JOIN {0}.{2}.Fish_evw as f ON f.EVENT_ID = se.EVENT_ID -INNER JOIN {0}.WILDADMIN.Tags_evw as t +INNER JOIN {0}.{2}.Tags_evw as t ON t.FISH_ID = f.FISH_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Fish4Health.sql b/scripts/sql/Fish4Health.sql index b32118c..d9c7fff 100644 --- a/scripts/sql/Fish4Health.sql +++ b/scripts/sql/Fish4Health.sql @@ -35,21 +35,21 @@ SELECT h.OPERCLE, h.COLLECTION_PART -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Fish_evw as f +INNER JOIN {0}.{2}.Fish_evw as f ON f.EVENT_ID = se.EVENT_ID -INNER JOIN {0}.WILDADMIN.Health_evw as h +INNER JOIN {0}.{2}.Health_evw as h ON h.FISH_ID = f.FISH_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Habitat1.sql b/scripts/sql/Habitat1.sql index 15f20f9..163a5f6 100644 --- a/scripts/sql/Habitat1.sql +++ b/scripts/sql/Habitat1.sql @@ -41,18 +41,18 @@ SELECT h.BACKWATER, h.PH -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Habitat_evw as h +INNER JOIN {0}.{2}.Habitat_evw as h ON h.EVENT_ID = se.EVENT_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Habitat2Transect.sql b/scripts/sql/Habitat2Transect.sql index 33594d2..ea7e2de 100644 --- a/scripts/sql/Habitat2Transect.sql +++ b/scripts/sql/Habitat2Transect.sql @@ -20,18 +20,18 @@ SELECT t.STARTING_BANK, t.TRANSECT_ID -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Transect_evw as t +INNER JOIN {0}.{2}.Transect_evw as t ON t.EVENT_ID = se.EVENT_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Habitat3TransectMeasurements.sql b/scripts/sql/Habitat3TransectMeasurements.sql index eaa227a..c497d1a 100644 --- a/scripts/sql/Habitat3TransectMeasurements.sql +++ b/scripts/sql/Habitat3TransectMeasurements.sql @@ -21,21 +21,21 @@ SELECT tm.DISTANCE_START, tm.TRANSECT_ID -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -INNER JOIN {0}.WILDADMIN.Transect_evw as t +INNER JOIN {0}.{2}.Transect_evw as t ON t.EVENT_ID = se.EVENT_ID -INNER JOIN {0}.WILDADMIN.TransectMeasurements_evw as tm +INNER JOIN {0}.{2}.TransectMeasurements_evw as tm ON tm.TRANSECT_ID = t.TRANSECT_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE se.EVENT_ID IN ({1}) diff --git a/scripts/sql/Location.sql b/scripts/sql/Location.sql index d7735b0..b6dfbab 100644 --- a/scripts/sql/Location.sql +++ b/scripts/sql/Location.sql @@ -23,15 +23,15 @@ SELECT se.OBSERVERS, se.SURVEY_PURPOSE -FROM {0}.WILDADMIN.SamplingEvents_evw as se +FROM {0}.{2}.SamplingEvents_evw as se -INNER JOIN {0}.WILDADMIN.Stations_evw as s +INNER JOIN {0}.{2}.Stations_evw as s ON s.STATION_ID = se.STATION_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRLakes_evw as l +LEFT OUTER JOIN {0}.{2}.UDWRLakes_evw as l ON l.Permanent_Identifier = s.WATER_ID -LEFT OUTER JOIN {0}.WILDADMIN.UDWRStreams_evw as st +LEFT OUTER JOIN {0}.{2}.UDWRStreams_evw as st ON st.Permanent_Identifier = s.WATER_ID WHERE EVENT_ID IN ({1}) diff --git a/src/components/queryHelpers.test.ts b/src/components/queryHelpers.test.ts index 7a4e844..0efb789 100644 --- a/src/components/queryHelpers.test.ts +++ b/src/components/queryHelpers.test.ts @@ -23,11 +23,11 @@ describe('getStationQuery', () => { }, ]; const expected = removeIrrelevantWhiteSpace( - `(${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM ${config.databaseName}.WILDADMIN.TableOne + `(${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM DB_NAME.USER.TableOne WHERE 1 = 1 AND 1 = 2)) - AND (${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM ${config.databaseName}.WILDADMIN.TableTwo + AND (${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM DB_NAME.USER.TableTwo WHERE 1 = 3)) - AND (${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM ${config.databaseName}.WILDADMIN.TableThree + AND (${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM DB_NAME.USER.TableThree WHERE 1 = 4))`, ); @@ -45,7 +45,7 @@ describe('getStationQuery', () => { table: 'TableOne', }, ]; - const expected = `(1 = 1) AND (${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM ${config.databaseName}.WILDADMIN.TableOne WHERE 1 = 2))`; + const expected = `(1 = 1) AND (${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} FROM DB_NAME.USER.TableOne WHERE 1 = 2))`; const results = getStationQuery(input); expect(results).toBe(expected); @@ -74,9 +74,9 @@ describe('getStationQuery', () => { }, ]; const expected = removeIrrelevantWhiteSpace(`(${config.fieldNames.STATION_ID} IN ( - SELECT ${config.fieldNames.STATION_ID} FROM ${config.databaseName}.WILDADMIN.${config.tableNames.events} + SELECT ${config.fieldNames.STATION_ID} FROM DB_NAME.USER.${config.tableNames.events} WHERE ${config.fieldNames.EVENT_ID} IN ( - SELECT ${config.fieldNames.EVENT_ID} FROM ${config.databaseName}.WILDADMIN.${config.tableNames.fish} + SELECT ${config.fieldNames.EVENT_ID} FROM DB_NAME.USER.${config.tableNames.fish} WHERE 1 = 1 ) AND 2 = 2 ))`); diff --git a/src/components/queryHelpers.ts b/src/components/queryHelpers.ts index d421348..5604bff 100644 --- a/src/components/queryHelpers.ts +++ b/src/components/queryHelpers.ts @@ -15,7 +15,7 @@ export function getStationQuery(queryInfos: QueryInfo[]): string { info.table = config.tableNames.events; info.where = `${config.fieldNames.EVENT_ID} IN ( SELECT ${config.fieldNames.EVENT_ID} FROM - ${config.databaseName}.WILDADMIN.${config.tableNames.fish} + ${config.databaseSecrets.databaseName}.${config.databaseSecrets.user}.${config.tableNames.fish} WHERE ${info.where})`; } @@ -41,7 +41,7 @@ export function getStationQuery(queryInfos: QueryInfo[]): string { } return `${previous}(${config.fieldNames.STATION_ID} IN (SELECT ${config.fieldNames.STATION_ID} - FROM ${config.databaseName}.WILDADMIN.${current} WHERE ${where}))`; + FROM ${config.databaseSecrets.databaseName}.${config.databaseSecrets.user}.${current} WHERE ${where}))`; }, ''); return removeIrrelevantWhiteSpace(query); diff --git a/src/config.js b/src/config.ts similarity index 81% rename from src/config.js rename to src/config.ts index 98ec1a4..b586b34 100644 --- a/src/config.js +++ b/src/config.ts @@ -1,9 +1,14 @@ +if (!process.env.VITE_DATABASE_CONFIG) { + throw new Error('VITE_DATABASE_CONFIG must be set in .env'); +} +const databaseSecrets: { databaseName: string; user: string } = JSON.parse(process.env.VITE_DATABASE_CONFIG); + const config = { MIN_DESKTOP_WIDTH: 768, WEB_MERCATOR_WKID: 3857, MARKER_FILL_COLOR: [234, 202, 0, 0.5], MARKER_OUTLINE_COLOR: [77, 42, 84, 1], - databaseName: 'Electrofishing', + databaseSecrets, fieldNames: { // common STATION_ID: 'STATION_ID',