https://github.com/trustbit/plainsql.migrations
Executes migration scripts written in plain old SQL
https://github.com/trustbit/plainsql.migrations
database dotnet-core migrations sql
Last synced: about 1 year ago
JSON representation
Executes migration scripts written in plain old SQL
- Host: GitHub
- URL: https://github.com/trustbit/plainsql.migrations
- Owner: trustbit
- License: mit
- Created: 2016-08-09T13:57:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2025-01-21T09:30:21.000Z (about 1 year ago)
- Last Synced: 2025-03-17T14:08:49.572Z (about 1 year ago)
- Topics: database, dotnet-core, migrations, sql
- Language: C#
- Homepage:
- Size: 82 KB
- Stars: 7
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PlainSql.Migrations

[](https://coveralls.io/github/trustbit/PlainSql.Migrations?branch=master)
Execute migration scripts written in plain old SQL. Only executes each migration once.
## Installation
```bash
dotnet add package PlainSql.Migrations
```
## Usage
`PlainSql.Migrations` provide a script loader and a migrator that interact to load and execute migration scripts.
```csharp
using PlainSql.Migrations;
private void ExecuteMigrations(string connectionString, string environment)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var migrationScripts = MigrationScriptsLoader.FromDirectory("./MigrationScripts");
Migrator.ExecuteMigrations(connection, migrationScripts);
Console.Writeline("Executed database migration scripts");
}
}
```
`MigrationScriptsLoader.FromDirectory` orders the migration scripts alphabetically
(using `System.StringComparer.InvariantCulture`).
## Global Tool
`PlainSql.Migrations` offers a .NET Global tool that can be installed like
so `dotnet tool install --global PlainSql.Migrations.Tool` and then executed from the terminal like
so `migrate -c "Uid=myuser;Pwd=password1;Host=localhost;Database=northwind;" -d postgres`.
## Creating Migration Files
`PlainSql.Migrations` also includes a .NET Global tool to generate timestamped migration files in the format
of `yyyyMMddHHmmss_DescriptionOfTheMigration.sql`.
### Usage
#### Installation
Install with `dotnet tool install --global PlainSql.Migrations.Generator`.
#### Generating Files
Use the `generate-migration` command.
```
$ generate-migration "add phone number to user table"
[10:42:21 INF] Creating migration file 20230302104221_AddPhoneNumberToUserTable.sql in ./MigrationScripts
[10:42:21 INF] Sucessfully created 20230302104221_AddPhoneNumberToUserTable.sql!
```
A different location for the migration scripts folder can be supplied with the `-m` option:
```
$ generate-migration "add phone number to user table" -m "./migrations"
[10:44:54 INF] Creating migration file 20230302104454_AddPhoneNumberToUserTable.sql in ./migrations
[10:44:54 INF] Sucessfully created 20230302104454_AddPhoneNumberToUserTable.sql!
```
## Database Support
* SQLite
* MS SQL
* PostgreSQL
## Concurrency
Migrations are executed in a single transaction with isolation level "Serializable". This usually
means that executing migrations is concurrency-safe. For details on which SQL statements are supported
by this transaction level, please refer to the documentation of your database technology.