Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
3,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. do_hr() {
  2. echo "==============================================================="
  3. }
  4.  
  5. #############################################################################
  6. do_hr
  7. echo "Defining database parameters"
  8. do_hr
  9. #############################################################################
  10.  
  11. DB_USER=worldmap
  12. DB_PW=worldmap
  13. DB_HOST=localhost
  14. NEW_DB=worldmap_new
  15. OLD_DB=worldmap
  16. GEONODE_PATH=/code/geonode/
  17. export DATABASE_URL=postgres://$DB_USER:$DB_PW@$DB_HOST:5432/$NEW_DB
  18.  
  19. #############################################################################
  20. do_hr
  21. echo "Removing previous saved database"
  22. do_hr
  23. #############################################################################
  24.  
  25. if [ $( psql -l | grep $NEW_DB | wc -l ) = '1' ]
  26. then
  27. echo "Removing $NEW_DB"
  28. dropdb $NEW_DB
  29. fi
  30.  
  31. #############################################################################
  32. do_hr
  33. echo "Create database $NEW_DB"
  34. do_hr
  35. #############################################################################
  36.  
  37. createdb $NEW_DB
  38.  
  39. #############################################################################
  40. do_hr
  41. echo "Executing geonode migrations"
  42. do_hr
  43. #############################################################################
  44.  
  45. python $GEONODE_PATH/manage.py makemigrations
  46. python $GEONODE_PATH/manage.py migrate account --noinput
  47. python $GEONODE_PATH/manage.py migrate
  48.  
  49. #############################################################################
  50. do_hr
  51. echo "Removing previous saved data"
  52. do_hr
  53. #############################################################################
  54.  
  55. # Clear old layers.
  56. psql -d $NEW_DB -c "DELETE FROM layers_layer"
  57.  
  58. # Clear old resourcebase.
  59. psql -d $NEW_DB -c "DELETE FROM base_resourcebase"
  60.  
  61. # Clear old accounts.
  62. psql -d $NEW_DB -c "DELETE FROM account_account"
  63.  
  64. # Clear old accounts email
  65. psql -d $NEW_DB -c "DELETE FROM account_emailaddress"
  66.  
  67. # Clear old groups.
  68. psql -d $NEW_DB -c "DELETE FROM people_profile_groups"
  69.  
  70. # Clear old users.
  71. psql -d $NEW_DB -c "DELETE FROM people_profile WHERE id > -1"
  72.  
  73.  
  74. #############################################################################
  75. do_hr
  76. echo "Executing migration"
  77. do_hr
  78. #############################################################################
  79.  
  80. # Create view for bbox in legacy database
  81. PGPASSWORD=$DB_PW psql -U $DB_USER -h $DB_HOST $OLD_DB -c "CREATE OR REPLACE VIEW maps_layer_bbox AS SELECT id, cast(bbox[1] as float) as bbox_x0, cast(bbox[2] as float) as bbox_y0, cast(bbox[3] as float) as bbox_x1, cast(bbox[4] as float) as bbox_y1 from (select id, string_to_array(replace(replace(replace(llbbox, ']',''), '[',''), ',',''), ' ') as bbox from maps_layer) as seq"
  82.  
  83. # Create new layers table view with bbox in legacy database.
  84. PGPASSWORD=$DB_PW psql -U $DB_USER -h $DB_HOST $OLD_DB -c "CREATE OR REPLACE VIEW augmented_maps_layer AS SELECT * FROM maps_layer INNER JOIN maps_layer_bbox USING (id) WHERE maps_layer_bbox.bbox_x0 > -181 and maps_layer_bbox.bbox_x1 < 181 and maps_layer_bbox.bbox_y0 > -91 and maps_layer_bbox.bbox_y1 < 90"
  85.  
  86. # Copy users.
  87. PGPASSWORD=$DB_PW psql -U $DB_USER -h $DB_HOST $OLD_DB -c 'copy (select id, password, last_login, is_superuser, username, first_name, last_name, email, is_staff, is_active, date_joined from auth_user) to stdout with csv' | psql $NEW_DB -c 'copy people_profile (id, password, last_login, is_superuser, username, first_name, last_name, email, is_staff, is_active, date_joined) from stdin csv'
  88.  
  89. # Get layers content type needed for polymorphic
  90. ID=$(psql worldmap -c "copy (select id from django_content_type where name like 'layer') to stdout with csv")
  91.  
  92. # Copy items to resourcebase. Removing temporal extent works.
  93.  
  94. # PGPASSWORD=$DB_PW psql -U $DB_USER -h $DB_HOST $OLD_DB -c "copy (select id, $ID, uuid, owner_id, title, date, date_type, abstract, language, supplemental_information, 'EPSG:4326', 'csw_typename', 'csw_schema', 'csw_mdsource', 'csw_type', 'csw_wkt_geometry', false, 0, 0, false, true, bbox_x0, bbox_y0, bbox_x1, bbox_y1, typename, false, temporal_extent_start, temporal_extent_end from augmented_maps_layer) to stdout with csv" | psql $NEW_DB -c "copy base_resourcebase (id, polymorphic_ctype_id, uuid, owner_id, title, date, date_type, abstract, language, supplemental_information, srid, csw_typename, csw_schema, csw_mdsource, csw_type, csw_wkt_geometry, metadata_uploaded, popular_count, share_count, featured, is_published, bbox_x0, bbox_y0, bbox_x1, bbox_y1, detail_url, metadata_uploaded_preserve, temporal_extent_start, temporal_extent_end) from stdin csv"
  95. PGPASSWORD=$DB_PW psql -U $DB_USER -h $DB_HOST $OLD_DB -c "copy (select id, $ID, uuid, owner_id, title, date, date_type, abstract, language, supplemental_information, 'EPSG:4326', 'csw_typename', 'csw_schema', 'csw_mdsource', 'csw_type', 'csw_wkt_geometry', false, 0, 0, false, true, bbox_x0, bbox_y0, bbox_x1, bbox_y1, typename, false from augmented_maps_layer) to stdout with csv" | psql $NEW_DB -c "copy base_resourcebase (id, polymorphic_ctype_id, uuid, owner_id, title, date, date_type, abstract, language, supplemental_information, srid, csw_typename, csw_schema, csw_mdsource, csw_type, csw_wkt_geometry, metadata_uploaded, popular_count, share_count, featured, is_published, bbox_x0, bbox_y0, bbox_x1, bbox_y1, detail_url, metadata_uploaded_preserve) from stdin csv"
  96.  
  97. # Set detail_url as /layers/{{typename}}
  98. psql $NEW_DB -c "UPDATE base_resourcebase SET detail_url = '/layers/'||detail_url;"
  99.  
  100. # Copy items to layer table.
  101. PGPASSWORD=$DB_PW psql -U $DB_USER -h $DB_HOST $OLD_DB -c 'copy (select id, title, abstract, purpose, constraints_other, supplemental_information, distribution_description, data_quality_statement, workspace, store, "storeType", name, typename, name from augmented_maps_layer) to stdout with csv' | psql $NEW_DB -c 'copy layers_layer (resourcebase_ptr_id, title_en, abstract_en, purpose_en, constraints_other_en, supplemental_information_en, distribution_description_en, data_quality_statement_en, workspace, store, "storeType", name, typename, charset) from stdin csv'
  102.  
  103. # Test.
  104. # 2 db
  105. # worldmaplegacy -> old geonode (default).
  106. # data_geonode -> new geonode (default).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement