Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSM points loaded into db #206

Merged
merged 9 commits into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ In your regular console:
node tools/init-database.js
```

```
node tools/update-tables.js // if you want to add or modify your tables, it doesnt drop the tables, so you're fine
```

you can always use `psql` separately to load and dump data:

```
Expand Down
3 changes: 1 addition & 2 deletions server/database/management/connectToDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var pg = require('pg');
var PRIVATE = require('../../../PRIVATE.json');

var conString = 'postgres://'+ PRIVATE.pg_user + ':' + PRIVATE.pg_pwd + '@localhost:5432/' + PRIVATE.db_name;

var MAX_ATTEMPTS = 10;
Expand All @@ -22,7 +21,7 @@ module.exports = function(){
if(err){
console.error("Couldn't connect to db", conString, err);
if(attempts >= MAX_ATTEMPTS)
reject(err);
reject("Couldn't connect: " + err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

es-lint unadapted :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll clean my own lint mess :p

else {
// wait twice more to give time and not overwhelm the database with useless attempts to connect
console.warn("Retrying in ", 2*time);
Expand Down
19 changes: 6 additions & 13 deletions server/database/management/createTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

var fs = require('fs');

var connectToDB = require('./connectToDB.js');

var createTableScript = fs.readFileSync( require.resolve('./createTables.sql') ).toString();

module.exports = function(db){
console.log('== Creating the database tables ==');

module.exports = function(){
return new Promise(function(resolve, reject){
connectToDB()
.then(function(db){
console.log('== Creating the database tables ==');
db.query(createTableScript, function(err, result) {
if(err) reject(err); else resolve(result);
});

})
.catch(function(err){
console.error('Could not connect', err);
db.query(createTableScript, function(err, result) {
if(err)
reject('Coudn\'t create the tables: ' + err.stack);
else resolve(result);
});
});
};
30 changes: 29 additions & 1 deletion server/database/management/createTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ CREATE TABLE IF NOT EXISTS networks (
color text NOT NULL,
url text DEFAULT NULL
) INHERITS(lifecycle);

DROP TRIGGER IF EXISTS updated_at_networks on networks;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you have a look on issue #191 ? Does this solve the issue ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't...

CREATE TRIGGER updated_at_networks BEFORE UPDATE ON networks FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();


CREATE TABLE IF NOT EXISTS places (
id SERIAL PRIMARY KEY,
name text NOT NULL,
Expand All @@ -50,6 +53,31 @@ CREATE TABLE IF NOT EXISTS places (
lon real NOT NULL,
geom geometry
) INHERITS(lifecycle);

DROP TRIGGER IF EXISTS updated_at_places on places;
CREATE TRIGGER updated_at_places BEFORE UPDATE ON places FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

CREATE INDEX place_geo_index ON places USING GIST (geom);

CREATE TABLE IF NOT EXISTS osmPlaces (
id SERIAL PRIMARY KEY,
osm_id text NOT NULL,
tags json NOT NULL,
lat real NOT NULL,
lon real NOT NULL,
network integer REFERENCES networks (id) NOT NULL,
geom geometry
) INHERITS(lifecycle);

DROP TRIGGER IF EXISTS updated_at_osmPlaces on osmPlaces;
CREATE TRIGGER updated_at_osmPlaces BEFORE UPDATE ON osmPlaces FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();



-- INDEXES
DROP INDEX IF EXISTS place_geo_index;
CREATE INDEX place_geo_index ON places USING GIST (geom);

DROP INDEX IF EXISTS osmPlace_geo_index;
CREATE INDEX osmPlace_geo_index ON osmPlaces USING GIST (geom)


24 changes: 22 additions & 2 deletions server/database/management/declarations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// autogenerated by sql-generate v1.1.0 on Wed Dec 09 2015 16:38:42 GMT+0000 (UTC)
// autogenerated by sql-generate v1.1.0 on Fri Mar 18 2016 09:36:00 GMT+0100 (CET)

var sql = require('sql');

Expand Down Expand Up @@ -66,6 +66,25 @@ exports.networks = sql.define({
});


/**
* SQL definition for public.osmplaces
*/
exports.osmplaces = sql.define({
name: 'osmplaces',
columns: [
{ name: 'created_at' },
{ name: 'updated_at' },
{ name: 'id' },
{ name: 'osm_id' },
{ name: 'tags' },
{ name: 'lat' },
{ name: 'lon' },
{ name: 'network' },
{ name: 'geom' }
]
});


/**
* SQL definition for public.places
*/
Expand Down Expand Up @@ -120,7 +139,8 @@ exports.raster_columns = sql.define({
{ name: 'pixel_types' },
{ name: 'nodata_values' },
{ name: 'out_db' },
{ name: 'extent' }
{ name: 'extent' },
{ name: 'spatial_index' }
]
});

Expand Down
15 changes: 6 additions & 9 deletions server/database/management/dropAllTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

var fs = require('fs');

var connectToDB = require('./connectToDB.js');

var dropTableScript = fs.readFileSync( require.resolve('./dropAllTables.sql') ).toString();

module.exports = function(){
module.exports = function(db){
console.warn('\n\t=====\n\nWARNING! Dropping all tables!\n\n\t=====\n');

return new Promise(function(resolve, reject){
connectToDB().then(function(db){
db.query(dropTableScript, function(err, result) {
if(err) reject(err); else resolve(result);
});

}).catch(reject);
db.query(dropTableScript, function(err, result) {
if(err)
reject('Coudn\'t drop the tables: ' + err);
else resolve(result);
});
});
};
1 change: 1 addition & 0 deletions server/database/management/dropAllTables.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- Destruction --
DROP TABLE IF EXISTS networks CASCADE;
DROP TABLE IF EXISTS places CASCADE;
DROP TABLE IF EXISTS osmplaces CASCADE;
31 changes: 31 additions & 0 deletions server/database/management/generateDecl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node

"use strict";

var fs = require('fs');
var path = require('path');

var generateSqlDefinition = require('sql-generate');

var PRIVATE = require('../../../PRIVATE.json');
var conString = 'postgres://'+ PRIVATE.pg_user + ':' + PRIVATE.pg_pwd + '@localhost:5432/' + PRIVATE.db_name;


module.exports = function(){
console.log('== Generating definitions ==');

return new Promise(function(resolve, reject){
generateSqlDefinition({ dsn: conString }, function(err, definitions) {
if (err)
reject('Coudn\'t generate definitions: ' + err);

fs.writeFileSync(path.join(__dirname, './declarations.js'), definitions.buffer);
console.log('Definitions generated');
resolve();
});

process.on('uncaughtException', function(err) {
reject('Coudn\'t generate definitions, caught exception: ', err);
});
});
};
Loading