forked from rust-lang/crates.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-import.sql.hbs
38 lines (32 loc) · 1.07 KB
/
dump-import.sql.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
BEGIN;
-- Disable triggers on each table.
{{#each tables}}
ALTER TABLE "{{this.name}}" DISABLE TRIGGER ALL;
{{/each}}
-- Set defaults for non-nullable columns not included in the dump.
{{#each tables as |table|}}
{{#each column_defaults}}
ALTER TABLE "{{table.name}}" ALTER COLUMN "{{@key}}" SET DEFAULT {{this}};
{{/each}}
{{/each}}
-- Truncate all tables.
{{#each tables}}
TRUNCATE "{{this.name}}" RESTART IDENTITY CASCADE;
{{/each}}
-- Enable this trigger so that `crates.textsearchable_index_col` can be excluded from the export
ALTER TABLE "crates" ENABLE TRIGGER "trigger_crates_tsvector_update";
-- Import the CSV data.
{{#each tables}}
\copy "{{this.name}}" ({{this.columns}}) FROM 'data/{{this.name}}.csv' WITH CSV HEADER
{{/each}}
-- Drop the defaults again.
{{#each tables as |table|}}
{{#each column_defaults}}
ALTER TABLE "{{table.name}}" ALTER COLUMN "{{@key}}" DROP DEFAULT;
{{/each}}
{{/each}}
-- Reenable triggers on each table.
{{#each tables}}
ALTER TABLE "{{this.name}}" ENABLE TRIGGER ALL;
{{/each}}
COMMIT;