Skip to main content

A tool for concatenating files in topological order

Project description

topcat

topological concatenation of files

Description

topcat is a simple tool to concatenate files in a topological order. It is useful when you have a set of files that depend on each other and you want to concatenate them in the right order.

For my usecase this is SQL files.

I like to treat my SQL files as a set of functions and views that depend on each other. I like to keep them in separate files and concatenate them in the right order to create a single file that I can run in my database.

Usage

Lets say you have a directory with the following files:

sql
├── my_other_schema
│   ├── functions
│   │   ├── a.sql
│   │   ├── b.sql
│   │   └── c.sql
│   └── schema.sql
└── my_schema
    ├── functions
    │   └── a.sql
    └── schema.sql

And the content of the files is:

sql/my_schema/schema.sql:

-- name: my_schema

DROP SCHEMA IF EXISTS my_schema CASCADE;
CREATE SCHEMA IF NOT EXISTS my_schema;

sql/my_schema/functions/a.sql:

-- name: my_schema.a
-- dropped_by: my_schema

CREATE FUNCTION my_schema.a() RETURNS INT AS
$$
SELECT 1;
$$ LANGUAGE SQL IMMUTABLE
                PARALLEL SAFE;

sql/my_schema/functions/b.sql:

-- name: my_schema.b
-- dropped_by: my_schema
-- requires: my_schema.a

CREATE FUNCTION my_schema.b() RETURNS INT AS
$$
SELECT my_schema.a() + 1
$$ LANGUAGE SQL;

sql/my_schema/functions/c.sql:

-- name: my_schema.c
-- dropped_by: my_schema
-- requires: my_schema.b

CREATE FUNCTION my_schema.c() RETURNS INT AS
$$
SELECT my_schema.b() + 1
$$ LANGUAGE SQL IMMUTABLE
                PARALLEL SAFE;

sql/my_other_schema/schema.sql:

-- name: my_other_schema

DROP SCHEMA IF EXISTS my_schema CASCADE;
CREATE SCHEMA IF NOT EXISTS my_schema;

sql/my_other_schema/functions/a.sql:

-- name: my_other_schema.a
-- dropped_by: my_other_schema
-- requires: my_schema.b

CREATE FUNCTION my_other_schema.a() RETURNS INT AS
$$
SELECT my_schema.b() + 1
$$ LANGUAGE SQL IMMUTABLE
                PARALLEL SAFE;

So the dependency graph is:

Now you can run topcat to concatenate the files in the right order:

topcat -i tests/input/sql -o tests/output/sql/output.sql

The content of output.sql will be:

-- This file was generated by topcat. To regenerate run:
--
-- topcat -i tests/input/sql -o tests/output/sql/output.sql -v

------------------------------------------------------------------------------------------------------------------------
-- tests/input/sql/my_other_schema/schema.sql
-- name: my_schema

DROP SCHEMA IF EXISTS my_schema CASCADE;
CREATE SCHEMA IF NOT EXISTS my_schema;

------------------------------------------------------------------------------------------------------------------------
-- tests/input/sql/my_other_schema/functions/a.sql
-- name: my_schema.a
-- dropped_by: my_schema

CREATE FUNCTION my_schema.a() RETURNS INT AS
$$
SELECT 1;
$$ LANGUAGE SQL;

------------------------------------------------------------------------------------------------------------------------
-- tests/input/sql/my_other_schema/functions/b.sql
-- name: my_schema.b
-- dropped_by: my_schema
-- requires: my_schema.a

CREATE FUNCTION my_schema.b() RETURNS INT AS
$$
SELECT my_schema.a() + 1
$$ LANGUAGE SQL;

------------------------------------------------------------------------------------------------------------------------
-- tests/input/sql/my_schema/schema.sql
-- name: my_other_schema

DROP SCHEMA IF EXISTS my_other_schema CASCADE;
CREATE SCHEMA IF NOT EXISTS my_other_schema;

------------------------------------------------------------------------------------------------------------------------
-- tests/input/sql/my_schema/functions/a.sql
-- name: my_other_schema.a
-- dropped_by: my_other_schema
-- requires: my_schema.b

CREATE FUNCTION my_other_schema.a() RETURNS INT AS
$$
SELECT my_schema.b() + 1
$$ LANGUAGE SQL IMMUTABLE
                PARALLEL SAFE;

------------------------------------------------------------------------------------------------------------------------
-- tests/input/sql/my_other_schema/functions/c.sql
-- name: my_schema.c
-- dropped_by: my_schema
-- requires: my_schema.b
-- requires: my_other_schema.a

CREATE FUNCTION my_schema.c() RETURNS INT AS
$$
SELECT my_schema.b() + my_other_schema.a() + 1
$$ LANGUAGE SQL;

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

topcat-0.1.0.tar.gz (69.3 kB view hashes)

Uploaded Source

Built Distributions

topcat-0.1.0-py3-none-win_arm64.whl (384.5 kB view hashes)

Uploaded Python 3 Windows ARM64

topcat-0.1.0-py3-none-win_amd64.whl (407.4 kB view hashes)

Uploaded Python 3 Windows x86-64

topcat-0.1.0-py3-none-win32.whl (390.5 kB view hashes)

Uploaded Python 3 Windows x86

topcat-0.1.0-py3-none-musllinux_1_2_x86_64.whl (542.9 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ x86-64

topcat-0.1.0-py3-none-musllinux_1_2_i686.whl (547.6 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ i686

topcat-0.1.0-py3-none-musllinux_1_2_armv7l.whl (480.5 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ ARMv7l

topcat-0.1.0-py3-none-musllinux_1_2_aarch64.whl (498.1 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ ARM64

topcat-0.1.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (496.8 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ x86-64

topcat-0.1.0-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (580.8 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ s390x

topcat-0.1.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (632.1 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ ppc64le

topcat-0.1.0-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (650.7 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ ppc64

topcat-0.1.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl (538.5 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ i686

topcat-0.1.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (459.9 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ ARMv7l

topcat-0.1.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (464.1 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ ARM64

topcat-0.1.0-py3-none-macosx_10_12_x86_64.whl (482.8 kB view hashes)

Uploaded Python 3 macOS 10.12+ x86-64

topcat-0.1.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (943.7 kB view hashes)

Uploaded Python 3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page