- http://json-schema.org/
- Describes/defines a JSON document
- Very popular and well-tooled, lots of validators in many different languages.
- Great for describing/defining/validating JSON responses, request payloads, etc.
- JSON schema is JSON, so human-readability is mediocre.
- Spec is obtuse.
- Tutorial: https://spacetelescope.github.io/understanding-json-schema/
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 is an effective but naive approach to selecting an authenticated user from a users table. | |
-- The problem is that the query is (intentionally) slow if it has to check a digest, | |
-- but finishes very quickly if the email doesn't match, and so provides a mechanism for user enumeration. | |
select * | |
from users | |
where email = 'daniel@fone.net.nz' | |
and password_digest = crypt('password', password_digest); | |
-- The key goal is to provide a roughly constant time query whether the email or the password is wrong | |
with |
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
-- Create table to hold raw imported data | |
-- | |
DROP TABLE IF EXISTS imported_reports; | |
CREATE TABLE imported_reports ( | |
reference varchar primary key, | |
project_reference varchar, | |
project_name varchar, | |
version varchar, | |
generation_time varchar, | |
-- etc |
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
DROP TABLE imported_reports; | |
VACUUM FULL ANALYZE VERBOSE; |
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
INSERT INTO reports (reference, program_id, generation_time, version) | |
SELECT | |
i.reference | |
programs.id AS program_id, | |
nullif(i.generation_time, '')::timestamp AS generation_time, | |
nullif(i.version, '')::numeric AS version, | |
-- etc | |
FROM imported_reports i | |
JOIN programs ON programs.reference = i.program_reference | |
ON CONFLICT (external_id) DO UPDATE SET |
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
-- Load projects from import | |
-- | |
INSERT INTO projects (reference, name, created_at, updated_at) | |
SELECT | |
i.project_reference AS reference, | |
i.project_name AS name | |
now() AS created_at, | |
now() AS updated_at | |
FROM (SELECT DISTINCT project_reference, project_name FROM imported_reports) i | |
ON CONFLICT (reference) DO UPDATE SET |
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
-- Import data | |
-- | |
COPY imported_reports | |
FROM '/tmp/large-enterprise-export.csv' | |
WITH (format csv, header) | |
; |
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
-- Create table to hold raw imported data | |
-- | |
DROP TABLE IF EXISTS imported_reports; | |
CREATE TABLE imported_reports ( | |
reference varchar primary key, | |
project_reference varchar, | |
project_name varchar, | |
version varchar, | |
generation_time varchar, | |
-- etc |
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
-- Queries a table like this: | |
-- | |
-- id settings updated_at | |
-- 10013 {"reports.url_expiry":400} 2018-04-30 00:00:00 | |
-- 10014 {"setting2":"foo"} 2018-05-01 00:00:00 | |
-- 10015 {"reports.url_expiry":200} 2018-05-02 00:00:00 | |
-- | |
-- to roll up the settings column into: | |
-- | |
-- { "reports.url_expiry" : 200, "setting2" : "foo" } |
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
MyExceptions = [RangeError, RegexpError, IOError].freeze | |
begin | |
raise IOError, "should be rescued" | |
rescue *MyExceptions => e | |
puts e | |
end |