Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e2ad2da
Added initial files structure
Ibraam-Nashaat Jun 5, 2025
7270b74
Modified env.py to deal with hypertables
Ibraam-Nashaat Jun 8, 2025
1ea5fac
Modified index creation in env.py
Ibraam-Nashaat Jun 8, 2025
63c6680
Cleaned the files and added comments
Ibraam-Nashaat Jun 12, 2025
caa7100
Added documentation files
Ibraam-Nashaat Jun 12, 2025
cf1ef6d
Added license file
Ibraam-Nashaat Jun 12, 2025
48b0df2
Added more documentation files
Ibraam-Nashaat Jun 12, 2025
dd4324c
Added .env file, removed root path and modified the documentation files
Ibraam-Nashaat Jun 13, 2025
f3af036
Removed .env file
Ibraam-Nashaat Jun 14, 2025
f70ef03
Added database models
Ibraam-Nashaat Jun 16, 2025
999663d
Added more database models
Ibraam-Nashaat Jun 16, 2025
d1af5ad
Completed hegemony cone endpoint
Ibraam-Nashaat Jun 19, 2025
f668e60
Fixed foreign keys errors in models
Ibraam-Nashaat Jun 19, 2025
5ff2a66
Added missing indexes to models
Ibraam-Nashaat Jun 19, 2025
4379d01
Merge remote-tracking branch 'upstream/main' into database-models
Ibraam-Nashaat Jun 19, 2025
975d2ee
Untracked migration files
Ibraam-Nashaat Jun 23, 2025
6ad10dd
Renamed to hegemony_cone.py and added alembic/versions to gitignore
Ibraam-Nashaat Jun 23, 2025
7e92667
Returned some id(primary key) of tables back from BigInteger to Integer
Ibraam-Nashaat Jun 25, 2025
1a4b713
Removed foreign key constraints from some models
Ibraam-Nashaat Jun 25, 2025
df9d065
Completed merge
Ibraam-Nashaat Jun 25, 2025
92c6428
Added networks-endpoint
Ibraam-Nashaat Jun 25, 2025
337759e
Merge branch 'InternetHealthReport:main' into networks-endpoint
Ibraam-Nashaat Jun 27, 2025
bb3e0d3
Added link-delay endpoint
Ibraam-Nashaat Jun 29, 2025
d5867d3
Merge remote-tracking branch 'upstream/main' into link-delay-endpoint
Ibraam-Nashaat Jul 3, 2025
58ef29b
Added link-forwarding endpoint
Ibraam-Nashaat Jul 3, 2025
bad1113
Added /network_delay/locaions endpoint
Ibraam-Nashaat Jul 3, 2025
2f6bfe3
Added /metis/atlas/deployment endpoint
Ibraam-Nashaat Jul 3, 2025
526f316
Added /network_delay endpoint
Ibraam-Nashaat Jul 9, 2025
f7e0068
Added /network_delay/alarms endpoint
Ibraam-Nashaat Jul 9, 2025
23949e1
Change _lte and _gte in controller to __lte and __gte
Ibraam-Nashaat Jul 9, 2025
9bf2ffc
Added /metis/atlas/selection endpoint
Ibraam-Nashaat Jul 9, 2025
74f8bea
Added link/delay/alarms endpoint
Ibraam-Nashaat Jul 14, 2025
f1aede6
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Jul 14, 2025
33a6a4c
Removed link endpoints
Ibraam-Nashaat Jul 14, 2025
6b17dfe
Added /tr-hegemony endpoint
Ibraam-Nashaat Jul 14, 2025
3c56730
Added /disco/events endpoint
Ibraam-Nashaat Jul 14, 2025
a62fda6
Added /hegemony/alarms endpoints
Ibraam-Nashaat Jul 14, 2025
d7cff0f
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Jul 15, 2025
e8eb6b9
Merge branch 'hegemony-alarms-endpoint' of github.com:Ibraam-Nashaat/…
Ibraam-Nashaat Jul 15, 2025
b96500d
Added /hegemony/countries endpoint
Ibraam-Nashaat Jul 15, 2025
8905a2d
Added /hegemony endpoint
Ibraam-Nashaat Jul 15, 2025
5233743
Added /hegemony/prefixes endpoint
Ibraam-Nashaat Jul 15, 2025
2d04bbe
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Jul 16, 2025
65cdd04
Merge branch 'hegemony-prefix-endpoint' of github.com:Ibraam-Nashaat/…
Ibraam-Nashaat Jul 16, 2025
0554319
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Jul 23, 2025
51e7cc9
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Jul 25, 2025
b321230
Added page size ti .env file
Ibraam-Nashaat Jul 25, 2025
8eefb6a
Return recent results in all endpoints when timebin fields are not pr…
Ibraam-Nashaat Aug 6, 2025
d418b75
Updated readme
Ibraam-Nashaat Aug 6, 2025
dc1b335
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Aug 19, 2025
a49e79b
Updated allowed CORS origins
Ibraam-Nashaat Aug 19, 2025
bcd8e8c
Increase version
Ibraam-Nashaat Aug 19, 2025
ba70b06
Merge remote-tracking branch 'upstream/main'
Ibraam-Nashaat Aug 25, 2025
f07135f
Added data-migration.md
Ibraam-Nashaat Aug 25, 2025
1a69bb9
Fixed main.py
Ibraam-Nashaat Aug 25, 2025
075d4b4
Modified data_migration.md
Ibraam-Nashaat Aug 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions docs/data_migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
## 1. Create the target database

