forked from smart-facility/cognicity-schema
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from urbanriskmap/dev
Merge dev into master
- Loading branch information
Showing
10 changed files
with
156 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env bash | ||
|
||
# This script creates a new schema and copies data over from production. | ||
# Run this from the root directory e.g. cognicity-schema/ | ||
|
||
# Define databases | ||
PROD=cognicity | ||
NEW=new_cognicity | ||
|
||
# Set env vars | ||
export PGDATABASE=`echo $NEW` | ||
export DATA=false # Disable default data loading in schema script | ||
export PGHOST=localhost | ||
export PG_DEFAULT_DB=cognicity # Upgrade so this exists | ||
export PGUSER=postgres | ||
|
||
# Create new database, empty schema | ||
source build/run.sh | ||
|
||
# Optional updates to old database | ||
# Edit the old database - replace card id with UUID | ||
## psql -d $PROD -h $PGHOST -U $PGUSER -f schema/reports/grasp/grasp.uuid_upgrade.sql | ||
|
||
# Copy the old data to the new database | ||
pg_dump -a $PROD -h $PGHOST -U $PGUSER --schema=cognicity --disable-triggers | psql -d $NEW -h $PGHOST -U $PGUSER | ||
|
||
# Kill prod connections | ||
psql -d $PROD -h $PGHOST -U $PGUSER -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$PROD' AND pid <> pg_backend_pid();" | ||
|
||
psql -d $NEW -h $PGHOST -U $PGUSER -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$NEW' AND pid <> pg_backend_pid();" | ||
|
||
# Append prod with _old suffix | ||
OLD=$PROD'_old' | ||
psql -d $NEW -h $PGHOST -U $PGUSER -c "ALTER DATABASE \"$PROD\" RENAME TO \"$OLD\"" | ||
|
||
# Rename new to prod | ||
psql -d $OLD -h $PGHOST -U $PGUSER -c "ALTER DATABASE \"$NEW\" RENAME TO \"$PROD\"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-- UPGRADE CARD_ID TO UUID TYPE FOR HISTORIC DATA | ||
|
||
-- Update grasp.cards | ||
-- 1. Create UUID extension | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
-- 2. Move old card ids | ||
ALTER TABLE grasp.cards RENAME card_id to old_card_id; | ||
-- 3. Create new column | ||
ALTER TABLE grasp.cards ADD card_id UUID DEFAULT uuid_generate_v4(); | ||
|
||
-- Now update grasp.reports table | ||
-- 4. Mode old card ids | ||
ALTER TABLE grasp.reports RENAME card_id to old_card_id; | ||
ALTER TABLE grasp.reports ADD card_id UUID; | ||
|
||
-- 5. Update reports | ||
UPDATE grasp.reports SET card_id = grasp.cards.card_id FROM grasp.cards WHERE grasp.reports.old_card_id = grasp.cards.old_card_id; | ||
|
||
-- 6. Drop old columns | ||
ALTER TABLE grasp.cards DROP old_card_id; | ||
ALTER TABLE grasp.reports DROP old_card_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const test = require('unit.js'); | ||
|
||
export default (db, version) => { | ||
// Cards endpoint | ||
describe('Schema version functionality', () => { | ||
|
||
// Test | ||
it ('Can get correct version', (done) => { | ||
|
||
// Check the test data can be output with rem_get_flood function | ||
let query = "SELECT * FROM cognicity.version()"; | ||
db.oneOrNone(query) | ||
.then((data) => { | ||
test.value(data.version).is(version); | ||
done(); | ||
}) | ||
.catch((error) => test.fail(error)); | ||
}); | ||
}); | ||
} |