mrjgreen / db-sync

PHP library with command line tool for efficiently syncing tables between remote MySQL databases
MIT License
292 stars 91 forks source link

RuntimeException table does not have a primary key #43

Open njovujsh opened 6 years ago

njovujsh commented 6 years ago

My Table has a primary key, but when I run The DbSyn, it returns an error table does not have a primary key what can I do. Thanks in advance

mrjgreen commented 6 years ago

Can you post the table definitions of both the source and destination tables?

If it’s saying you don’t have a primary key, then I suspect that is the case for one of the tables...

Cheers Joe Green

On 29 Jul 2018, at 05:28, njovu notifications@github.com wrote:

My Table has a primary key, but when I run The DbSyn, it returns an error table does not have a primary key what can I do. Thanks in advance

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

njovujsh commented 6 years ago

Step 1 This is the first Table step 1

Step 2 Second Table step 2 Step 3 step 3

Step 4 step 4 I first tested on localhost for both of them.

njovujsh commented 6 years ago

There is a lighter library I tried and it works on local host. The only limitation is that I failed to connect to the remote server!. This one looks the most awesome but have so far failed to use it.

mrjgreen commented 6 years ago

Looks like you have a mistake in your second “new Table()” line, where you’ve specified the database name in the table field. I think “db_spotout.test_table” should just be “test_table”

Cheers Joe Green

On 29 Jul 2018, at 14:00, njovu notifications@github.com wrote:

Step 1 **

** Step 2

Step 3

Step 4

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

njovujsh commented 6 years ago

step 5 I tried without and with the database concoction and it gave me same results

On Sun, Jul 29, 2018 at 12:12 PM, Joe Green notifications@github.com wrote:

Looks like you have a mistake in your second “new Table()” line, where you’ve specified the database name in the table field. I think “db_spotout.test_table” should just be “test_table”

Cheers Joe Green

On 29 Jul 2018, at 14:00, njovu notifications@github.com wrote:

Step 1 **

** Step 2

Step 3

Step 4

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mrjgreen/db-sync/issues/43#issuecomment-408688102, or mute the thread https://github.com/notifications/unsubscribe-auth/AGHQvQwYJGI10T8p_ZVzRTWgPpkikydIks5uLd8BgaJpZM4VlSI0 .

njovujsh commented 6 years ago

step 5

rene-hermenau commented 6 years ago

Can you post a few lines of the data from the source table?

What happens if you include both columns for syncing?

$columnConfig = new ColumnConfiguration(array('id', 'name'), array());

njovujsh commented 6 years ago

datasample Code

code data The error is of course still there as from the beginning

mrjgreen commented 6 years ago

So the bit of code responsible for deciding the primary key is here: https://github.com/mrjgreen/db-sync/blob/v3/src/Table.php#L100

It's essentially running this query:

SHOW INDEX FROM db_timesheet.test_table WHERE `key_name` = 'PRIMARY'

which should return a result set including the columns: Column_name, Seq_in_index Eg:

Column_name Seq_in_index
Col1 0
Col2 1

We transform this into an array sorted by the Seq_in_index column:

[
    0 => 'Col1',
    1 => 'Col2'
]

If you could post the results of the query:

SHOW INDEX FROM db_timesheet.test_table WHERE `key_name` = 'PRIMARY'

We should be able to figure out what's going wrong here...

njovujsh commented 6 years ago

That is the result I get when I pass the query (SHOW INDEX FROM db_timesheet.test_table WHERE key_name = 'PRIMARY')

On Sun, Aug 5, 2018 at 12:25 PM Joe Green notifications@github.com wrote:

So the bit of code responsible for deciding the primary key is here: https://github.com/mrjgreen/db-sync/blob/v3/src/Table.php#L100

It's essentially running this query:

SHOW INDEX FROM db_timesheet.test_table WHERE key_name = 'PRIMARY'

which should return a result set including the columns: Column_name, Seq_in_index Eg: Column_name Seq_in_index Col1 0 Col2 1

We transform this into an array sorted by the Seq_in_index column:

[ 0 => 'Col1', 1 => 'Col2' ]

If you could post the results of the query:

SHOW INDEX FROM db_timesheet.test_table WHERE key_name = 'PRIMARY'

We should be able to figure out what's going wrong here...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mrjgreen/db-sync/issues/43#issuecomment-410531286, or mute the thread https://github.com/notifications/unsubscribe-auth/AGHQvVClOxqBkRWFiAcOxhVeM0KMxmR8ks5uNxx2gaJpZM4VlSI0 .

rene-hermenau commented 6 years ago

Where is the result?

njovujsh commented 6 years ago

issues

mrjgreen commented 6 years ago

I’m sorry I think I’m going to struggle to diagnose this one.

The only thing I can suggest is that you have your connection/database/table names are incorrect and you are using dbsync against the wrong table... is this something you can double check?

