Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
M
migrate_plus
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
1
Merge Requests
1
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
migrate_plus
Commits
cd369d53
Commit
cd369d53
authored
May 31, 2019
by
heddn
Committed by
heddn
May 31, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#3006094
by heddn, jcnventura, DanChadwick: Add migrate source for sql table
parent
c9cb639c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
148 additions
and
83 deletions
+148
-83
src/Plugin/migrate/source/Table.php
src/Plugin/migrate/source/Table.php
+80
-0
src/Plugin/migrate_plus/data_parser/Xml.php
src/Plugin/migrate_plus/data_parser/Xml.php
+1
-1
tests/src/Kernel/MigrateTableTest.php
tests/src/Kernel/MigrateTableTest.php
+67
-82
No files found.
src/Plugin/migrate/source/Table.php
0 → 100644
View file @
cd369d53
<?php
namespace
Drupal\migrate_plus\Plugin\migrate\source
;
use
Drupal\Core\State\StateInterface
;
use
Drupal\migrate\MigrateException
;
use
Drupal\migrate\Plugin\migrate\source\SqlBase
;
use
Drupal\migrate\Plugin\MigrationInterface
;
/**
* Source plugin for retrieving data via URLs.
*
* @MigrateSource(
* id = "table"
* )
*/
class
Table
extends
SqlBase
{
const
TABLE_ALIAS
=
't'
;
/**
* The name of the destination table.
*
* @var string
*/
protected
$tableName
;
/**
* IDMap compatible array of id fields.
*
* @var array
*/
protected
$idFields
;
/**
* Array of fields present on the destination table.
*
* @var array
*/
protected
$fields
;
/**
* {@inheritdoc}
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
MigrationInterface
$migration
,
StateInterface
$state
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$migration
,
$state
);
$this
->
tableName
=
$configuration
[
'table_name'
];
// Insert alias in id_fields.
foreach
(
$configuration
[
'id_fields'
]
as
&
$field
)
{
$field
[
'alias'
]
=
static
::
TABLE_ALIAS
;
}
$this
->
idFields
=
$configuration
[
'id_fields'
];
$this
->
fields
=
isset
(
$configuration
[
'fields'
])
?
$configuration
[
'fields'
]
:
[];
}
/**
* {@inheritdoc}
*/
public
function
query
()
{
return
$this
->
select
(
$this
->
tableName
,
static
::
TABLE_ALIAS
)
->
fields
(
static
::
TABLE_ALIAS
,
$this
->
fields
);
}
/**
* {@inheritdoc}
*/
public
function
fields
()
{
return
$this
->
fields
;
}
/**
* {@inheritdoc}
*/
public
function
getIds
()
{
if
(
empty
(
$this
->
idFields
))
{
throw
new
MigrateException
(
'Id fields are required for a table source'
);
}
return
$this
->
idFields
;
}
}
src/Plugin/migrate_plus/data_parser/Xml.php
View file @
cd369d53
...
...
@@ -135,7 +135,7 @@ class Xml extends DataParserPluginBase {
* A \SimpleXmlElement when the document is parseable, or false if a
* parsing error occurred.
*
* @throws MigrateException
* @throws
\Drupal\migrate\
MigrateException
*/
protected
function
getSimpleXml
()
{
$node
=
$this
->
reader
->
expand
();
...
...
tests/src/Kernel/MigrateTableTest.php
View file @
cd369d53
...
...
@@ -2,7 +2,6 @@
namespace
Drupal\Tests\migrate_plus\Kernel
;
use
Drupal\Core\Database\Database
;
use
Drupal\migrate\MigrateExecutable
;
use
Drupal\Tests\migrate\Kernel\MigrateTestBase
;
...
...
@@ -13,7 +12,8 @@ use Drupal\Tests\migrate\Kernel\MigrateTestBase;
*/
class
MigrateTableTest
extends
MigrateTestBase
{
const
TABLE_NAME
=
'migrate_test_destination_table'
;
const
SOURCE_TABLE_NAME
=
'migrate_test_source_table'
;
const
DEST_TABLE_NAME
=
'migrate_test_destination_table'
;
/**
* The database connection.
...
...
@@ -30,76 +30,88 @@ class MigrateTableTest extends MigrateTestBase {
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
connection
=
Database
::
getConnection
();
$this
->
connection
->
schema
()
->
createTable
(
static
::
TABLE_NAME
,
[
'description'
=>
'Test table'
,
'fields'
=>
[
'data'
=>
[
'type'
=>
'varchar'
,
'length'
=>
'32'
,
'not null'
=>
TRUE
,
],
'data2'
=>
[
'type'
=>
'varchar'
,
'length'
=>
'32'
,
'not null'
=>
TRUE
,
],
'data3'
=>
[
'type'
=>
'varchar'
,
'length'
=>
'32'
,
'not null'
=>
TRUE
,
$this
->
connection
=
$this
->
container
->
get
(
'database'
);
$connections
=
[
static
::
SOURCE_TABLE_NAME
=>
$this
->
sourceDatabase
,
static
::
DEST_TABLE_NAME
=>
$this
->
connection
,
];
foreach
(
$connections
as
$table
=>
$connection
)
{
$connection
->
schema
()
->
createTable
(
$table
,
[
'description'
=>
'Test table'
,
'fields'
=>
[
'data'
=>
[
'type'
=>
'varchar'
,
'length'
=>
'32'
,
'not null'
=>
TRUE
,
],
'data2'
=>
[
'type'
=>
'varchar'
,
'length'
=>
'32'
,
'not null'
=>
TRUE
,
],
'data3'
=>
[
'type'
=>
'varchar'
,
'length'
=>
'32'
,
'not null'
=>
TRUE
,
],
],
'primary key'
=>
[
'data'
],
]);
}
$query
=
$this
->
sourceDatabase
->
insert
(
static
::
SOURCE_TABLE_NAME
)
->
fields
([
'data'
,
'data2'
,
'data3'
]);
$values
=
[
[
'data'
=>
'dummy value'
,
'data2'
=>
'dummy2 value'
,
'data3'
=>
'dummy3 value'
,
],
[
'data'
=>
'dummy value2'
,
'data2'
=>
'dummy2 value2'
,
'data3'
=>
'dummy3 value2'
,
],
[
'data'
=>
'dummy value3'
,
'data2'
=>
'dummy2 value3'
,
'data3'
=>
'dummy3 value3'
,
],
'primary key'
=>
[
'data'
],
]);
];
foreach
(
$values
as
$record
)
{
$query
->
values
(
$record
);
}
$query
->
execute
();
}
/**
* {@inheritdoc}
*/
protected
function
tearDown
()
{
$this
->
connection
->
schema
()
->
dropTable
(
static
::
TABLE_NAME
);
$this
->
sourceDatabase
->
schema
()
->
dropTable
(
static
::
SOURCE_TABLE_NAME
);
$this
->
connection
->
schema
()
->
dropTable
(
static
::
DEST_TABLE_NAME
);
parent
::
tearDown
();
}
/**
* Create a minimally valid migration with some source data.
*
* @return array
* The migration definition.
* Tests table migration.
*/
p
rotected
function
getTableDestination
Migration
()
{
p
ublic
function
testTable
Migration
()
{
$definition
=
[
'id'
=>
'migration_table_test'
,
'migration_tags'
=>
[
'Testing'
],
'source'
=>
[
'plugin'
=>
'embedded_data'
,
'data_rows'
=>
[
[
'data'
=>
'dummy value'
,
'data2'
=>
'dummy2 value'
,
'data3'
=>
'dummy3 value'
,
],
[
'data'
=>
'dummy value2'
,
'data2'
=>
'dummy2 value2'
,
'data3'
=>
'dummy3 value2'
,
],
[
'data'
=>
'dummy value3'
,
'data2'
=>
'dummy2 value3'
,
'data3'
=>
'dummy3 value3'
,
],
],
'ids'
=>
[
'plugin'
=>
'table'
,
'table_name'
=>
static
::
SOURCE_TABLE_NAME
,
'id_fields'
=>
[
'data'
=>
[
'type'
=>
'string'
],
],
],
'destination'
=>
[
'plugin'
=>
'table'
,
'table_name'
=>
static
::
TABLE_NAME
,
'id_fields'
=>
[
'data'
=>
[
'type'
=>
'string'
]],
'table_name'
=>
static
::
DEST_TABLE_NAME
,
'id_fields'
=>
[
'data'
=>
[
'type'
=>
'string'
],
],
],
'process'
=>
[
'data'
=>
'data'
,
...
...
@@ -107,20 +119,13 @@ class MigrateTableTest extends MigrateTestBase {
'data3'
=>
'data3'
,
],
];
return
$definition
;
}
/**
* Tests table destination.
*/
public
function
testTableDestination
()
{
$migration
=
\
Drupal
::
service
(
'plugin.manager.migration'
)
->
createStubMigration
(
$this
->
getTableDestinationMigration
());
$migration
=
\
Drupal
::
service
(
'plugin.manager.migration'
)
->
createStubMigration
(
$definition
);
$executable
=
new
MigrateExecutable
(
$migration
,
$this
);
$executable
->
import
();
$values
=
$this
->
connection
->
select
(
static
::
TABLE_NAME
)
->
fields
(
static
::
TABLE_NAME
)
$values
=
$this
->
connection
->
select
(
static
::
DEST_
TABLE_NAME
)
->
fields
(
static
::
DEST_
TABLE_NAME
)
->
execute
()
->
fetchAllAssoc
(
'data'
);
...
...
@@ -129,31 +134,11 @@ class MigrateTableTest extends MigrateTestBase {
$this
->
assertEquals
(
'dummy2 value2'
,
$values
[
'dummy value2'
]
->
data2
);
$this
->
assertEquals
(
'dummy3 value3'
,
$values
[
'dummy value3'
]
->
data3
);
$this
->
assertEquals
(
3
,
count
(
$values
));
}
/**
* Tests table rollback.
*/
public
function
testTableRollback
()
{
$this
->
testTableDestination
();
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration
=
\
Drupal
::
service
(
'plugin.manager.migration'
)
->
createStubMigration
(
$this
->
getTableDestinationMigration
());
$executable
=
new
MigrateExecutable
(
$migration
,
$this
);
$executable
->
import
();
$values
=
$this
->
connection
->
select
(
static
::
TABLE_NAME
)
->
fields
(
static
::
TABLE_NAME
)
->
execute
()
->
fetchAllAssoc
(
'data'
);
$this
->
assertEquals
(
'dummy value'
,
$values
[
'dummy value'
]
->
data
);
$this
->
assertEquals
(
3
,
count
(
$values
));
// Now rollback.
$executable
->
rollback
();
$values
=
$this
->
connection
->
select
(
static
::
TABLE_NAME
)
->
fields
(
static
::
TABLE_NAME
)
$values
=
$this
->
connection
->
select
(
static
::
DEST_
TABLE_NAME
)
->
fields
(
static
::
DEST_
TABLE_NAME
)
->
execute
()
->
fetchAllAssoc
(
'data'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment