BEGIN;
    -- 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}}

    -- 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}}
COMMIT;

BEGIN;
    -- Disable triggers on each table.
{{~#each tables}}
    ALTER TABLE "{{this.name}}" DISABLE TRIGGER ALL;
{{~/each}}
COMMIT;

BEGIN;
    -- 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}}

    -- 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}}
COMMIT;

BEGIN;
    -- Reenable triggers on each table.
{{~#each tables}}
    ALTER TABLE "{{this.name}}" ENABLE TRIGGER ALL;
{{~/each}}
COMMIT;

Disable database triggers during import
Disable database triggers during import

- This allows `updated_at` field values to be loaded during import.
  Before this change, `updated_at` was set to when the import ran,
  overwriting the values being loaded.
- Also, the import completes in about one fifth the time
  (on my slow MacBook Air).