I can’t see anything wrong with the code you have posted, but I can’t see a way that this error could be generated for any other reason than the primary key is missing from the table.

Cheers Joe Green

On 14 Aug 2018, at 11:58, njovu notifications@github.com wrote:

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

njovujsh commented 6 years ago

Thanks for the effort Joe, but unless you want me to share the whole credentials which I can, I have not missed any names. In-fact I used the same table name with two simple columns to make sure I get everything right!

andygithubchen commented 6 years ago

src/Table.php line : 98, add this line: $name = str_replace('"', '`', $name);

MarioRadu commented 6 years ago

Hi, I'm trying to use this packet but for some reason the code doesn't return no error but at the same time it doesn't work . Here is my sample code :

error_reporting(E_ALL); ini_set('display_errors', 1);

require_once DIR . '/vendor/autoload.php';

use \DbSync\DbSync; use \DbSync\Transfer\Transfer; use \DbSync\Hash\ShaHash; use \DbSync\Table; use \DbSync\ColumnConfiguration;

$sync = new DbSync(new Transfer(new ShaHash(),1024,8)); //$sync->setLogger(new Symfony\Component\Console\Logger\ConsoleLogger());

$sync->dryRun(false);

$sync->delete(false);

$sourceConnection = new PDO('mysql:host=hostIp;dbname=dbName','dbName','password'); $targetConnection = new PDO('mysql:host=127.0.0.1;dbname=dbName','dbName','password');

$sourceTable = new Table(new \Database\Connection($sourceConnection), 'aliapidemia', 'categories'); $targetTable = new Table(new \Database\Connection($targetConnection), 'aliapidemia', 'categories');

var_dump($sync->sync($targetTable, $sourceTable));

echo "done";

It only prints out "done" and doesn't do anything. Someone can please advice ? Thanks ! )

eliasfarah commented 5 years ago

I figured out the mistake, if you look at the command source, you'll see diff code: look some example:

require "vendor/autoload.php";

use Database\Connectors\ConnectionFactory;
use DbSync\DbSync;
use DbSync\Hash\ShaHash;
use DbSync\Table;
use DbSync\Transfer\Transfer;

$sync = new DbSync(new Transfer(new ShaHash()));

$sync->dryRun(false);

$source = createConnection('localhost', 'root', '', 'utf8');
$destination = createConnection('localhost', 'root', '', 'utf8');

$sourceTable = new Table($source, 'database1', 'table-origin');
$targetTable = new Table($destination, 'database2', 'table-destination');

$sync->sync($sourceTable, $targetTable);

function createConnection($host, $user, $password, $charset)
{
    return (new ConnectionFactory())->make([
        'host'      => $host,
        'port'      => 3306,
        'username'  => $user,
        'password'  => $password,
        'charset'   => $charset,
        'collation' => 'utf8_general_ci',
        'driver'    => 'mysql',

        'options' => [
            \PDO::ATTR_ERRMODE               => \PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE    => \PDO::FETCH_ASSOC,
            \PDO::ATTR_EMULATE_PREPARES      => false,
        ]
    ]);
}

You just have to use ConnectionFactory instead of \PDO

csopheak commented 5 years ago

thanks

On Sat, May 25, 2019 at 3:34 AM Elias Farah notifications@github.com wrote:

I figured out the mistake, if you look at the command source, you'll see diff code: look some example:

require "vendor/autoload.php";

use Database\Connectors\ConnectionFactory; use DbSync\DbSync; use DbSync\Hash\ShaHash; use DbSync\Table; use DbSync\Transfer\Transfer;

$sync = new DbSync(new Transfer(new ShaHash()));

$sync->dryRun(false);

$source = createConnection('localhost', 'root', '', 'utf8'); $destination = createConnection('localhost', 'root', '', 'utf8');

$sourceTable = new Table($source, 'database1', 'table-origin'); $targetTable = new Table($destination, 'database2', 'table-destination');

$sync->sync($sourceTable, $targetTable);

function createConnection($host, $user, $password, $charset) { return (new ConnectionFactory())->make([ 'host' => $host, 'port' => 3306, 'username' => $user, 'password' => $password, 'charset' => $charset, 'collation' => 'utf8_general_ci', 'driver' => 'mysql',

    'options' => [
        \PDO::ATTR_ERRMODE               => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_DEFAULT_FETCH_MODE    => \PDO::FETCH_ASSOC,
        \PDO::ATTR_EMULATE_PREPARES      => false,
    ]
]);

}

You just have to use ConnectionFactory instead of \PDO

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mrjgreen/db-sync/issues/43?email_source=notifications&email_token=ADSQJATKFMYCPMSMEBUXB4LPXBGO3A5CNFSM4FMVEI2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWGQFRA#issuecomment-495780548, or mute the thread https://github.com/notifications/unsubscribe-auth/ADSQJAUVK3WF6S3O2HWOTALPXBGO3ANCNFSM4FMVEI2A .