Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thegamecracks/sqlitediff

A command-line program for generating SQLite schema diffs.
https://github.com/thegamecracks/sqlitediff

migration-tool python python-package sqlite

Last synced: 1 day ago
JSON representation

A command-line program for generating SQLite schema diffs.

Awesome Lists containing this project

README

        

# sqlitediff

[![](https://img.shields.io/github/actions/workflow/status/thegamecracks/sqlitediff/pyright-lint.yml?style=flat-square&label=pyright)](https://microsoft.github.io/pyright/#/)
[![](https://img.shields.io/github/actions/workflow/status/thegamecracks/sqlitediff/python-test.yml?style=flat-square&logo=pytest&label=tests)](https://docs.pytest.org/en/stable/)

A command-line program for generating SQLite schema diffs.

> [!NOTE]
>
> This project is not associated with the sqlitediff package on [PyPI](https://pypi.org/project/sqlitediff/),
> catering towards forensic analysis of SQLite databases. If you're interested
> in 5f0ne's source code, check out [his repository](https://github.com/5f0ne/sqlitediff)!

```sql
$ sqlitediff examples/user_group_1.sql examples/user_group_2.sql
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Modified Objects --

-- Previous table schema for user:
-- CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE sqlitediff_temp (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
INSERT INTO sqlitediff_temp (id, name) SELECT id, name FROM user;
DROP TABLE user;
ALTER TABLE sqlitediff_temp RENAME TO user;

-- Restoring references to user:
CREATE INDEX ix_user_name ON user (name);

-- Previous view schema for user_group_kawaii:
-- CREATE VIEW user_group_kawaii (id) AS
-- SELECT user_id FROM user_group WHERE group_id = 1;
DROP VIEW IF EXISTS user_group_kawaii;
CREATE VIEW user_group_kawaii (id) AS
SELECT user_id FROM user_group WHERE group_id = 2;

-- Previous trigger schema for add_user_to_kawaii_group:
-- CREATE TRIGGER add_user_to_kawaii_group
-- INSERT ON user
-- BEGIN
-- INSERT INTO user_group (user_id, group_id) VALUES (new.id, 1);
-- END;
DROP TRIGGER IF EXISTS add_user_to_kawaii_group;
CREATE TRIGGER add_user_to_kawaii_group
INSERT ON user
BEGIN
INSERT INTO user_group (user_id, group_id) VALUES (new.id, 2);
END;

-- New Objects --

ALTER TABLE "group" ADD COLUMN description TEXT NOT NULL DEFAULT '';

CREATE INDEX ix_group_user ON user_group (group_id, user_id);

CREATE VIEW user_group_all (id) AS
SELECT user_id FROM user_group WHERE group_id = 1;

CREATE TRIGGER add_user_to_all_group
INSERT ON user
BEGIN
INSERT INTO user_group (user_id, group_id) VALUES (new.id, 1);
END;

-- Please verify foreign keys before committing!
-- The following pragma should return 0 rows:
PRAGMA foreign_key_check;

COMMIT;
```

sqlitediff uses the [`sqlite_schema`] table to read your database structure
and compare differences between tables, indices, views, and triggers.
It can parse DDL for tables to determine new, modified, or deleted columns
and tries to produce [ALTER TABLE] statements where supported by SQLite.
Additionally, recommendations will be provided if sqlitediff detects
potential issues with the output script such as table/column renames.

[`sqlite_schema`]: https://sqlite.org/schematab.html
[ALTER TABLE]: https://sqlite.org/lang_altertable.html

## Usage

Assuming you have [Python 3.8+](https://www.python.org/downloads/) and
[Git](https://git-scm.com/), you can install this application with:

```sh
pip install git+https://github.com/thegamecracks/[email protected]
```

After installation, the command-line interface can be used with `sqlitediff`
or `python -m sqlitediff`. It can compare SQLite database files directly
or take .sql scripts which are executed in-memory before comparison.
Run [`sqlitediff --help`](/src/sqlitediff/__main__.py) for more information.

> [!WARNING]
>
> Do not run sqlitediff's output on a production database un-modified
> without first verifying that the script works on a copy. Some modifications
> by themselves can cause constraint violations or data loss due to ambiguity
> in how the changes should be applied or the order in which they are executed.
> In the worst-case scenario, you can use the output as a reference to write
> your own migration script.

## License

This project is written under the [MIT] license.

[MIT]: /LICENSE