Skip to content

Support php class based seeding/chopping #7

@h-bragg

Description

@h-bragg

Create some sort of interface SeederInterface, ChopperInterface.

interface SeederInterface {
    public function seed(<db abstraction layer> $db, string $schema);
}

interface ChopperInterface {
    public function chop(<db abstraction layer> $db, string $schema);
}

Example

class CountrySeeder implements SeederInterface, ChopperInterface {
    public function seed($db, string $schema) {
        $db->insert('table', [
            ['name' => 'Algeria', 'country_code' => 'AL'],
            ['name' => 'Afghanistan', 'country_code' => 'AF'],
        ]);
    }

    public function chop($db, string $schema) {
        $db->delete('table', ['country_code' => ['AL', 'AF']]);
    }
}

Be able to place these files with the .sql files. They can be called anything.

This will allow you to seed dynamic data, or data without requiring the id's.

Then we can extended it to deal with just data, and ignore the method of seeding

interface SeedDataInterface {
    const SEED_TYPE_TRUNCATE = 'truncate'; // truncate the whole table first
    const SEED_TYPE_UPDATE = 'update'; // seed that data, and update any existing entries
    const SEED_TYPE_IGNORE = 'ignore'; // seed the data, but ignore any existing entries

    public function getData(): array; // [[key => value, ...], ...]
    public function getTableName(): string;
    public function getSeedType(): string; // SEED_TYPE_*
}

Example:

class CountrySeedData implements SeedDataInterface
{
    public function getTableName(): string {
        return 'country';
    }

    public function getSeedType(): string {
        return static::SEED_TYPE_UPDATE;
    }

    public function getData(): array {
        return [
            'AF' => [
                'country_code' => 'AF',
                'country_code_iso2' => 'AF',
                'country_code_iso3' => 'AFG',
                'name' => 'Afghanistan',
            ],
            'AX' => [
                'country_code' => 'AX',
                'country_code_iso2' => 'AX',
                'country_code_iso3' => 'ALA',
                'name' => 'Aland Islands',
            ],
        ];
    }
}

We could use this as the bases for file format (Yaml/Json/CSV) importing; something like:

class FileParser implements FileParserInterface {
    public function __construct(FormatParserInterface $parser) {}
    public function parse(string $filePath): SeedDataInterface {
        $data = $this->parser->parse($filePath);
        return new SeedData($data['table'], $data['data'], $data['type'] ?? static::SEED_TYPE_TRUNCATE);
    }
}

class SeedData implements SeedDataInterface {
    public function __construct(string $table, array $data, string $type = static::SEED_TYPE_TRUNCATE) {}
}

$yamlFileParser = new FileParser(new YamlFormatParser());
$seeder->seed($yamlFileParser->parse('/a/path/to/some/file'));

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions