Duck is a database migration framework. Manage your database by creating incremental SQL changes or Delphi functions.
Installation is done using the boss install
command:
boss install github.com/CarlosHe/duck
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.
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.
begin
DuckManager.AddMigration(TCreateExampleTableMigration.New);
end.
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;
uses
Duck.Contract.ParseMigration,
Duck.ParseMigration;
var
LDuckParseMigration: IDuckParseMigration;
begin
LDuckParseMigration := TDuckParseMigration.New(DuckManager);
LDuckParseMigration.LoadFromFolder('.\migrations');
end.
Apply all available migrations.
begin
DuckManager.Up;
end.
Migrate up to a specific version.
begin
DuckManager.UpToVersion(20230315000000);
end.
Migrate up a single migration from the current version
begin
DuckManager.UpByOne;
end.
Roll back a single migration from the current version.
begin
DuckManager.Down;
end.
Roll back migrations to a specific version.
begin
DuckManager.DownToVersion(20230315000000);
end.
Roll back the most recently applied migration, then run it again.
begin
DuckManager.Redo;
end.
Roll back all migrations.
begin
DuckManager.Reset;
end.
Duck
is free and open-source software licensed under the MIT License.