Structured migration of data in SQLite databases
Project description
fastmigrate
The fastmigrate library helps you with structured migration of data in SQLite. That is, it gives you a way to change the schema of your database, while preserving user data.
Here's how it works for a user.
In a directory containing a .fastmigrate file (which will generally be the project root), the user calls fastmigrate. By default, this will look for migration scripts in ./migrations/ and a database at ./data/database.db. These values can also be overridden by CLI arguments or by values set in the .fastmigrate configuration file, which is in ini format.
It will then detect every validly-named file in the migrations directory, select the ones with version numbers greater than the current db version number, and apply the files in alphabetical order, updating the db's version number as it proceeds, stopping if any migration fails.
Key concepts:
-
the version number of a database:
- this is an
intvalue stored in a table_metain a field calledversion. This table will be enforced to have exactly one row. This value will be the "db version value" of the last migration script which was run on that database.
- this is an
-
the migrations directory is a directory which contains only migration scripts.
-
a migration script must be:
- a file which conforms to the "FastMigrate naming rule"
- one of the following:
- a .py or .sh file. In this case, fastmigrate will execute the file, pass the path to the db as the first positional argument. Fastmigrate will interpret a non-zero exit code as failure.
- a .sql file. In this case, fastmigrate will execute the SQL script against the database.
-
the FM naming rule is that every migration script must have a name matching this pattern:
[index]-[description].[fileExtension], where[index]must be a string representing 4-digit integer. This naming convention defines the order in which scripts should be run. -
attempting a migration is:
- determining the current version of a database
- determining if there are any migration scripts with versions higher than the db version
- trying to run those scripts
Command Line Options
-
Basic Usage:
fastmigrateThis will use the defaults, looking for migrations in
./migrations/and the database in./data/database.db. -
Specify Paths:
fastmigrate --db path/to/database.db --migrations path/to/migrations -
Create Database:
fastmigrate --createdbCreates an empty database with the _meta table if it doesn't exist.
-
Database Backup:
fastmigrate --backupCreates a timestamped backup of the database before running any migrations. The backup file will be named
database.db.YYYYMMDD_HHMMSS.backup.
Important Considerations
-
Sequential Execution: Migrations are executed in order based on their index numbers. If migration #3 fails, migrations #1-2 remain applied and the process stops.
-
Version Integrity: The database version is only updated after a migration is successfully completed.
-
External Side Effects: Python and Shell scripts may have side effects outside the database (file operations, network calls) that are not managed by fastmigrate.
-
Database Locking: During migration, the database may be locked. Applications should not attempt to access it while migrations are running.
-
Backups: For safety, you can use the
--backupoption to create a backup before running migrations.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastmigrate-0.1.0.tar.gz.
File metadata
- Download URL: fastmigrate-0.1.0.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
870111ba7a92672fcb378eeeccb97127021dee8da1ae29e6aec1dbc1339cb25a
|
|
| MD5 |
2c16d224df2897a1de4beebff8d7605c
|
|
| BLAKE2b-256 |
4802ec597a3a8daaa81ab37faf302d32859f3345ccaf63b9ab23aa3e9e0266a8
|
File details
Details for the file fastmigrate-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastmigrate-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4ad04b827160d471d7b7f9d5550d45b9b965fcdf9f0f37322f862ea008ff4f2
|
|
| MD5 |
f70fb0b250249ae380549a7e31dd83d5
|
|
| BLAKE2b-256 |
21f3df6ea170983c76a7bb93d745186769874fed8a78a480c7e7609e22720931
|