{"id":16299589,"url":"https://github.com/dcwatson/pgexplode","last_synced_at":"2025-04-09T21:28:50.324Z","repository":{"id":57452113,"uuid":"252760912","full_name":"dcwatson/pgexplode","owner":"dcwatson","description":"Utility for exploding a PostgreSQL table (and any related data) into separate schemas.","archived":false,"fork":false,"pushed_at":"2021-02-10T20:54:08.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-16T04:19:59.598Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcwatson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-03T14:47:45.000Z","updated_at":"2021-11-03T15:53:32.000Z","dependencies_parsed_at":"2022-09-02T06:44:01.096Z","dependency_job_id":null,"html_url":"https://github.com/dcwatson/pgexplode","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcwatson%2Fpgexplode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcwatson%2Fpgexplode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcwatson%2Fpgexplode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcwatson%2Fpgexplode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcwatson","download_url":"https://codeload.github.com/dcwatson/pgexplode/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248114221,"owners_count":21049997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T20:48:38.910Z","updated_at":"2025-04-09T21:28:50.296Z","avatar_url":"https://github.com/dcwatson.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pgexplode\n\nA utility for exploding a PostgreSQL table (and any related data) into separate schemas.\n\n\n## Example\n\nImagine the following table structure and data in a database named `exploder`:\n\n```sql\nCREATE TABLE tenant (id serial PRIMARY KEY, slug varchar);\nCREATE TABLE related (id serial PRIMARY KEY, tenant_id integer NOT NULL REFERENCES tenant(id), value varchar);\nINSERT INTO tenant (id, slug) VALUES (1, 'alpha'), (2, 'beta');\nINSERT INTO related (tenant_id, value) VALUES\n    (1, 'alpha-value-1'),\n    (1, 'alpha-value-2'),\n    (1, 'alpha-value-3'),\n    (2, 'beta-value-1'),\n    (2, 'beta-value-2'),\n    (2, 'beta-value-3'),\n    (2, 'beta-value-4'),\n    (2, 'beta-value-5')\n;\n```\n\nRunning the following command:\n\n```\npgexplode -d postgres:///exploder tenant.slug\n```\n\nWould create two schemas, `alpha` and `beta` and copy the table data as follows:\n\n```\n+ alpha\n  ~ tenant: 1\n  ~ related: 3\n+ beta\n  ~ tenant: 1\n  ~ related: 5\n```\n\nNot specifying a column for schema naming will automatically generate schemas named `\u003ctable\u003e_\u003cpk\u003e`:\n\n```\npgexplode -d postgres:///exploder tenant\n\n+ tenant_1\n  ~ tenant: 1\n  ~ related: 3\n+ tenant_2\n  ~ tenant: 1\n  ~ related: 5\n```\n\n## Outputting/Debugging SQL\n\nAdding an `--sql` flag to the command above will output the SQL being run, which can be helpful when tweaking or\ndebugging:\n\n```sql\n-- alpha\nDROP SCHEMA IF EXISTS \"alpha\" CASCADE;\nCREATE SCHEMA \"alpha\";\nCREATE TABLE \"alpha\".tenant (LIKE public.tenant INCLUDING ALL);\nINSERT INTO \"alpha\".tenant (SELECT * FROM tenant WHERE id = 1);\nCREATE TABLE \"alpha\".related (LIKE public.related INCLUDING ALL);\nINSERT INTO \"alpha\".related (SELECT related.* FROM related JOIN tenant ON related.tenant_id = tenant.id WHERE tenant.id = 1);\nCREATE SEQUENCE \"alpha\".related_id_seq;\nALTER SEQUENCE \"alpha\".related_id_seq OWNED BY \"alpha\".related.id;\nALTER TABLE \"alpha\".related ALTER id SET DEFAULT nextval('alpha.related_id_seq'::regclass);\nCREATE SEQUENCE \"alpha\".tenant_id_seq;\nALTER SEQUENCE \"alpha\".tenant_id_seq OWNED BY \"alpha\".tenant.id;\nALTER TABLE \"alpha\".tenant ALTER id SET DEFAULT nextval('alpha.tenant_id_seq'::regclass);\nALTER TABLE \"alpha\".related ADD CONSTRAINT related_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES \"alpha\".tenant (id);\n\n-- beta\nDROP SCHEMA IF EXISTS \"beta\" CASCADE;\nCREATE SCHEMA \"beta\";\nCREATE TABLE \"beta\".tenant (LIKE public.tenant INCLUDING ALL);\nINSERT INTO \"beta\".tenant (SELECT * FROM tenant WHERE id = 2);\nCREATE TABLE \"beta\".related (LIKE public.related INCLUDING ALL);\nINSERT INTO \"beta\".related (SELECT related.* FROM related JOIN tenant ON related.tenant_id = tenant.id WHERE tenant.id = 2);\nCREATE SEQUENCE \"beta\".related_id_seq;\nALTER SEQUENCE \"beta\".related_id_seq OWNED BY \"beta\".related.id;\nALTER TABLE \"beta\".related ALTER id SET DEFAULT nextval('beta.related_id_seq'::regclass);\nCREATE SEQUENCE \"beta\".tenant_id_seq;\nALTER SEQUENCE \"beta\".tenant_id_seq OWNED BY \"beta\".tenant.id;\nALTER TABLE \"beta\".tenant ALTER id SET DEFAULT nextval('beta.tenant_id_seq'::regclass);\nALTER TABLE \"beta\".related ADD CONSTRAINT related_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES \"beta\".tenant (id);\n```\n\nYou can see in addition to simply creating copies of each table, `pgexplode` is also making sure the new tables have\ntheir own sequences for serial columns, and tables are properly re-keyed within the new schema.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcwatson%2Fpgexplode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcwatson%2Fpgexplode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcwatson%2Fpgexplode/lists"}