sqlite-migrate


Namesqlite-migrate JSON
Version 0.1b0 PyPI version JSON
download
home_page
SummaryA simple database migration system for SQLite, based on sqlite-utils
upload_time2023-10-27 23:47:43
maintainer
docs_urlNone
authorSimon Willison
requires_python
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqlite-migrate

[![PyPI](https://img.shields.io/pypi/v/sqlite-migrate.svg)](https://pypi.org/project/sqlite-migrate/)
[![Changelog](https://img.shields.io/github/v/release/simonw/sqlite-migrate?include_prereleases&label=changelog)](https://sqlite-migrate.datasette.io/en/stable/changelog.html)
[![Tests](https://github.com/simonw/sqlite-migrate/workflows/Test/badge.svg)](https://github.com/simonw/sqlite-migrate/actions?query=workflow%3ATest)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/sqlite-migrate/blob/main/LICENSE)

A simple database migration system for SQLite, based on [sqlite-utils](https://sqlite-utils.datasette.io/).

**This project is an early alpha. Expect breaking changes.**

## Installation

This tool works as a plugin for `sqlite-utils`. First [install that](https://sqlite-utils.datasette.io/en/stable/installation.html):

```bash
pip install sqlite-utils
```
Then install this plugin like so:
```bash
sqlite-utils install sqlite-migrate
```
## Migration files

This tool works against migration files. A migration file looks like this:

```python
from sqlite_migrate import Migrations

# Pick a unique name here - it must not clash with other migration sets that
# the user might run against the same database.

migration = Migrations("creatures")

# Use this decorator against functions that implement migrations
@migration()
def m001_create_table(db):
    # db is a sqlite-utils Database instance
    db["creatures"].create(
        {"id": int, "name": str, "species": str},
        pk="id"
    )

@migration()
def m002_add_weight(db):
    # db is a sqlite-utils Database instance
    db["creatures"].add_column("weight", float)
```
Here is [documentation on the Database instance](https://sqlite-utils.datasette.io/en/stable/python-api.html) passed to each migration function.

## Running migrations

Running this command will execute those migrations in sequence against the specified database file.

Call `migrate` with a path to your database and a path to the migrations file you want to apply:
```bash
sqlite-utils migrate creatures.db path/to/migrations.py
```
Running this multiple times will have no additional affect, unless you add more migration functions to the file.

If you call it without arguments it will search for and apply any `migrations.py` files in the current directory or any of its subdirectories.

You can also pass the path to a directory, in which case all `migrations.py` files in that directory and its subdirectories will be applied:

```bash
sqlite-utils migrate creatures.db path/to/parent/
```
When applying a single migrations file you can use the `--stop-before` option to apply all migrations up to but excluding the specified migration:

```bash
sqlite-utils migrate creatures.db path/to/migrations.py --stop-before m002_add_weight
```

## Listing migrations

Add `--list` to list migrations without running them, for example:

```bash
sqlite-utils migrate creatures.db --list
```
The output will look something like this:
```
Migrations for: creatures

  Applied:
    m001_create_table - 2023-07-23 04:09:40.324002
    m002_add_weight - 2023-07-23 04:09:40.324649
    m003_add_age - 2023-07-23 04:09:44.441616
    m003_cleanup - 2023-07-23 04:09:44.443394
    m004_cleanup - 2023-07-23 04:09:44.444184
    m005_cleanup - 2023-07-23 04:09:44.445389
    m006_cleanup - 2023-07-23 04:09:44.446742
    m007_cleanup - 2023-07-23 04:16:02.529983

  Pending:
    m008_cleanup
```

## Verbose mode

Add `-v` or `--verbose` for verbose output, which will show the schema before and after the migrations were applied along with a diff:

```bash
sqlite-utils migrate creatures.db --verbose
```
Example output:
```
Migrating creatures.db

Schema before:

  CREATE TABLE "_sqlite_migrations" (
     [migration_set] TEXT,
     [name] TEXT,
     [applied_at] TEXT,
     PRIMARY KEY ([migration_set], [name])
  );
  CREATE TABLE [creatures] (
     [id] INTEGER PRIMARY KEY,
     [name] TEXT,
     [species] TEXT
  , [weight] FLOAT);

Schema after:

  CREATE TABLE "_sqlite_migrations" (
     [migration_set] TEXT,
     [name] TEXT,
     [applied_at] TEXT,
     PRIMARY KEY ([migration_set], [name])
  );
  CREATE TABLE "creatures" (
     [id] INTEGER PRIMARY KEY,
     [name] TEXT,
     [species] TEXT,
     [weight] FLOAT,
     [age] INTEGER,
     [shoe_size] INTEGER
  );

Schema diff:

    [applied_at] TEXT,
    PRIMARY KEY ([migration_set], [name])
  );
-CREATE TABLE [creatures] (
+CREATE TABLE "creatures" (
    [id] INTEGER PRIMARY KEY,
    [name] TEXT,
-   [species] TEXT
-, [weight] FLOAT);
+   [species] TEXT,
+   [weight] FLOAT,
+   [age] INTEGER,
+   [shoe_size] INTEGER
+);
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "sqlite-migrate",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Simon Willison",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/13/86/1463a00d3c4bdb707c0ed4077d17687465a0aa9444593f66f6c4b49e39b5/sqlite-migrate-0.1b0.tar.gz",
    "platform": null,
    "description": "# sqlite-migrate\n\n[![PyPI](https://img.shields.io/pypi/v/sqlite-migrate.svg)](https://pypi.org/project/sqlite-migrate/)\n[![Changelog](https://img.shields.io/github/v/release/simonw/sqlite-migrate?include_prereleases&label=changelog)](https://sqlite-migrate.datasette.io/en/stable/changelog.html)\n[![Tests](https://github.com/simonw/sqlite-migrate/workflows/Test/badge.svg)](https://github.com/simonw/sqlite-migrate/actions?query=workflow%3ATest)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/sqlite-migrate/blob/main/LICENSE)\n\nA simple database migration system for SQLite, based on [sqlite-utils](https://sqlite-utils.datasette.io/).\n\n**This project is an early alpha. Expect breaking changes.**\n\n## Installation\n\nThis tool works as a plugin for `sqlite-utils`. First [install that](https://sqlite-utils.datasette.io/en/stable/installation.html):\n\n```bash\npip install sqlite-utils\n```\nThen install this plugin like so:\n```bash\nsqlite-utils install sqlite-migrate\n```\n## Migration files\n\nThis tool works against migration files. A migration file looks like this:\n\n```python\nfrom sqlite_migrate import Migrations\n\n# Pick a unique name here - it must not clash with other migration sets that\n# the user might run against the same database.\n\nmigration = Migrations(\"creatures\")\n\n# Use this decorator against functions that implement migrations\n@migration()\ndef m001_create_table(db):\n    # db is a sqlite-utils Database instance\n    db[\"creatures\"].create(\n        {\"id\": int, \"name\": str, \"species\": str},\n        pk=\"id\"\n    )\n\n@migration()\ndef m002_add_weight(db):\n    # db is a sqlite-utils Database instance\n    db[\"creatures\"].add_column(\"weight\", float)\n```\nHere is [documentation on the Database instance](https://sqlite-utils.datasette.io/en/stable/python-api.html) passed to each migration function.\n\n## Running migrations\n\nRunning this command will execute those migrations in sequence against the specified database file.\n\nCall `migrate` with a path to your database and a path to the migrations file you want to apply:\n```bash\nsqlite-utils migrate creatures.db path/to/migrations.py\n```\nRunning this multiple times will have no additional affect, unless you add more migration functions to the file.\n\nIf you call it without arguments it will search for and apply any `migrations.py` files in the current directory or any of its subdirectories.\n\nYou can also pass the path to a directory, in which case all `migrations.py` files in that directory and its subdirectories will be applied:\n\n```bash\nsqlite-utils migrate creatures.db path/to/parent/\n```\nWhen applying a single migrations file you can use the `--stop-before` option to apply all migrations up to but excluding the specified migration:\n\n```bash\nsqlite-utils migrate creatures.db path/to/migrations.py --stop-before m002_add_weight\n```\n\n## Listing migrations\n\nAdd `--list` to list migrations without running them, for example:\n\n```bash\nsqlite-utils migrate creatures.db --list\n```\nThe output will look something like this:\n```\nMigrations for: creatures\n\n  Applied:\n    m001_create_table - 2023-07-23 04:09:40.324002\n    m002_add_weight - 2023-07-23 04:09:40.324649\n    m003_add_age - 2023-07-23 04:09:44.441616\n    m003_cleanup - 2023-07-23 04:09:44.443394\n    m004_cleanup - 2023-07-23 04:09:44.444184\n    m005_cleanup - 2023-07-23 04:09:44.445389\n    m006_cleanup - 2023-07-23 04:09:44.446742\n    m007_cleanup - 2023-07-23 04:16:02.529983\n\n  Pending:\n    m008_cleanup\n```\n\n## Verbose mode\n\nAdd `-v` or `--verbose` for verbose output, which will show the schema before and after the migrations were applied along with a diff:\n\n```bash\nsqlite-utils migrate creatures.db --verbose\n```\nExample output:\n```\nMigrating creatures.db\n\nSchema before:\n\n  CREATE TABLE \"_sqlite_migrations\" (\n     [migration_set] TEXT,\n     [name] TEXT,\n     [applied_at] TEXT,\n     PRIMARY KEY ([migration_set], [name])\n  );\n  CREATE TABLE [creatures] (\n     [id] INTEGER PRIMARY KEY,\n     [name] TEXT,\n     [species] TEXT\n  , [weight] FLOAT);\n\nSchema after:\n\n  CREATE TABLE \"_sqlite_migrations\" (\n     [migration_set] TEXT,\n     [name] TEXT,\n     [applied_at] TEXT,\n     PRIMARY KEY ([migration_set], [name])\n  );\n  CREATE TABLE \"creatures\" (\n     [id] INTEGER PRIMARY KEY,\n     [name] TEXT,\n     [species] TEXT,\n     [weight] FLOAT,\n     [age] INTEGER,\n     [shoe_size] INTEGER\n  );\n\nSchema diff:\n\n    [applied_at] TEXT,\n    PRIMARY KEY ([migration_set], [name])\n  );\n-CREATE TABLE [creatures] (\n+CREATE TABLE \"creatures\" (\n    [id] INTEGER PRIMARY KEY,\n    [name] TEXT,\n-   [species] TEXT\n-, [weight] FLOAT);\n+   [species] TEXT,\n+   [weight] FLOAT,\n+   [age] INTEGER,\n+   [shoe_size] INTEGER\n+);\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "A simple database migration system for SQLite, based on sqlite-utils",
    "version": "0.1b0",
    "project_urls": {
        "Changelog": "https://github.com/simonw/sqlite-migrate/releases",
        "Homepage": "https://github.com/simonw/sqlite-migrate",
        "Issues": "https://github.com/simonw/sqlite-migrate/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "df92994545b912e6d6feb40323047f02ca039321e690aa2c27afcd5c4105e37b",
                "md5": "6c96c7bacf8b84c9b241432ce60f978d",
                "sha256": "a4125e35e1de3dc56b6b6ec60e9833ce0ce20192b929ddcb2d4246c5098859c6"
            },
            "downloads": -1,
            "filename": "sqlite_migrate-0.1b0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6c96c7bacf8b84c9b241432ce60f978d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 9986,
            "upload_time": "2023-10-27T23:47:42",
            "upload_time_iso_8601": "2023-10-27T23:47:42.476768Z",
            "url": "https://files.pythonhosted.org/packages/df/92/994545b912e6d6feb40323047f02ca039321e690aa2c27afcd5c4105e37b/sqlite_migrate-0.1b0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "13861463a00d3c4bdb707c0ed4077d17687465a0aa9444593f66f6c4b49e39b5",
                "md5": "d904e85ee8a035b4d0848ad38115b3a6",
                "sha256": "8d502b3ca4b9c45e56012bd35c03d23235f0823c976d4ce940cbb40e33087ded"
            },
            "downloads": -1,
            "filename": "sqlite-migrate-0.1b0.tar.gz",
            "has_sig": false,
            "md5_digest": "d904e85ee8a035b4d0848ad38115b3a6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10736,
            "upload_time": "2023-10-27T23:47:43",
            "upload_time_iso_8601": "2023-10-27T23:47:43.855353Z",
            "url": "https://files.pythonhosted.org/packages/13/86/1463a00d3c4bdb707c0ed4077d17687465a0aa9444593f66f6c4b49e39b5/sqlite-migrate-0.1b0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-27 23:47:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "simonw",
    "github_project": "sqlite-migrate",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqlite-migrate"
}
        
Elapsed time: 0.12411s