jackc / jackc/tern

Combine tern and pg-schema-diff

Open
#132 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
1.3k
Forks
86
PR merge metrics
No merged PRs in 30d

Description

Over time, multiple migrations makes life hard to get a clear view of the actual database schema. I was wondering whether the schema could keept as the "desired schema" and use `pg-schema-diff` to generate the diffs meant to be applied by tern.

For example, at the very beggining, the `schema.sql` looks like this:
```sql
create table t1(
id serial primary key
);
```

Given that the schema is empty, the auto-generated migration should look like this:
```sql
create table t1(
id serial primary key
);
---- create above / drop below ----
drop table t1;
```

Then, someone adds a column to `t1` byediting `schema.sql` file:
```sql
create table t1(
id serial primary key,
placebo varchar not null
);
```

The auto-generated migration should look like this:
```sql
alter table t1 add column placebo varchar not null;
---- create above / drop below ----
alter table t1 drop column placebo;
```

This way, `schema.sql` reprensets the desired state and each migration is automatically generated using `pg-schema-diff`. Notice that both "create above" and "drop below" can be generated using `pg-schema-diff` by inverting from/to flags.

To make this very robust, an `pglite` or similar instance could be used.

Any thoughts?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the proposed schema.sql as the desired schema, then review how tern currently generates migrations and how pg-schema-diff could supply the forward and reverse SQL. Consider whether a pglite or similar instance is needed for comparison. Done means schema changes produce reversible migrations in tern's expected format.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.