-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: create workflow to test schema creation (ws)
- Loading branch information
Showing
4 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
name: Create Giswater Schema (WS) | ||
|
||
on: | ||
push: | ||
branches: [ dev-3.6-manage-views ] | ||
pull_request: | ||
branches: [ dev-3.6-manage-views ] | ||
|
||
jobs: | ||
setup-and-test-db: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install Python dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r test/requirements.txt | ||
- name: Setup PostgreSQL and PostGIS for Linux/macOS/Windows | ||
uses: nyurik/action-setup-postgis@v2.1 | ||
with: | ||
database: giswater_test_db | ||
|
||
- name: Install pgrouting & postgis_raster | ||
env: | ||
PGPASSWORD: postgres | ||
run: | | ||
sudo apt-get install postgresql-14-pgrouting | ||
psql -h localhost -U postgres -d giswater_test_db -c 'CREATE EXTENSION pgrouting;' | ||
psql -h localhost -U postgres -d giswater_test_db -c 'CREATE EXTENSION postgis_raster;' | ||
- name: Replace variables in SQL files | ||
run: python test/replace_vars.py | ||
|
||
- name: Create sample schema | ||
env: | ||
PGPASSWORD: postgres | ||
run: python test/execute_sql_files.py ws | ||
|
||
- name: Verify Database | ||
env: | ||
PGPASSWORD: postgres | ||
run: | | ||
# Check PostGIS version | ||
postgis_version=$(psql -h localhost -U postgres -d giswater_test_db -t -c "SELECT postgis_full_version();") | ||
if [ -z "$postgis_version" ]; then | ||
echo "PostGIS extension not found or not installed correctly." | ||
exit 1 | ||
else | ||
echo "PostGIS version:" | ||
echo "$postgis_version" | ||
fi | ||
# Check pgRouting version | ||
pgrouting_version=$(psql -h localhost -U postgres -d giswater_test_db -t -c "SELECT * FROM pgr_version();") | ||
if [ -z "$pgrouting_version" ]; then | ||
echo "pgRouting extension not found or not installed correctly." | ||
exit 1 | ||
else | ||
echo "pgRouting version:" | ||
echo "$pgrouting_version" | ||
fi | ||
# Check features | ||
arcs=$(psql -h localhost -U postgres -d giswater_test_db -t -c "SELECT count(*) FROM ws_36.v_edit_arc;") | ||
if [ -z "$arcs" ]; then | ||
echo "No arcs found in the v_edit_arc table." | ||
exit 1 | ||
else | ||
echo "Arcs found:" | ||
echo "$tables" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os | ||
import argparse | ||
import psycopg2 | ||
|
||
|
||
def execute_sql_file(conn, file_path): | ||
with open(file_path, 'r') as file: | ||
sql_content = file.read() | ||
with conn.cursor() as cursor: | ||
try: | ||
cursor.execute(sql_content) | ||
conn.commit() | ||
print(f"Executed {file_path} successfully.") | ||
except Exception as e: | ||
conn.rollback() | ||
print(f"Error executing {file_path}: {e}") | ||
raise | ||
|
||
|
||
def connect_to_db(): | ||
# Database connection parameters | ||
db_params = { | ||
'dbname': 'giswater_test_db', | ||
'user': 'postgres', | ||
'password': os.getenv('PGPASSWORD', 'postgres'), | ||
'host': 'localhost', | ||
'port': 5432 | ||
} | ||
|
||
# Connect to the PostgreSQL database | ||
conn = psycopg2.connect(**db_params) | ||
return conn | ||
|
||
|
||
def main(project_type): | ||
print(f"Project type: {project_type}") | ||
|
||
conn = connect_to_db() | ||
|
||
# Define the root directories to process | ||
root_directories = ["utils", f"{project_type}", "i18n/en_US"] | ||
exclude_prefix = "ud_" if project_type == "ws" else "ws_" | ||
|
||
# Execute SQL files in the root directories | ||
for root_dir in root_directories: | ||
print(f"Processing root directory: {root_dir}") | ||
for root, _, files in os.walk(root_dir): | ||
for file in sorted(files): | ||
if file.endswith(".sql") and exclude_prefix not in file: | ||
file_path = os.path.join(root, file) | ||
execute_sql_file(conn, file_path) | ||
|
||
# Define the base updates directory | ||
updates_dir = "updates/36" | ||
|
||
# Check if the updates directory exists and process it | ||
if os.path.isdir(updates_dir): | ||
for root, _, files in os.walk(updates_dir): | ||
for file in sorted(files): | ||
if file.endswith(".sql") and exclude_prefix not in file: | ||
file_path = os.path.join(root, file) | ||
execute_sql_file(conn, file_path) | ||
else: | ||
print(f"Directory {updates_dir} does not exist") | ||
|
||
# Execute child views | ||
execute_sql_file(conn, f"childviews/en_US/{project_type}_schema_model.sql") | ||
|
||
# Execute last process command | ||
with conn.cursor() as cursor: | ||
lastprocess_command = f""" | ||
SELECT {project_type}_36.gw_fct_admin_schema_lastprocess( | ||
'{{"client":{{"device":4, "lang":"en_US"}}, "data":{{"isNewProject":"TRUE", "gwVersion":"3.6.012", "projectType":"{project_type.upper()}", "epsg":25831, "descript":"{project_type}_36", "name":"{project_type}_36", "author":"postgres", "date":"29-07-2024"}}}}' | ||
); | ||
""" | ||
cursor.execute(lastprocess_command) | ||
conn.commit() | ||
|
||
# Define the example directory | ||
example_dir = f"example/user/{project_type}" | ||
|
||
# Check if the example directory exists and process it | ||
if os.path.isdir(example_dir): | ||
for root, _, files in os.walk(example_dir): | ||
for file in sorted(files): | ||
if file.endswith(".sql") and exclude_prefix not in file: | ||
file_path = os.path.join(root, file) | ||
execute_sql_file(conn, file_path) | ||
else: | ||
print(f"Directory {example_dir} does not exist") | ||
|
||
# Close the database connection | ||
conn.close() | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description='Execute SQL files of a certain project type.') | ||
parser.add_argument('project_type', type=str, help='Project type. Must be "ws" or "ud"') | ||
args = parser.parse_args() | ||
main(args.project_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
|
||
def replace_vars_in_file(file_path, replacements): | ||
with open(file_path, 'r') as file: | ||
content = file.read() | ||
for old, new in replacements.items(): | ||
content = content.replace(old, new) | ||
with open(file_path, 'w') as file: | ||
file.write(content) | ||
|
||
def main(): | ||
sql_dir = './' | ||
replacements = { | ||
'SCHEMA_NAME': 'ws_36', | ||
'SRID_VALUE': '25831', | ||
# Add more replacements as needed | ||
} | ||
for root, dirs, files in os.walk(sql_dir): | ||
for file in files: | ||
if file.endswith('.sql'): | ||
replace_vars_in_file(os.path.join(root, file), replacements) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
psycopg2 |