/duck

The Duck is a migration framework for database versioning

Primary LanguagePascalMIT LicenseMIT

Duck


Duck is a database migration framework. Manage your database by creating incremental SQL changes or Delphi functions.


⚙️ Installation

Installation is done using the boss install command:

boss install github.com/CarlosHe/duck

⚡️ Quickstart

ex: FireDAC connector and PostgreSQL repository.

uses
  Duck.Contract.Manager,
  Duck.Manager,
  Duck.Connector.FireDAC,
  Duck.Repository.PostgreSQL;

var DuckManager: IDuckManager;

begin
  DuckManager := TDuckManager.New(
    TDuckPostgreSQLRepository.New(
      TDuckFireDACConnector.New(FDConnection)
    )
  );
end.

Create class migration

unit Migration.V20230315000000_CreateTableExample;

interface

uses
  Duck.Contract.Migration;

type

  TCreateExampleTableMigration = class(TInterfacedObject, IDuckMigration)
  strict private
    { strict private declarations }
    constructor Create;
  private
    { private declarations }
  protected
    { protected declarations }
  public
    { public declarations }
    function GetVersion: Int64;
    function GetName: string;
    function GetUp: string;
    function GetDown: string;
    class function New: IDuckMigration;
  end;

implementation

{ TCreateExampleTableMigration }

constructor TCreateExampleTableMigration.Create;
begin

end;

function TCreateExampleTableMigration.GetDown: string;
begin
  Result := 'DROP TABLE examples;';
end;

function TCreateExampleTableMigration.GetName: string;
begin
  Result := 'create_table_examples';
end;

function TCreateExampleTableMigration.GetVersion: Int64;
begin
  Result := 20230315000000;
end;

class function TCreateExampleTableMigration.New: IDuckMigration;
begin
  Result := Self.Create;
end;

function TCreateExampleTableMigration.GetUp: string;
begin
  Result := 'CREATE TABLE examples (name varchar NOT NULL);';
end;

end.

Add class migration

begin
  DuckManager.AddMigration(TCreateExampleTableMigration.New);
end.

Create file migration

Create migration file (version_name.sql). ex: 20230315000000_create_table_examples.sql

-- +duck Up
CREATE TABLE examples (name varchar NOT NULL);
-- +duck Down
DROP TABLE examples;

Add folder migration files

uses
  Duck.Contract.ParseMigration,
  Duck.ParseMigration;

var
  LDuckParseMigration: IDuckParseMigration;
begin
  LDuckParseMigration := TDuckParseMigration.New(DuckManager);
  LDuckParseMigration.LoadFromFolder('.\migrations');
end.

Up

Apply all available migrations.

begin
  DuckManager.Up;
end.

UpToVersion

Migrate up to a specific version.

begin
  DuckManager.UpToVersion(20230315000000);
end.

UpByOne

Migrate up a single migration from the current version

begin
  DuckManager.UpByOne;
end.

Down

Roll back a single migration from the current version.

begin
  DuckManager.Down;
end.

DownToVersion

Roll back migrations to a specific version.

begin
  DuckManager.DownToVersion(20230315000000);
end.

Redo

Roll back the most recently applied migration, then run it again.

begin
  DuckManager.Redo;
end.

Reset

Roll back all migrations.

begin
  DuckManager.Reset;
end.

⚠️ License

Duck is free and open-source software licensed under the MIT License.