diff --git a/src/Command/MakeMigration.php b/src/Command/MakeMigration.php deleted file mode 100644 index b733baf..0000000 --- a/src/Command/MakeMigration.php +++ /dev/null @@ -1,152 +0,0 @@ -definitionInstanceRegistry = $definitionInstanceRegistry; - $this->connection = $connection; - } - - protected function configure(): void - { - $this - ->addArgument('plugin', InputArgument::REQUIRED, 'Plugin Name') - ->addArgument('definition', InputArgument::IS_ARRAY, 'Definition name'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $io = new SymfonyStyle($input, $output); - - $toSchema = new Schema(); - foreach ($input->getArgument('definition') as $name) { - $definition = $this->definitionInstanceRegistry->getByEntityName($name); - $this->migrationSchemaBuilder->buildSchemaOfDefinition($toSchema, $definition); - } - - $fromSchema = $this->connection->createSchemaManager()->introspectSchema(); - - $comparator = new Comparator(); - $updateQueries = $comparator->compareSchemas($fromSchema, $toSchema)->toSaveSql($this->connection->getDatabasePlatform()); - - if (\count($updateQueries) === 0) { - $io->success('Schema is already up to date'); - - return 0; - } - - [$namespace, $migrationPath, $className, $timestamp] = $this->determineMigrationPath($input->getArgument('plugin')); - - $migration = <<executeUpdate(\'%s\');' . PHP_EOL, addslashes((string) $sql)); - } - - $content = str_replace( - [ - '#NAMESPACE#', - '#CLASSNAME#', - '#TIMESTAMP#', - '#STMS#' - ], - [ - $namespace, - $className, - $timestamp, - $dbalExec - ], - $migration - ); - - \file_put_contents($migrationPath, $content); - - $io->success(\sprintf('Generated migration file at "%s"', $migrationPath)); - - return 0; - } - - private function determineMigrationPath(string $plugin): array - { - foreach ($this->pluginInfos as $pluginInfo) { - if ($pluginInfo['name'] !== $plugin) { - continue; - } - - $reflectionClass = new \ReflectionClass($pluginInfo['baseClass']); - - $timeStamp = time(); - $path = dirname($reflectionClass->getFileName()); - $migrationPath = $path . '/Migration'; - - $fs = new Filesystem(); - - if (!$fs->exists($migrationPath)) { - $fs->mkdir($migrationPath); - } - - return [ - $reflectionClass->getNamespaceName() . '\\Migration', // namespace - $path . '/Migration/Migration' . $timeStamp . '.php', // file name - 'Migration' . $timeStamp, // class name - $timeStamp - ]; - } - - throw new \RuntimeException(sprintf('Cannot find plugin by name "%s"', $plugin)); - - } -} diff --git a/src/Command/SchemaDiff.php b/src/Command/SchemaDiff.php deleted file mode 100644 index f343e71..0000000 --- a/src/Command/SchemaDiff.php +++ /dev/null @@ -1,36 +0,0 @@ -connection = $connection; - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $localSchema = $this->builder->build(); - $remoteSchema = $this->connection->createSchemaManager()->introspectSchema(); - - $comparator = new Comparator(); - $sqls = $comparator->compareSchemas($remoteSchema, $localSchema)->toSaveSql($this->connection->getDatabasePlatform()); - - $output->writeln($sqls); - - return 0; - } -} diff --git a/src/Component/Generator/Migration/MigrationSchemaBuilder.php b/src/Component/Generator/Migration/MigrationSchemaBuilder.php deleted file mode 100644 index 89d8449..0000000 --- a/src/Component/Generator/Migration/MigrationSchemaBuilder.php +++ /dev/null @@ -1,288 +0,0 @@ -instanceRegistry = $instanceRegistry; - } - - public function build(): Schema - { - $schema = new Schema(); - - foreach ($this->instanceRegistry->getDefinitions() as $definition) { - $this->buildSchemaOfDefinition($schema, $definition); - } - - return $schema; - } - - public function buildSchemaOfDefinition(Schema $schema, EntityDefinition $definition): void - { - $table = $schema->createTable($definition->getEntityName()); - - foreach ($definition->getFields() as $field) { - if ($field->is(Runtime::class)) { - continue; - } - - if ($field instanceof AssociationField) { - continue; - } - - if (!$field instanceof StorageAware) { - continue; - } - - if ($field instanceof TranslatedField) { - continue; - } - - [$type, $options] = $this->getFieldDefinition($field); - - $table->addColumn($field->getStorageName(), $type, $options); - } - - $table->setPrimaryKey(array_map(fn(StorageAware $field) => $field->getStorageName(), $definition->getPrimaryKeys()->getElements())); - $this->addForeignKeys($table, $definition); - } - - private function getFieldDefinition(Field $field): array - { - $options = [ - 'notnull' => false, - ]; - - if ($field->is(Required::class) && !$field instanceof UpdatedAtField) { - $options['notnull'] = true; - } - - switch (true) { - case $field instanceof VersionField: - case $field instanceof ReferenceVersionField: - case $field instanceof ParentFkField: - case $field instanceof IdField: - case $field instanceof FkField: - $type = Types::BINARY; - $options['length'] = 16; - $options['fixed'] = true; - - break; - - case $field instanceof UpdatedAtField: - case $field instanceof CreatedAtField: - case $field instanceof DateTimeField: - $type = Types::DATETIME_MUTABLE; - - break; - - case $field instanceof DateField: - $type = Types::DATE_MUTABLE; - - break; - - case $field instanceof CartPriceField: - case $field instanceof CalculatedPriceField: - case $field instanceof PriceDefinitionField: - case $field instanceof PriceField: - case $field instanceof ListField: - case $field instanceof JsonField: - $type = Types::JSON; - - break; - - case $field instanceof ChildCountField: - case $field instanceof IntField: - $type = Types::INTEGER; - - break; - - case $field instanceof TreePathField: - case $field instanceof LongTextField: - $type = Types::TEXT; - - break; - - case $field instanceof TreeLevelField: - $type = Types::INTEGER; - - break; - - case $field instanceof RemoteAddressField: - $type = Types::STRING; - - break; - - case $field instanceof PasswordField: - $type = Types::STRING; - - break; - - case $field instanceof FloatField: - $type = Types::FLOAT; - - break; - - case $field instanceof StringField: - $type = Types::STRING; - $options['length'] = $field->getMaxLength(); - - break; - - case $field instanceof BoolField: - $type = Types::BOOLEAN; - $options['default'] = 0; - - break; - - case $field instanceof BlobField: - $type = Types::BLOB; - - break; - - default: - throw new \RuntimeException(sprintf('Unknown field %s', $field::class)); - } - - return [ - $type, - $options - ]; - } - - private function addForeignKeys(Table $table, EntityDefinition $definition): void - { - $fields = $definition->getFields()->filter( - function (Field $field) { - if ($field instanceof ManyToOneAssociationField || ($field instanceof OneToOneAssociationField && $field->getStorageName() !== 'id')) { - return true; - } - - return false; - } - ); - - $referenceVersionFields = $definition->getFields()->filterInstance(ReferenceVersionField::class); - - /** @var ManyToOneAssociationField $field */ - foreach ($fields as $field) { - $reference = $field->getReferenceDefinition(); - - $hasOneToMany = $definition->getFields()->filter(function (Field $field) use ($reference) { - if (!$field instanceof OneToManyAssociationField) { - return false; - } - if ($field instanceof ChildrenAssociationField) { - return false; - } - - return $field->getReferenceDefinition() === $reference; - })->count() > 0; - - $columns = [ - $field->getStorageName(), - ]; - - $referenceColumns = [ - $field->getReferenceField(), - ]; - - if ($reference->isVersionAware()) { - $versionField = null; - - /** @var ReferenceVersionField $referenceVersionField */ - foreach ($referenceVersionFields as $referenceVersionField) { - if ($referenceVersionField->getVersionReferenceDefinition() === $reference) { - $versionField = $referenceVersionField; - - break; - } - } - - if ($field instanceof ParentAssociationField) { - $columns[] = 'version_id'; - } else { - $columns[] = $versionField->getStorageName(); - } - - $referenceColumns[] = 'version_id'; - } - - $update = 'CASCADE'; - - if ($field->is(CascadeDelete::class)) { - $delete = 'CASCADE'; - } elseif ($field->is(RestrictDelete::class)) { - $delete = 'RESTRICT'; - } else { - $delete = 'SET NULL'; - } - - // skip foreign key to prevent bi-directional foreign key - if ($hasOneToMany) { - continue; - } - - $table->addForeignKeyConstraint( - $reference->getEntityName(), - $columns, - $referenceColumns, - [ - 'onUpdate' => $update, - 'onDelete' => $delete - ], - sprintf('fk.%s.%s', $definition->getEntityName(), $field->getStorageName()) - ); - } - } -}