Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Attached is a PHP script that creates all of the database tables for all sites in /drupal/sites/*.
It works with CVS checkouts of either HEAD or the DRUPAL-4-6 branch as of 2005-04-14 19:00 UTC.
Until it gets added to CVS, you can use it by saving the latest version of the file from this page, rename it "create.php" and put it in the "/drupal/database" directory. Then either execute it from the command line or point your web browser to it. For exact usage instructions, read the documentation inside the script.
Comment | File | Size | Author |
---|---|---|---|
#15 | create.php.v2.txt | 14.25 KB | danielc |
create.php.v1.txt | 11.43 KB | danielc |
Comments
Comment #1
nkurz CreditAttribution: nkurz commentedThanks Daniel!
I got it, used it, and it worked fine. Documentation and instructions were very clear. I had to modify a bit because I had already created the set of tables for my 'default' site, so at first it failed because the 'access' table already existed. I got around this by just putting in a hardcoded test for 'if ($site == 'default') {continue;}, but obviously this isn't a general solution.
It would probably be good (if you are interested in making this more generally useful) to add a way to specify which sites to create tables for, or alternatively which sites to ignore creating tables for. I'm guessing that many people (like me) are going to want something like this when the add a new site, and not only when there are setting up a new system. The easiest way of doing this might be to just change the error handling if a table creation fails. Instead of stopping, it could just ignore the error and continue.
--nate
Comment #2
danielc CreditAttribution: danielc commentedNate:
> it worked fine.
Good.
> add a way to specify which sites to create tables for
Simple. Temporarily those directories out of /drupal/sites. Though that would cause problems for live sites.
> The easiest way of doing this might be to just change the error
> handling if a table creation fails. Instead of stopping, it could
> just ignore the error and continue.
An interesting thought.
Comment #3
justin3 CreditAttribution: justin3 commentedWould it be possible to change this value in the create.php script
$sites_dir = '../sites';
change '../sites' to '../newsites'
would this allow me add two new directories with their settings.php file into the 'newsites' folder and have these table-prefixes added by the script, then i could move the new sites to the regular 'sites' folder...?
Comment #4
cre8d CreditAttribution: cre8d commentedI'd love to get this working but I ran into a problem - I get this error:
Found no valid settings.php files in /drupal/sites/*.'
I have followed the previous person's suggestion, as I've already created the tables for my default site and have changed the code to look in newsites. It seems to fail at this point:
if ($db_url == $default_url) {
continue;
}
$db_url and $default_url are the same on purpose because I want to use the same database for both of the sites and share some tables.
Any help appreciated.
Comment #5
petepope CreditAttribution: petepope commentedWhen I tried to execute it through the browser, I got the following error message:
"Fatal error: Table 'access' already exists query: CREATE TABLE access ( aid tinyint(10) NOT NULL auto_increment, mask varchar(255) NOT NULL default '', type varchar(255) NOT NULL default '', status tinyint(2) NOT NULL default '0', PRIMARY KEY (aid) ) TYPE=MyISAM in /home/petepope/public_html/drupal/includes/database.mysql.inc on line 66"
I'm a relative newbie to this game, and I'd appreciate any help that might be forthcoming.
Comment #6
Bèr Kessels CreditAttribution: Bèr Kessels commentedplease start a new issue for new questions or issues, do no 'hijack' existing issues.
Comment #7
petepope CreditAttribution: petepope commentedBer -
I fail to see how reporting an issue with the script that is the topic of the thread can be 'hijacking'.
Rather than upset anybody, though, I will open a new issue...
- Pete
Comment #8
Bèr Kessels CreditAttribution: Bèr Kessels commentedSorry. It was unclear to me that your error had anything to do with the script. Sorry for that. I was referring to changing the state of the issue to 'support' and then to 4.6.
This is a patch for CVS, and not a place to post support issues for 4.6
Puttingversion back to CVS.
Comment #9
nbd CreditAttribution: nbd commentedWhile running crate.php I came into this problem:
Duplicate entry 'modules/block.module' for key 1 query: INSERT INTO system VALUES ('modules/block.module','block','module',"1,0,0)
I assume it's because the settings table is shared by the subsites and create.php tries to update it again for each subsite. what can I do now?
For the moment I do want the settings I change to be effective across the sub-sites. In the future I may want seperate settings for each.
For additional information my specific sites db_prefix is as follows:
$db_prefix = array(
'default' => 'abc_',
'authmap' => 'shared_',
'profile_fields' => 'shared_',
'profile_values' => 'shared_',
'role' => 'shared_',
'sequences' => 'shared_',
'sessions' => 'shared_',
'users' => 'shared_',
'users_roles' => 'shared_',
'users_uid_seq' => 'shared_', // for pgsql
);
Thanks,
nbd
Comment #10
jeff h CreditAttribution: jeff h commentedThanks Daniel for a great script.
I am wondering whether someone cleverer than me would be able to modify this code so that it simply continues if a table already exists? That way it would be possible to run this script every time a new site needs to be added to the sites folder. This would make the script really really useful.
Comment #11
bossy22 CreditAttribution: bossy22 commentedHi,
I have a problem with the table ceration script.
I have subdirs in my sites folder
sites/default
sites/hendrikmartz.test
when i run the script, poitning the web browser to
http://drupal.test/database/create.php?mysql
I get a blank page, no comments, no nothing.
no tables were created, when i check the db with phpmyadmin.
in the sites/hendrikmartz.test/settings.php
i've set the $base_url to http://hendrikmartz.test
and the $db_prefix = 'hm_';
any suggestions?
thanx
bossy
Comment #12
PaulN CreditAttribution: PaulN commentedHello all,
I got an error trying to run this script from browser and command line:
IE:
http://localhost/database/create.php?dbms=mysql
returns:
Fatal error: Call to undefined function drupal_maintenance_theme() in c:\Inetpub\wwwroot\includes\database.mysql.inc on line 63
PHP Fatal error: Call to undefined function drupal_maintenance_theme() in c:\Inetpub\wwwroot\includes\database.mysql.inc on line 63
from cmd
php ./create.php mysql
returns the same error
It looks like I'm missing a patch.
I'm using MySql 5/ PHP 5/ Drupal 4.7
Please, help
Comment #13
PaulN CreditAttribution: PaulN commentedJust in case someone else has the same problem,
I'm posting a short term workaround here.
Commenting out execution of generated queries in execute_array_queries function and adding the echo for the queries instead will output the generated queries into browser.
You can eather copy/paste the output into file or redirect into file (if running from command prompt) and source the output directly in mysql.
function execute_arrays_queries($dbms, $db_url, $queries) {
global $active_db;
$includes_dir = '../includes';
require_once $includes_dir . '/database.' . $dbms . '.inc';
/*
* Don't need to catch errors here due to die() in db_connect().
*
* The return value must be stored in a varialbe named $active_db
* because that's the connection variable used in _db_query().
*/
//$active_db = db_connect($db_url);
ksort($queries);
foreach ($queries as $query) {
// Don't need to catch errors here due to triger_error() in _db_query().
// _db_query($query);
echo $query;
echo ";
";
}
}
Comment #14
danielc CreditAttribution: danielc commentedPaul:
Read the error message. You are having a problem with includes/database.mysql.inc not finding the drupal_maintenance_theme() funciton. This has nothing to do with the table creation script. The drupal_maintenance_theme() function is not part of Drupal 4.6. It is a new function in bootstrap.php in HEAD. Sounds like your installation has a mixture of new and old files.
--Dan
Comment #15
danielc CreditAttribution: danielc commentedAttached is an updated version of the table creation script which makes installing multiple Drupal sites easier.
Changes in the new version:
+ Now works with both Drupal 4.7 and 4.6.
+ Skips tables, indices and functions that already exist.
The documentation is also improved to explain why $db_url's are not allowed to use the default value.
Comment #16
dman CreditAttribution: dman commentedYay for this script!
It is so cool.
I'm just now experimenting with multi-sites, and this made things really automatic.
I suggested an automation for table creation (specifically for modules) a while ago, but not much happened from it. I am however using this method in my own modules.
I believe the proposed Install system should take care of this, eventually.
.dan.
Comment #17
mkabot68 CreditAttribution: mkabot68 commentedDoes this script take into account the tables that need to be created for installed modules? If so, does it deal with the proper db_prefix for these tables?
Comment #18
danielc CreditAttribution: danielc commentedBoatDaddy: No, this does not directly deal with module tables. It just works on tables specified in the SQL scripts in the database directory.
Comment #19
Gerhard Killesreiter CreditAttribution: Gerhard Killesreiter commentedI suggest to apply for a CVS account and add this useful script to the tricks directory.
Comment #20
wrunt CreditAttribution: wrunt commentedThe problem that PaulN got only occurs when something goes wrong in db_connect. It tries to print out an error message, and calls drupal_maintenance_theme(), which fails. The reason I got this error was that I had assumed that the script would create the database for me if it didn't exist, but it didn't. This is probably a good thing as my drupal db user shouldn't have the permissions to create a database. Anyway, if you're getting this error, try creating your database before running create.php.
Comment #21
Darren OhI tried this script in the Drupal 4.7.0 release. It will not work on a MySQL database unless there is a file named database.mysql in the database directory. 4.7.0 contains files named database.4.0.mysql and database.4.1.mysql. The script worked after I created a symbolic link to one of these files and named it database.mysql.
Comment #22
curtisc CreditAttribution: curtisc commentedI tried running the script and received the following error:
php create.php mysql
Query:CREATE TABLE aboutdecks.org_access (
aid tinyint(10) NOT NULL auto_increment,
mask varchar(255) NOT NULL default '',
type varchar(255) NOT NULL default '',
status tinyint(2) NOT NULL default '0',
PRIMARY KEY (aid)
)
DEFAULT CHARACTER SET utf8
Error:CREATE command denied to user 'drupaluser'@'localhost' for table 'org_access'
query: CREATE TABLE testsite.org_access (
aid tinyint(10) NOT NULL auto_increment,
mask varchar(255) NOT NULL default '',
type varchar(255) NOT NULL default '',
status tinyint(2) NOT NULL default '0',
PRIMARY KEY (aid)
)
DEFAULT CHARACTER SET utf8 in /var/www/html/drupal/includes/database.mysql.inc on line 120.
It seems as though it's not encoding the ASCII quotes properly because it's trying to login to mysql with HTML ASCII quotes around the username, which mysql doesn't understand. Any suggestions? My settings.php file just has this as the $db_url: $db_url = 'mysql://drupaluser:thepassword@localhost/drupal';
Comment #23
curtisc CreditAttribution: curtisc commentednevermind the previous post. I didn't realize that mysql didn't support periods in table names. Duh.
Comment #24
annabug CreditAttribution: annabug commentedHello. I realize I'm probably doing something very simple as far as what I'm doing incorrect. I just can't find it. I'm using the script to create a 2nd set of tables. In my 'sites' folder I have 'default' and 'CT'. I need the tables created for CT. I ran the script and recieve:
Your tables were created! But 57 were skipped because they already existed.
So I tried disabling the default folder - same thing. It appears to no be picking up the CT folder. Any help would be appreciated. Thank you.
- Anna
Comment #25
zxmon CreditAttribution: zxmon commentedI copied the text from V2 of the script into a PHP file in the /database folder of my local copy of Drupal ...
cd database
/usr/bin/php ./create.php mysql
always returns
'mysql' is an invalid/unsupported DBMS.
Same from the web. This sounds like a perfect script for me (don't know much about SQL statements), and everybody else is enjoying it. So, any anybody clues as to what I'm missing?
Thanks
x
Comment #26
Darren OhSee comment #21. You need to a file named database.mysql. Try creating a symbolic link to database.4.1.mysql.
Comment #27
zxmon CreditAttribution: zxmon commentedThanks, I tried both the symbolic link and also just copying the 4.1 file from the original tarball to "database.mysql"
In The Browser
http://www.xavierthomas.net/database/create.php?dbms=database.mysql
On The Command Line
/var/www/web5/web/database# /usr/bin/php ./create.php database.mysql
both return " 'database.mysql' is an invalid/unsupported DBMS. "
I have Drupal-4.7.3, MySQL 5.0.22, and PHP5
Comment #28
Darren OhYou should be typing "create.php mysql", not "create.php database.mysql".
Comment #29
Darren OhYou should be typing "create.php mysql", not "create.php database.mysql".
Comment #30
zxmon CreditAttribution: zxmon commentedSuccess!
It worked when I created a symbolic link "database.myql" to "database.4.1.mysql" from a default install.
Thanks again for your help.
x