Impossible to use due to the namespaces (non-commandline)
damms005 opened this issue · 9 comments
I find this tool impossible to use for non-commandline.
I simply wanted to test on localhost before using in production.
I downloaded the archive, extracted it to 'test' directory in root folder. I copied and pasted the sample code under the "Use library within project (non-commandline)" section of Readme.md
I got error: "Fatal error: Class 'DbSync\DbSync' not found in /media/deo/windowsbase/xampp/htdocs/iuo/portal/toolz/mysync/test.php on line 9"
No amount of tweaking made it work, including use include()/required(), appending backlash to beginning of namespace use directive, etc. (although my experience of using PHP namespaces is that you have to include() or require() source namespace files before using them, but does not work here)
I simply want to test this tool, now the first attempt at test is not even working, or is it simply impossible to use it as a normal PHP class script without any installations/extension loading?
Hi, I am sorry you are having problems getting started. It doesn't really sound like your problem is anything to do with this library, but more to do with correctly loading libraries into your application. However, I am happy to try and help if I can.
First of all, I would recommend installing this via composer as suggested in the docs, which is the standard package manager for PHP. You can do this using composer require mrjgreen/db-sync
.
If you are unsure how to use composer, you can get started here.
If this isn't an option for you, then you are most likely going to have to include each library file manually. This is going to be tricky, but should be possible. If this is the direction you are going to go, then please feel free to post the code you have tried to use here, and I'll try and make suggestions.
Thanks for your response.
First, composer not an option. I cannot install stuff on my godaddy account (except you can help on how to use composer on a shared-hosting godaddy hosting without them yelling all over)
With regards to what I have tried, I noticed some namespaces whose file are not in the downloaded zip (master). These namespaces are
use Database\Connection;
use Database\Query\Builder;
use Database\Query\Expression;
use Database\Query\Grammars\MySqlGrammar;
...etc
So I think I need those files.
What I have tried so far:
test.php
<?php
require( "DbSync/DbSync.php" );
require( "DbSync/Hash/HashInterface.php" );
require( "DbSync/Hash/HashAbstract.php" );
require( "DbSync/Hash/ShaHash.php" );
require( "DbSync/Hash/CrcHash.php" );
require( "DbSync/Hash/Md5Hash.php" );
require( "DbSync/Transfer/TransferInterface.php" );
require( "DbSync/Transfer/Transfer.php" );
require( "DbSync/Table.php" );
require( "DbSync/ColumnConfiguration.php" );
use DbSync\DbSync;
use DbSync\Transfer;
use DbSync\Hash\ShaHash;
use DbSync\Table;
use DbSync\ColumnConfiguration;
$blockSize = 4;
$transferSize = 4;
$sync = new DbSync(new \DbSync\Transfer\Transfer(new ShaHash(), $blockSize, $transferSize));
// $sync->setLogger(new YourPsrLogger());
$sync->dryRun(true);
$sync->delete(false);
$sourceConnection = "localhost";
$sourceDb = 'iqpluskn_iuokada;';
$sourceTable = 'SIStudentMaster';
$targetConnection = "localhost";
$targetDb = "iqpluskn_iuokada";
$targetTable = "payments_outgoing";
$syncColumns = 'id';
$ignoreColumns = 'date';
$sourceTable = new Table($sourceConnection, $sourceDb, $sourceTable);
$targetTable = new Table($targetConnection, $targetDb, $targetTable);
// if you only want specific columns
$columnConfig = new ColumnConfiguration($syncColumns, $ignoreColumns);
// optionally apply a where clause
$sourceTable->setWhereClause(new WhereClause("column_name = ?", ['value']));
$targetTable->setWhereClause(new WhereClause("column_name > ?", ['value']));
$sync->sync($sourceTable, $targetTable, $columnConfig);
?>
Also, as you may have seen, I do not know how to use $sourceTable->setWhereClause(new WhereClause("column_name = ?", ['value']));
. What I want to achieve is to compare if date column in SourceTable is greater than the date column in TargetTable.
You do not install composer on your remote server, so GoDaddy has nothing to do with this. Composer is something you use locally to install and manage your package dependencies.
You are right, this library has a dependency on https://github.com/mrjgreen/database. Composer will automatically install this for you.
I suggest you take a look at how to get started with composer for windows. This is the best advice I can offer at this stage, and it is absolutely the best way for you to achieve what you are trying to do.
So how do I use $sourceTable->setWhereClause(new WhereClause("column_name = ?", ['value']));
to be able to compare if date column in SourceTable is greater than the date column in TargetTable before so that the newer row replaces the old one (and then non-existing rows are also inserted)?
Unless you have very specific requirements, you will most likely not need to use the WhereClause
feature. The whole idea behind this tool is that it automatically compares and syncs differences in data between the source and target databases.
Is there a specific reason you need to check a date column?
Yes, I have a specific reason.
I have a remote server that stores records in users
table.
These same records on the remote server is also available locally on my local server.
This means that I have the users
table on the remote and my local server.
users
table has a column called last_updated_datetime
that stores value for last update.
So sometimes I do stuff locally that updates this record and I want to update such changes on the remote server. Therefore I need a tool that will update remote record when localserver.lastup.last_updated_datetime
> remoteserver.last_updated_datetime
; and then also add any new entry in the localserver.lastup.users
to remoteserver.users
.
Can this tool handle this? If not, no any tool that can handle it?
Okay - this sounds like a fairly strange requirement and possibly a little risky, both in terms of data integrity and security. I strongly suspect there is an architectural solution to your problem that would perhaps lead to a better set up, but if you do need to do this, then I would recommend the following:
- Create a new table on the remote server, that is a duplicate of
users
, E.G.users_sync
- Sync from
local.users
toremote.users_sync
without any data restrictrions - Run a SQL query on the remote server:
REPLACE INTO users
SELECT users_sync.* FROM users_sync
LEFT JOIN users USING(id)
WHERE users.last_updated IS NULL OR users_sync.last_updated > users.last_updated
Whao!
You're either a genius, a wizard, or simply a juju master master!..all of the above!
Thanks pal. You just saved the day!
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