Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/ackara/daterpillar

Daterpillar is a micro-orm and DevOps toolset that helps you manage databases and generate scripts.
https://github.com/ackara/daterpillar

database devops migration mssql mysql nuget schema sqlite xml

Last synced: 2 months ago
JSON representation

Daterpillar is a micro-orm and DevOps toolset that helps you manage databases and generate scripts.

Awesome Lists containing this project

README

        

# Daterpillar

[![nuget](https://img.shields.io/nuget/v/Daterpillar.svg?maxAge=2592000?style=flat-square)](https://www.nuget.org/packages/Daterpillar)
[![powershell](https://img.shields.io/powershellgallery/v/daterpillar.svg?style=flat)](https://www.powershellgallery.com/packages/Daterpillar)

## The Problem
Your *.NET* project depends on a SQL database to store it's data, therefore you need a way to keep your SQL tables in-sync with your entities/classes.

## The Solution
**Daterpillar** is a *netstandard* library keep your classes and tables in-sync by generating **sql-migration-scripts** from your project's `.dll` file.

### Usage

Lets say you have the following class in your project.

```csharp
// Notice the `User` class is decorated with the `Table` and `Column` attributes.

[Table()]
public class User
{
[Key(), Column(AutoIncrement = true)]
public string Id { get; set; }

[Column("full_name", scale: 32)]
[StaticId("ec35bf5d-9a40-4e6f-8412-852b81807a09")]
public string Name { get; set; }

public DateTime DOB { get; set; }
}
```

The *NUGET* package ships with a *MSBuild* target called `GenerateDaterpillarMigrationScript`. It will run after the project has been built, and produce a `.sql` script that should update your database schema, but first you will need to configure it. Open your `.*proj` project-file and add the following elements inside a `` element.

```xml

true

migrations

MySQL

snapshot.schema.xml
```

Once configured, whenever the project is built, a `.sql` script will be created.

```sql
# EXAMPLE: `V1.0.0__create_schema.mysql.sql`
CREATE TABLE `user`(
`Id` VARCHAR(64) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`full_name` VARCHAR(32) NOT NULL,
`DOB` DATETIME NOT NULL
);
```

It is also possible to generate a new script at runtime.

```csharp
var migrator = new Migrator();
migrator.GenerateMigrationScript(Language.MySQL, typeof(User).Assembly, snapshotFilePath, migrationDirectory, fileName);
```

Now that you have a migration-script you will probably want to run it. You can run the script manually or use other tools like [Evolve](https://www.nuget.org/packages/Evolve/), [FlywayDB](https://flywaydb.org/) or [Powershell](https://www.powershellgallery.com/packages/Daterpillar).

#### More about .schema.xml

To generated the migration-script mentioned earlier, the difference between `snapshot.schema.xml` and `[your-assembly].schema.xml` is used to generate the script. So where did this `[your-assembly].schema.xml` file came from? The *MSBuild* target `ExportDaterpillarSchema`, and it is executed after each successful build. You can extend a schema by merging multiple schemas together. Lets say you want to add seed-data to a table, first you will have to add a `[assembly: Import("seed.schema.xml")]` attribute to your project.

```csharp
// example: `./Properties/AssemblyInfo.cs`
...
[assembly: Acklann.Daterpillar.Import("seed.schema.xml")]
// or
// [assembly: Acklann.Daterpillar.Import("*.schema.xml")]
...
```

Next add the `seed.schema.xml` to your project, and make sure it is being copied next to the `.dll` (**Copy To Output Directory: Copy if newer**).

```xml



INSERT INTO User (full_name, DOB) VALUES ('Petra Ral', '2000-11-15');

```

You can also override or add columns by redefining a table.

```xml




varchar



varchar

```