First, create the target database `ihr-fastapi` where you want the tables and data to reside:


## 2. Run Alembic migrations

Run Alembic to create the schema and tables inside `ihr-fastapi`:

```bash
alembic upgrade head
```

This ensures the target database has all the required tables ready.

---

## 3. Connect to `ihr-fastapi` using psql

Open a `psql` session to your target database:

```bash
psql -U <USERNAME> -d ihr-fastapi
```

Replace `<USERNAME>` with your PostgreSQL username.

---
## 4. Configure FDW to access the source database (`ihr`)

Inside the `psql` session, run:

```sql
-- Enable the FDW extension
CREATE EXTENSION IF NOT EXISTS postgres_fdw;

-- Create a connection to the ihr DB
CREATE SERVER "<SERVER_NAME>"
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '<HOST>', dbname '<DB_NAME>', port '<PORT>');

-- Map your Postgres user to the remote DB credentials
CREATE USER MAPPING FOR CURRENT_USER
SERVER "<SERVER_NAME>"
OPTIONS (user '<USERNAME>', password '<PASSWORD>');

-- Import only the needed tables into a schema called ihr_src
CREATE SCHEMA IF NOT EXISTS ihr_src;
IMPORT FOREIGN SCHEMA public
FROM SERVER "<SERVER_NAME>"
INTO ihr_src;
```

Replace the following placeholders with your actual values:
- `<SERVER_NAME>`: A name for your foreign server
- `<HOST>`: The hostname where your source database is running
- `<DB_NAME>`: The name of your source database
- `<PORT>`: The PostgreSQL port
- `<USERNAME>`: Your PostgreSQL username for the source database
- `<PASSWORD>`: Your PostgreSQL password for the source database

### Explanation

* **`CREATE EXTENSION`** enables FDW support.
* **`CREATE SERVER`** defines the connection to the source DB (`ihr`).
* **`USER MAPPING`** maps your local PostgreSQL user to the remote DB user/password.
* **`IMPORT FOREIGN SCHEMA`** pulls in table definitions into a local schema (`ihr_src`). These are **foreign tables** that point to the source DB.

---

## 5. Prepare to export migration SQL

We’ll generate a file (`migration.sql`) with `INSERT ... SELECT` statements to copy the data.

In `psql`, run:

```sql
\x off
\pset format unaligned
\pset pager off
\pset tuples_only on
\o migration.sql
```

### Explanation

* **`\x off`** → disables expanded display.
* **`\pset format unaligned`** → outputs raw SQL without table formatting.
* **`\pset pager off`** → prevents paging.
* **`\pset tuples_only on`** → suppresses headers/footers.
* **`\o migration.sql`** → redirects all output into a file named `migration.sql`.

---

## 6. Generate the data migration SQL

Run this query in `psql`:

```sql
SELECT
'INSERT INTO public.' || table_name || ' (' ||
string_agg(column_name, ', ' ORDER BY ordinal_position) ||
') SELECT ' ||
string_agg(column_name, ', ' ORDER BY ordinal_position) ||
' FROM ihr_src.' || table_name ||
CASE
WHEN EXISTS (
SELECT 1 FROM information_schema.columns c2
WHERE c2.table_schema = 'public'
AND c2.table_name = c.table_name
AND c2.column_name = 'timebin'
)
THEN ' WHERE timebin >= NOW() - INTERVAL ''3 months'';'
ELSE ';'
END ||
E'\n' ||
'\echo ''SUCCESS: ' || table_name || ' data inserted'''
FROM information_schema.columns c
WHERE table_schema = 'public'
AND table_name LIKE 'ihr_%'
GROUP BY table_name
ORDER BY table_name;
```

This builds a series of `INSERT INTO ... SELECT ...` statements that:

* Copy all data from `ihr_src.table` to `public.table`.
* If a `timebin` column exists, only copy rows from the **last 3 months**.
* Print a confirmation message (`SUCCESS: ...`) after each table insert.

---

## 7. Exit `psql`

Type:

```sql
\q
```

Now you’ll have a file called `migration.sql` in your current directory.

---

## 8. Run the migration

Finally, execute the migration script against the `ihr-fastapi` database:

```bash
psql -U <USERNAME> -d ihr-fastapi -f migration.sql
```

This will insert data into all your new tables.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
allow_origins=origins,
allow_methods=["*"],
allow_headers=["*"],
)
)