Merge lp:~gnuoy/ubuntu-geonames/swap-tables-in into lp:ubuntu-geonames

Proposed by Liam Young
Status: Merged
Merged at revision: 27
Proposed branch: lp:~gnuoy/ubuntu-geonames/swap-tables-in
Merge into: lp:ubuntu-geonames
Diff against target: 160 lines (+72/-29)
1 file modified
import-geonames.sh (+72/-29)
To merge this branch: bzr merge lp:~gnuoy/ubuntu-geonames/swap-tables-in
Reviewer Review Type Date Requested Status
Ubuntu Geonames Hackers Pending
Review via email: mp+185459@code.launchpad.net

Description of the change

Allow new data to be loaded alongside old data and then swapped in.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'import-geonames.sh'
2--- import-geonames.sh 2013-05-28 10:19:18 +0000
3+++ import-geonames.sh 2013-09-13 10:35:56 +0000
4@@ -37,11 +37,13 @@
5 tail -n +2 iso-languagecodes.txt > iso-languagecodes.txt.tmp
6 grep -v '^#' countryInfo.txt > countryInfo.txt.tmp
7 tail -n +2 timeZones.txt > timeZones.txt.tmp
8-
9+
10+LOAD_POSTFIX="_load"
11+BACKUP_POSTFIX="_bkup"
12 $PSQL_CMD <<EOT
13 BEGIN;
14-DROP TABLE IF EXISTS geoname;
15-CREATE TABLE geoname (
16+DROP TABLE IF EXISTS geoname${LOAD_POSTFIX};
17+CREATE TABLE geoname${LOAD_POSTFIX} (
18 geonameid int,
19 name varchar(200),
20 asciiname varchar(200),
21@@ -62,10 +64,10 @@
22 timezone varchar(40),
23 moddate date
24 );
25-\copy geoname (geonameid,name,asciiname,alternatenames,latitude,longitude,fclass,fcode,country,cc2, admin1,admin2,admin3,admin4,population,elevation,gtopo30,timezone,moddate) from $WORKPATH/allCountries.txt null as ''
26+\copy geoname${LOAD_POSTFIX} (geonameid,name,asciiname,alternatenames,latitude,longitude,fclass,fcode,country,cc2, admin1,admin2,admin3,admin4,population,elevation,gtopo30,timezone,moddate) from $WORKPATH/allCountries.txt null as ''
27
28-DROP TABLE IF EXISTS alternatename;
29-CREATE TABLE alternatename (
30+DROP TABLE IF EXISTS alternatename${LOAD_POSTFIX};
31+CREATE TABLE alternatename${LOAD_POSTFIX} (
32 alternatenameId int,
33 geonameid int,
34 isoLanguage varchar(7),
35@@ -75,10 +77,10 @@
36 isColloquial boolean,
37 isHistoric boolean
38 );
39-\copy alternatename (alternatenameid,geonameid,isoLanguage,alternateName,isPreferredName,isShortName,isColloquial,isHistoric) from $WORKPATH/alternateNames.txt null as '';
40+\copy alternatename${LOAD_POSTFIX} (alternatenameid,geonameid,isoLanguage,alternateName,isPreferredName,isShortName,isColloquial,isHistoric) from $WORKPATH/alternateNames.txt null as '';
41
42-DROP TABLE IF EXISTS countryinfo;
43-CREATE TABLE countryinfo (
44+DROP TABLE IF EXISTS countryinfo${LOAD_POSTFIX};
45+CREATE TABLE countryinfo${LOAD_POSTFIX} (
46 iso_alpha2 char(2),
47 iso_alpha3 char(3),
48 iso_numeric integer,
49@@ -99,50 +101,91 @@
50 neighbours char(50),
51 equivalentFipsCode char(10)
52 );
53-\copy countryInfo (iso_alpha2,iso_alpha3,iso_numeric,fips_code,name,capital,areaInSqKm,population,continent,tld,currency,currencyName,Phone,postalCodeFormat,postalCodeRegex,languages,geonameId,neighbours,equivalentFipsCode) from $WORKPATH/countryInfo.txt.tmp null as ''
54+\copy countryInfo${LOAD_POSTFIX} (iso_alpha2,iso_alpha3,iso_numeric,fips_code,name,capital,areaInSqKm,population,continent,tld,currency,currencyName,Phone,postalCodeFormat,postalCodeRegex,languages,geonameId,neighbours,equivalentFipsCode) from $WORKPATH/countryInfo.txt.tmp null as ''
55
56-DROP TABLE IF EXISTS iso_languagecodes;
57-CREATE TABLE iso_languagecodes(
58+DROP TABLE IF EXISTS iso_languagecodes${LOAD_POSTFIX};
59+CREATE TABLE iso_languagecodes${LOAD_POSTFIX}(
60 iso_639_3 CHAR(4),
61 iso_639_2 VARCHAR(50),
62 iso_639_1 VARCHAR(50),
63 language_name VARCHAR(200)
64 );
65-\copy iso_languagecodes (iso_639_3, iso_639_2, iso_639_1, language_name) from $WORKPATH/iso-languagecodes.txt.tmp null as ''
66+\copy iso_languagecodes${LOAD_POSTFIX} (iso_639_3, iso_639_2, iso_639_1, language_name) from $WORKPATH/iso-languagecodes.txt.tmp null as ''
67
68-DROP TABLE IF EXISTS admin1codes;
69-CREATE TABLE admin1codes (
70+DROP TABLE IF EXISTS admin1codes${LOAD_POSTFIX};
71+CREATE TABLE admin1codes${LOAD_POSTFIX} (
72 code varchar(10),
73 name TEXT,
74 nameAscii TEXT,
75 geonameid int
76 );
77-\copy admin1codes (code,name,nameAscii,geonameid) from $WORKPATH/admin1CodesASCII.txt null as ''
78+\copy admin1codes${LOAD_POSTFIX} (code,name,nameAscii,geonameid) from $WORKPATH/admin1CodesASCII.txt null as ''
79
80-DROP TABLE IF EXISTS timeZones;
81-CREATE TABLE timeZones (
82+DROP TABLE IF EXISTS timeZones${LOAD_POSTFIX};
83+CREATE TABLE timeZones${LOAD_POSTFIX} (
84 code CHAR(2),
85 timeZoneId VARCHAR(200),
86 GMT_offset numeric(3,1),
87 DST_offset numeric(3,1),
88 RAW_offset numeric(3,1)
89 );
90-\copy timeZones (code,timeZoneId,GMT_offset,DST_offset,RAW_offset) from $WORKPATH/timeZones.txt.tmp null as ''
91+\copy timeZones${LOAD_POSTFIX} (code,timeZoneId,GMT_offset,DST_offset,RAW_offset) from $WORKPATH/timeZones.txt.tmp null as ''
92
93-DROP TABLE IF EXISTS continentCodes;
94-CREATE TABLE continentCodes (
95+DROP TABLE IF EXISTS continentCodes${LOAD_POSTFIX};
96+CREATE TABLE continentCodes${LOAD_POSTFIX} (
97 code CHAR(2),
98 name VARCHAR(20),
99 geonameid INT
100 );
101-INSERT INTO continentCodes VALUES ('AF', 'Africa', 6255146);
102-INSERT INTO continentCodes VALUES ('AS', 'Asia', 6255147);
103-INSERT INTO continentCodes VALUES ('EU', 'Europe', 6255148);
104-INSERT INTO continentCodes VALUES ('NA', 'North America', 6255149);
105-INSERT INTO continentCodes VALUES ('OC', 'Oceania', 6255150);
106-INSERT INTO continentCodes VALUES ('SA', 'South America', 6255151);
107-INSERT INTO continentCodes VALUES ('AN', 'Antarctica', 6255152);
108-
109+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('AF', 'Africa', 6255146);
110+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('AS', 'Asia', 6255147);
111+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('EU', 'Europe', 6255148);
112+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('NA', 'North America', 6255149);
113+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('OC', 'Oceania', 6255150);
114+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('SA', 'South America', 6255151);
115+INSERT INTO continentCodes${LOAD_POSTFIX} VALUES ('AN', 'Antarctica', 6255152);
116+COMMIT;
117+EOT
118+
119+# If the live tables exist, back them up and drop the indexes
120+#TABLES_PRESENT=$(psql $PGUSER_PARAM $PGPASS $PGHOST $PGPORT $PGDBNAME -A -q -t -c "select count(*) from pg_tables where tablename='geoname'")
121+TABLES_PRESENT=$($PSQL_CMD -A -q -t -c "select count(*) from pg_tables where tablename='geoname'")
122+if [[ $TABLES_PRESENT == 1 ]]; then
123+ echo "Backing up current tables"
124+ $PSQL_CMD <<EOT
125+BEGIN;
126+DROP TABLE IF EXISTS geoname${BACKUP_POSTFIX};
127+DROP TABLE IF EXISTS alternatename${BACKUP_POSTFIX};
128+DROP TABLE IF EXISTS countryinfo${BACKUP_POSTFIX};
129+DROP TABLE IF EXISTS iso_languagecodes${BACKUP_POSTFIX};
130+DROP TABLE IF EXISTS admin1codes${BACKUP_POSTFIX};
131+DROP TABLE IF EXISTS timeZones${BACKUP_POSTFIX};
132+DROP TABLE IF EXISTS continentCodes${BACKUP_POSTFIX};
133+DROP INDEX IF EXISTS geoname_id_idx;
134+DROP INDEX IF EXISTS geoname_admin1codes_code_idx;
135+DROP INDEX IF EXISTS geoname_countryinfo_isoalpha2_idx;
136+DROP INDEX IF EXISTS geoname_alternatename_idx;
137+ALTER TABLE geoname RENAME TO geoname${BACKUP_POSTFIX};
138+ALTER TABLE alternatename RENAME TO alternatename${BACKUP_POSTFIX};
139+ALTER TABLE countryinfo RENAME TO countryinfo${BACKUP_POSTFIX};
140+ALTER TABLE iso_languagecodes RENAME TO iso_languagecodes${BACKUP_POSTFIX};
141+ALTER TABLE admin1codes RENAME TO admin1codes${BACKUP_POSTFIX};
142+ALTER TABLE timeZones RENAME TO timeZones${BACKUP_POSTFIX};
143+ALTER TABLE continentCodes RENAME TO continentCodes${BACKUP_POSTFIX};
144+COMMIT;
145+EOT
146+fi
147+
148+# Put the load tables live and rebuild the indexes
149+$PSQL_CMD <<EOT
150+BEGIN;
151+ALTER TABLE geoname${LOAD_POSTFIX} RENAME TO geoname;
152+ALTER TABLE alternatename${LOAD_POSTFIX} RENAME TO alternatename;
153+ALTER TABLE countryinfo${LOAD_POSTFIX} RENAME TO countryinfo;
154+ALTER TABLE iso_languagecodes${LOAD_POSTFIX} RENAME TO iso_languagecodes;
155+ALTER TABLE admin1codes${LOAD_POSTFIX} RENAME TO admin1codes;
156+ALTER TABLE timeZones${LOAD_POSTFIX} RENAME TO timeZones;
157+ALTER TABLE continentCodes${LOAD_POSTFIX} RENAME TO continentCodes;
158 CREATE INDEX geoname_id_idx ON geoname(geonameid);
159 CREATE INDEX geoname_admin1codes_code_idx ON admin1codes(code);
160 CREATE INDEX geoname_countryinfo_isoalpha2_idx ON countryinfo(iso_alpha2);

Subscribers

People subscribed via source and target branches

to all changes: