Skip to content

Commit d07f025

Browse files
authored
Merge pull request #185 from threefoldtech/master-fix-init-countries
Fix init-countries script
2 parents 055136b + 3892fd2 commit d07f025

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

docs/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ docker-compose up -d
2525
Indexer services should now be started, you can check if it's syncing properly by streaming the logs for the indexer:
2626

2727
```
28-
docker logs indexer_indexer_1 -f
28+
docker logs indexer-ingest-1 -f
2929
```
3030

3131
You should be able to follow tfchain blocks processing:

scripts/countries.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

scripts/init-countries.js

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ const {
1212
DB_USER
1313
} = process.env
1414

15+
// Exit Codes
16+
// 0 - Success
17+
// 2 - Unexpected error
18+
// 10 - Unexpected error on idle client
19+
// 20 - Unexpected error on inserting data to the database
20+
// 30 - Unexpected error on fetching data from the data source
21+
1522
async function main () {
1623
const config = {
1724
user: DB_USER,
@@ -21,33 +28,54 @@ async function main () {
2128
database: DB_NAME
2229
}
2330

31+
const pool = new Pool(config)
32+
pool.on('error', (err, client) => {
33+
console.error(err)
34+
console.error('--- Unexpected error on idle client, exiting ---')
35+
process.exit(10)
36+
})
37+
38+
const client = await pool.connect()
39+
40+
// check first if countries and cities already exist
41+
const ExpectedCountriesCount = 250
42+
const ExpectedCitiesCount = 77786
43+
const countriesQuery = 'SELECT * FROM country'
44+
const citiesQuery = 'SELECT * FROM city'
45+
46+
const countriesExist = await client.query(countriesQuery)
47+
const citiesExist = await client.query(citiesQuery)
48+
49+
if (countriesExist.rowCount >= ExpectedCountriesCount && citiesExist.rowCount >= ExpectedCitiesCount) {
50+
console.log('--- Countries and cities already exist, skipping ---')
51+
process.exit(0)
52+
} else {
53+
console.log('Countries or cities do not exist, creating...')
54+
}
55+
56+
// fetch countries
2457
let countries = []
2558
try {
2659
countries = await getCountries()
2760
} catch (error) {
28-
console.log('--- No Countries were found, a restart is suggested ---')
29-
process.exit(0)
61+
console.error(error)
62+
console.error("--- Can't fetch countries, exiting ---")
63+
process.exit(30)
3064
}
3165

66+
// fetch cities
3267
let cities = []
3368
try {
3469
cities = await getCities()
3570
} catch (error) {
36-
console.log('--- No Cities were found, a restart is suggested ---')
37-
process.exit(0)
71+
console.error(error)
72+
console.error("--- Can't fetch cities, exiting ---")
73+
process.exit(30)
3874
}
3975

40-
const pool = new Pool(config)
41-
pool.on('error', (err, client) => {
42-
console.error('Unexpected error on idle client', err)
43-
process.exit(-1)
44-
})
45-
46-
const client = await pool.connect()
47-
4876
try {
4977
const countryPromises = countries.data.map((country, index) => {
50-
const text = 'INSERT INTO country(id, country_id, name, code, region, subregion, lat, long) VALUES($1, $2, $3, $4, $5, $6, $7, $8)'
78+
const text = 'INSERT INTO country(id, country_id, name, code, region, subregion, lat, long) VALUES($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, code = EXCLUDED.code, region = EXCLUDED.region, subregion = EXCLUDED.subregion, lat = EXCLUDED.lat, long = EXCLUDED.long'
5179
let code = country.cca2
5280
if (!code) {
5381
code = country.cca3
@@ -66,7 +94,11 @@ async function main () {
6694
return client.query(text, [index, index, name, code, region, subregion, lat, long])
6795
})
6896

69-
await Promise.all(countryPromises)
97+
await Promise.all(countryPromises).catch(err => {
98+
console.error(err)
99+
console.error("--- Can't insert countries, exiting ---")
100+
process.exit(20)
101+
})
70102

71103
const query = {
72104
name: 'fetch',
@@ -88,31 +120,38 @@ async function main () {
88120
countryCity = 'Unknown'
89121
}
90122

91-
const text = 'INSERT INTO city(id, city_id, country_id, name) VALUES($1, $2, $3, $4) RETURNING *'
92123
index++
93124

94125
return [index, index, foundCountryID, countryCity]
95126
})
96127
}).filter(g => g)
97128

98-
const inserts = format('INSERT INTO city(id, city_id, country_id, name) VALUES %L', flatten(mappedCities))
129+
const inserts = format('INSERT INTO city(id, city_id, country_id, name) VALUES %L ON CONFLICT (id) DO UPDATE SET country_id = EXCLUDED.country_id, name = EXCLUDED.name', flatten(mappedCities))
99130

100131
// console.log(inserts)
101132
client.query(inserts)
102133
.then(res => {
103134
console.log(res)
104135
})
105-
.catch(err => console.log(err))
106-
.then(process.exit(0))
136+
.catch(err => {
137+
console.error(err);
138+
console.error("--- Can't insert cities, exiting ---")
139+
process.exit(20)
140+
})
141+
.then(_ => {
142+
console.log('--- Countries and cities inserted successfully ---');
143+
process.exit(0);
144+
})
107145

108146
} catch (error) {
109-
console.log(error)
110-
process.exit(0)
147+
console.error(error)
148+
console.error("--- Failed to init countries and cities, exiting ---")
149+
process.exit(2)
111150
}
112151
}
113152

114153
async function getCountries () {
115-
return axios.get('https://restcountries.com/v3/all')
154+
return axios.get('https://raw.githubusercontent.com/threefoldtech/tfchain_graphql/master/scripts/countries.json')
116155
}
117156

118157
async function getCities () {

0 commit comments

Comments
 (0)