I've created some migration and seed files using bake, but now the dba guys say i should use an sql script to create and populate the tables. Is there a fast way of converting the files, without having to write the script by hand?
Yes, of course. Connect to SQL Server with SQL Profiler and run your current migration. Written profiled script is the converted one. You need only set appropriate Profiler's filter properties.
Related
My project has a big database. And I have just changed it to master/slave database to improve query time. Now I can change between master/slave database on controllers->actions using URLs.
But now I want to apply that change with cake shell too. I don't know if there's any way to determine which controllers->actions are being called in cake shell?
Please check this page :https://book.cakephp.org/3.0/en/console-and-shells/commands.html
And create your command for change this databases )
I am trying to implement a simple database using PHP & sqlite on my Linux/Apache server.
I can read from it quite readily, but I cannot perform any UPDATE, DELETE or INSERT actions. The Fatal Error I get is:
General error: 5 database is locked
As a simple example:
$pdo=new PDO('sqlite:test.sqlite');
$pdo->exec("INSERT INTO menus(id,name,description) VALUES(6,'test','this is a test')");
This waits for a long time (about a minute), and then reports the above error.
I have read a lot of suggestions, many of which suggest that the database or its containing folder should be writable. They are. (Or were. I made them world writable for testing, and restored more reasonable permissions when that failed.)
I have no trouble writing to the database using other techniques such as the sqlite3 command in Linux and the SQLite manager addon in Firefox.
I would welcome any comments on how to make this work.
Please, try to give the database file a 777 permission and try again. I suspect it has something to do with permissions because you are able to modify the database using sqlite3 program.
If it fails, then try to see the answers to this question.
Is it possible to version control a PHP + MySQL + Apache project? And could it keep track of the changes in the database, like for example if I added a new table, can I possibly commit that?
Thank you.
It is not normal to keep databases in version control. Some developers use a sqlite database for development so that it can be checked into version control, but this can lead to issues as sqlite syntax can be different from MySQL.
However, you can keep your database schema and migrations source control. Look at a projects such as mysql-php-migrations to get started.
There's a good tutorial on using PHP with Git at http://net.tutsplus.com/tutorials/other/easy-version-control-with-git/ - this should get you started.
Using Git for your PHP scripts is no problem, however tracking changes to the database is a little trickier. If you have SQL scripts that create the database structure then these can be version controlled with no problems. Otherwise you could use mysqldump to output the structure to an SQL script after any changes you make:
mysqldump -d -h localhost -u root -pmypassword mydatabase > dumpfile.sql
You could use git to track PHP scripts and SQL scripts that create the necessary database structure. Those SQL scripts could of course be version controlled and recreate the database schema at any given state.
Git is essentially SCM, which means Source Control. Tables in mysql are stored as binary files, so it isn't very good idea.
You can, however, store SQL queries which create these tables, allowing you to re-create them if you needed to.
As for php, it will be all good.
I recently released a really simple shell script that will help keep changes to a MySQL database under version control.
https://github.com/stevecomrie/mysql-version-control
I'm setting up Postgres database testing for a Kohana 3.2 project. I would like to create a temporary schema, run several sql scripts from file to establish the schema, and then do my testing and then drop the temporary schema in tearDown.
I've tried getting the contents of the scripts using file_get_contents($sqlfilename) and then using Kohana's DB:query() but that is hack and didn't work properly anyway.
pg_execute will not work because the files are batch scripts
Should I run psql commands with exec()?
What is the best/preferred way of running batch scripts on a Postgres DB from PHP?
Thanks!!!
pg_query can run multiple statements:
query
The SQL statement or statements to be executed. When multiple statements are passed to the function, they are automatically executed
as one transaction, unless there are explicit BEGIN/COMMIT commands
included in the query string. However, using multiple transactions in
one function call is not recommended.
Data inside the query should be properly escaped.
Thus, a hacky way to run a batch script would be:
pg_query($conn, "BEGIN; COMMIT;\n" . file_get_contents($filename));
The only correct alternative I can think of would be to run psql as an external program:
system("psql < " . escapeshellarg($filename));
I haven't tested either of these. Good luck.
My project is a collection of PHP scripts using MySQL as a database and needs to be installed locally using WAMP/LAMP/MAMP.
Previously I've been sending the users a link to a zipped archive and having them overwrite it, but since I took the plunge to GitHub, I've realized that there are far better ways; namely Service Hooks in GitHub. However, this would work fine as long as I don't alter the database in any way, which is a good possibility.
I've been toying with the idea of how I would implement this, but I can't find a clear solution. So far I've concluded with that I need to have a directory (say update/) which contains .sql files for each update. The PHP script will then check said directory for a file corresponding with the new version number (not sure how I will define a version number; I was thinking of using the commit ID, but that won't be available until after the commit, so...).
I would love some input on this!
Here's how I would tackle this (not the most elegant or performant):
Add a flag in the DB with a version number
Add a min-version number in your DB layer PHP file
Check that the DB version is greater than the min-version
If it is: continue about your business
Else: Run the PHP file in update/ which would have a series of ALTER TABLE commands to be run on the DB server
Update the min-version number in the DB to the latest number
All done
Alternately instead of querying the DB you can have a file which is generated by your DB interface PHP file (and ignored with .gitignore) which you can just as above.
I would really recommend checking out Doctrine and its migration feature.
This does exactly what you are looking for, plus you get a very nice tool for working with all other aspects of your database handling.