• Stars
    star
    166
  • Rank 227,748 (Top 5 %)
  • Language
    PHP
  • License
    GNU Lesser Genera...
  • Created over 6 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”€ UXDM helps developers migrate data from one system or format to another.

πŸ”€ Universal Extensible Data Migrator (UXDM)

UXDM helps developers migrate data from one system or format to another.

Installation

UXDM can be easily installed using Composer. Just run the following command from the root of your project.

composer require divineomega/uxdm

If you have never used the Composer dependency manager before, head to the Composer website for more information on how to get started.

Quick Start

  1. Create a new PHP file to contain your UXDM migration code. In this example, we'll call it user-csv-import.php. Remember to add require 'vendor/autoload.php' and relevant use statements, if necessary.

  2. Create your source and destination objects. This example uses a CSV source and PDO (database) destination.

$csvSource = new CSVSource('users.csv');

$pdoDestination = new PDODestination(new PDO('mysql:dbname=test-database;host=127.0.0.1', 'root', 'password'), 'users');
  1. Create and configure a new UXDM migrator object.
$migrator = new Migrator;
$migrator->setSource($csvSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setKeyFields(['id'])
         ->withProgressBar()
         ->migrate();
  1. Run your newly created migration. In this example, we can just run php user-csv-import.php from the command line and will get a nice progress bar.

See the sections below for more information on the available source and destination objects, and more advanced usage examples.

Migrations

Each UXDM migration requires a source object and at least one destination object. These determine where and how data is read and written. The UXDM package works with a variety of source and destination objects, including the following.

  • PDO (PHP Database Object) Source & Destination
  • Eloquent (as used in Laravel) Source & Destination
  • Doctrine (as used in Symfony) Destination
  • CSV (Comma Separated Values) Source & Destination
  • Excel Source & Destination
  • Associative Array Source & Destination
  • JSON Files Source & Destination
  • XML Source & Destination
  • WordPress Post Source
  • WordPress User Source
  • Debug Output Destination

Some of these are built-in to the core UXDM package, while others are available as separate packages.

Source and destination objects can be used in any combination. Data can be migrated from a CSV and inserted into a database, just as easily as data can be migrated from a database to a CSV.

You can also use similar source and destination objects in the same migration. For example, a common use of UXDM is to use a PDO source and PDO destination to transfer data from one database to another.

Please see the Sources & Destinations page for more sources and destinations, and detailed documentation on their usage.

Examples

Database to database migration

An example of a basic database to database UXDM migration is shown below.

$pdoSource = new PDOSource(new PDO('mysql:dbname=old-test;host=127.0.0.1', 'root', 'password123'), 'users');

$pdoDestination = new PDODestination(new PDO('mysql:dbname=new-test;host=127.0.0.1', 'root', 'password456'), 'new_users');

$migrator = new Migrator;
$migrator->setSource($pdoSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setKeyFields(['id'])
         ->withProgressBar()
         ->migrate();

This migration will move the id, email and name fields from the the users table in the old-test database, to the new_users table in the new-test database, replacing any existing records with the same id (the key field).

Source data validation

You can use UXDM to validate the source data. If validation fails part way through a migration, the migration will halt and a ValidationException will be thrown. However, if ->validateBeforeMigrating() is called, all data rows will be preemptively validated before the migration begins.

The code below shows how to validate various fields.

$pdoSource = new PDOSource(new PDO('mysql:dbname=old-test;host=127.0.0.1', 'root', 'password123'), 'users');

$pdoDestination = new PDODestination(new PDO('mysql:dbname=new-test;host=127.0.0.1', 'root', 'password456'), 'new_users');

$migrator = new Migrator;
$migrator->setSource($pdoSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setValidationRules([
            'id' => [new Required(), new IsNumeric()],
            'email' => [new Required(), new IsString(), new IsEmail()],
            'name' => [new Required(), new IsString()],
         ])
      // ->validateBeforeMigrating()
         ->setKeyFields(['id'])
         ->withProgressBar()
         ->migrate();

This migration will validate the source data matches the defined validation rules.

  • 'id' must be present, and numeric.
  • 'email' must be present, a string, and a correctly formatted email address.
  • 'name' must be present, and a string.

UXDM uses the Omega Validator package. See its documentation for all available validation rules.

Mapping field names from source to destination

This examples shows how UXDM can map field names from source to destination.

$migrator = new Migrator;
$migrator->setSource($pdoSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setKeyFields(['id'])
         ->setFieldMap(['name' => 'full_name'])
         ->withProgressBar()
         ->migrate();

This migration will move data from the source name field into the destination full_name field, while still moving the id and email fields normally.

Transforming data rows during migration

Sometimes the data you want to move from source to destination needs transforming. This can be changing existing items of data, adding new data items, or removing items you do not need.

UXDM allows you to create one or more transformer objects, and add them to the migration. See the following examples of how to use transformers to manipulate your data.

Changing existing data items

This example shows how you can transform existing data items during migration.

class NameCaseTransformer implements TransformerInterface
{
    public function transform(DataRow $dataRow): void
    {
        $nameDataItem = $dataRow->getDataItemByFieldName('name');
        $nameDataItem->value = ucwords(strtolower($nameDataItem->value));
    }
}

$migrator = new Migrator;
$migrator->setSource($pdoSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setKeyFields(['id'])
         ->addTransformer(new NameCaseTransformer())
         ->withProgressBar()
         ->migrate();

This migration will ensure that all names fields have consistent case.

Adding data items

This example shows how you can add new data items while the migration is taking place.

class AddRandomNumberTransformer implements TransformerInterface
{
    public function transform(DataRow &$dataRow): void
    {
        $dataRow->addDataItem(new DataItem('random_number', rand(1,1000)));
    }
}

$migrator = new Migrator;
$migrator->setSource($pdoSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setKeyFields(['id'])
         ->addTransformer(new AddRandomNumberTransformer())
         ->withProgressBar()
         ->migrate();

This migration will add a random number into a field called random_number for each row of data. This will then be migrated to the destination database along with the other fields.

Removing data items

This example demonstrates how data items can be removed from a data row. You may wish to do this if you want to use its value, but not actually migrate it to the destination.

class EmailToHashTransformer implements TransformerInterface
{
    public function transform(DataRow $dataRow): void
    {
        $emailDataItem = $dataRow->getDataItemByFieldName('email');
        $dataRow->addDataItem(new DataItem('email_hash', md5($emailDataItem->value)));
        $dataRow->removeDataItem($emailDataItem);
    }
}

$migrator = new Migrator;
$migrator->setSource($pdoSource)
         ->setDestination($pdoDestination)
         ->setFieldsToMigrate(['id', 'email', 'name'])
         ->setKeyFields(['id'])
         ->addTransformer(new EmailToHashTransformer())
         ->withProgressBar()
         ->migrate();

This migration gets the data from the email field in the source, creates a new email_hash data item which contains an md5 of the email address, and then removes the original email data item. This new email_hash will then be migrated to the destination database along with the other fields, excluding the removed email field.

More Repositories

1

password_exposed

πŸ”’ Password Exposed Helper Function - Check if a password has been exposed in a data breach.
PHP
215
star
2

artisan-menu

πŸ“ Artisan Menu - Use Artisan via an elegant console GUI
PHP
148
star
3

eloquent-attribute-value-prediction

Predict attribute values for your Laravel Eloquent models using machine learning!
PHP
147
star
4

php-ssh-connection

Provides an elegant syntax to connect to SSH servers and execute commands.
PHP
102
star
5

laravel-password-exposed-validation-rule

πŸ”’ Laravel validation rule that checks if a password has been exposed in a data breach.
PHP
87
star
6

cachet.php

πŸ“› PHP client library for the Cachet API
PHP
39
star
7

php-summary

πŸ“ Automatically summarises text, using a naive summarisation algorithm
PHP
27
star
8

dates-timezone-conversion-trait

β³πŸ—Ί Automatically convert an Eloquent model's dates to and from the current user's timezone
PHP
23
star
9

array_undot

array_undot (the opposite of the array_dot helper function) expands a dot notation array into a full multi-dimensional array.
PHP
22
star
10

DO-File-Cache

πŸ—„οΈ PHP File-based Caching Library
PHP
17
star
11

php-reading-time

⏱ Calculates reading time for text
PHP
17
star
12

clippy-for-laravel

πŸ“Ž Clippy for Laravel
PHP
16
star
13

laravel-last-activity

Stores the last activity time of users within your Laravel application
PHP
16
star
14

php-cli-progress-bar

⏱️ Progress bar for command line PHP scripts
PHP
16
star
15

cachet.js

πŸ“› JavaScript client library for the Cachet API
HTML
15
star
16

php-hcl-parser

βš’πŸ”€πŸ˜ Parses HCL configuration files into PHP objects.
PHP
14
star
17

laravel-offensive-validation-rule

🀬🀭 Laravel validation rule that checks if a string is offensive.
PHP
13
star
18

laravel-multiple-choice

Package that provides multiple choice questions and answers
PHP
12
star
19

php-camel-caser

πŸͺπŸ’Ό PHP Camel Caser - Lets you use built-in PHP functions in camel case
PHP
11
star
20

laravel-natural-where

Laravel Natural Where extends the Laravel query builder to allow expressing of where operators in natural language.
PHP
10
star
21

psr-18-guzzle-adapter

PSR-18 adapter for the Guzzle HTTP client
PHP
10
star
22

laravel-omega-search

Easily add an intelligent search engine to your Laravel powered website or web application
PHP
10
star
23

laravel-extendable-basket

πŸ›’ Laravel Extendable Basket provides several abstract classes that implement basic ecommerce basket functionality
PHP
10
star
24

web-article-formatter

πŸŒπŸ”€πŸ“° Converts a webpage article into other formats, like PDF, markdown, JSON, plain text...
PHP
10
star
25

DO-File-Cache-PSR-6

πŸ—„ PSR-6 adapter for DO File Cache
PHP
9
star
26

laravel-addresses

Laravel Addresses
PHP
9
star
27

php-countries

🌍 PHP Countries is a library that provides an elegant syntax to country data.
PHP
8
star
28

exiguous-ecommerce

πŸ›’ A super simple ecommerce library, that uses flat files and takes a very minimalistic approach.
PHP
8
star
29

php-geolocation

PHP library that determines the country of an IP address
PHP
8
star
30

php-github-status-api

Programmatically determine if GitHub is working well, or experiencing issues
PHP
7
star
31

is_offensive

🀬🀭 Is Offensive Helper Function - Check if a string contains offensive words or variations of them
PHP
7
star
32

simple-neural-network-js

Simple Neural Network library (JavaScript)
JavaScript
7
star
33

SEO-Middleware

Middleware to redirect to HTTPS when in production, and remove www. from URLs
PHP
7
star
34

php-password-cracker

PHP package to crack passwords
PHP
6
star
35

php-file-sync

Synchronise files between multiple local or remote file systems
PHP
6
star
36

wikipedia-info-box-parser

Parses the info boxes on Wikipedia pages into an associative array
PHP
6
star
37

php-server-info

Gets metrics on a server via an SSH connection
PHP
5
star
38

dotenv-loader

Automatically load in a `.env` file from the root of your project into environment variables
PHP
5
star
39

json-key-value-store

A simple JSON based key value store
PHP
5
star
40

php-wikitext-parser

Parse Wikitext in PHP
PHP
5
star
41

laravel-password-security-audit

πŸ” Artisan command to audit the security of your users' passwords
PHP
5
star
42

PhantomJS-Laravel-Testing

The PhantomJS Laravel Testing package allows you to easily test your Laravel application's JavaScript functionality.
PHP
5
star
43

laravel-malware-validation-rule

Scans uploaded files for viruses and other malware
PHP
5
star
44

slickwallet

Slick Wallet
JavaScript
5
star
45

laravel-route-restrictor

🚫 Laravel middleware to restrict a site or specific routes using HTTP basic authentication
PHP
5
star
46

laravel-geolocation-request

Laravel Geolocation Request
PHP
4
star
47

php-wikipedia-search

Easily search for articles on Wikipedia
PHP
4
star
48

Watchful-Eyes

Watchful Eyes - A JavaScript powered site monitor
JavaScript
4
star
49

php-postcodes

πŸ“¬ This library handles various UK postcode related tasks
PHP
4
star
50

object_to_array

This PHP package provides an `object_to_array` helper function. You can use this function to convert an object to an array.
PHP
4
star
51

php-distance

Allows for calculation of many types of distance between points
PHP
4
star
52

laravel-domain-to-locale

Change your Laravel app's locale based on the domain name
PHP
4
star
53

php-word-info

PHP library to look up information about words
PHP
4
star
54

attempt

πŸ” Attempt to run a function, automatically retrying if an exception occurs
PHP
3
star
55

twitter-to-mastodon-sync

A tool to sync Twitter tweets to a Mastodon account
PHP
3
star
56

uxdm-eloquent

πŸ”€ Eloquent source and destination for the UXDM data migrator
PHP
3
star
57

sms-providers

Provides data on popular SMS providers
PHP
3
star
58

omega-search

Easily add an intelligent search engine to your website or web application
PHP
3
star
59

uxdm-spatie-data-transfer-object

πŸ”€ Spatie Data Transfer Object source and destination for the UXDM data migrator
PHP
3
star
60

php-dot-net-ticks

⏱ PHP .NET Ticks helps you convert .NET ticks, a form of precise time measurement used by the .NET DateTime object.
PHP
3
star
61

uxdm-pdf-destination

PDF destination for the UXDM data migrator
PHP
2
star
62

ThisIsHowIRole

PHP role management system
PHP
2
star
63

omega-validator

Easy to use, framework independent data validator
PHP
2
star
64

email-structure-parser

πŸ“§ Parses the structure of multipart emails
PHP
2
star
65

WordScore

WordScore - A game in which each letter is worth a certain amount of points and you must take it in turns to guess the word with the highest score
Java
2
star
66

fuzzy-events

Perform actions based on a fuzzy string matches
PHP
2
star
67

php-languages

A tiny package to help convert between languages names (such as English, French, German) and various ISO language codes (such as en, fr, de).
PHP
2
star
68

symfony-password-exposed-bundle

πŸ”’ Symfony bundle that checks if a password has been exposed in a data breach.
PHP
1
star
69

DecentMessaging

Decent Messaging - a decentralised encrypted messaging system platform
Java
1
star
70

uxdm-excel

πŸ”€ Microsoft Excel source and destination for the UXDM data migrator
PHP
1
star
71

DecentChat

A decentralised chat application built on the Decent Messaging platform
JavaScript
1
star
72

translator

A simple PHP internationalisation library
PHP
1
star
73

php-base-search

Base interfaces/classes for searcher and search results
PHP
1
star
74

overflow-framework

PHP
1
star
75

php-reddit-search

Easily search for posts on Reddit
PHP
1
star
76

php-stackexchange-search

Easily search sites on the StackExchange network
PHP
1
star
77

js13k

Drawbacks - Our 2015 entry into http://js13kgames.com/
JavaScript
1
star
78

AutoTwit

A customisable Twitter bot that uses Markov chains and tries to act human
Java
1
star
79

eavp-demos

Demos for the Eloquent Attribute Value Prediction pacakge
PHP
1
star
80

one-hour-game-jam-236

HTML
1
star
81

uxdm-doctrine

Doctrine destination for the UXDM data migrator
PHP
1
star
82

password-suggester

PHP
1
star
83

neural-networks-experiment-two

Java
1
star
84

Soapsuds

Soapsuds is a PHP library that allows developers to easily create a SOAP web service from a regular class.
PHP
1
star
85

php-dcom

PHP Database Connection Object Manager
PHP
1
star