Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support php class based seeding/chopping #7

Open
h-bragg opened this issue Aug 15, 2018 · 0 comments
Open

Support php class based seeding/chopping #7

h-bragg opened this issue Aug 15, 2018 · 0 comments

Comments

@h-bragg
Copy link

h-bragg commented Aug 15, 2018

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'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants