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.

CommentFileSizeAuthor
#15 create.php.v2.txt14.25 KBdanielc
create.php.v1.txt11.43 KBdanielc
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nkurz’s picture

Thanks 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

danielc’s picture

Nate:

> 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.

justin3’s picture

Would 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...?

cre8d’s picture

Category: feature » support

I'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.

petepope’s picture

Version: » 4.6.2

When 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.

Bèr Kessels’s picture

Version: 4.6.2 » x.y.z
Category: support » feature

please start a new issue for new questions or issues, do no 'hijack' existing issues.

petepope’s picture

Version: x.y.z » 4.6.2

Ber -

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

Bèr Kessels’s picture

Version: 4.6.2 » x.y.z

Sorry. 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.

nbd’s picture

Title: Database Table Creation Script » Database Table Creation Script - problem
Assigned: Unassigned » nbd
Category: feature » support

While 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

jeff h’s picture

Thanks 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.

bossy22’s picture

Hi,

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

PaulN’s picture

Hello 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

PaulN’s picture

Just 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 ";
";
}
}

danielc’s picture

Paul:

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

danielc’s picture

Title: Database Table Creation Script - problem » Database Table Creation Script
Version: x.y.z »
Category: support » feature
Status: Needs review » Reviewed & tested by the community
FileSize
14.25 KB

Attached 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.

dman’s picture

Yay 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.

mkabot68’s picture

Title: Database Table Creation Script » RE: Database Table Creation Script
Status: Reviewed & tested by the community » Active

Does 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?

danielc’s picture

Component: base system » database system
Assigned: nbd » danielc
Status: Active » Reviewed & tested by the community

BoatDaddy: No, this does not directly deal with module tables. It just works on tables specified in the SQL scripts in the database directory.

Gerhard Killesreiter’s picture

Status: Reviewed & tested by the community » Closed (won't fix)

I suggest to apply for a CVS account and add this useful script to the tricks directory.

wrunt’s picture

The 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.

Darren Oh’s picture

I 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.

curtisc’s picture

I 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';

curtisc’s picture

Priority: Normal » Minor
Status: Closed (won't fix) » Closed (works as designed)

nevermind the previous post. I didn't realize that mysql didn't support periods in table names. Duh.

annabug’s picture

Hello. 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

zxmon’s picture

I 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

Darren Oh’s picture

See comment #21. You need to a file named database.mysql. Try creating a symbolic link to database.4.1.mysql.

zxmon’s picture

Thanks, 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

Darren Oh’s picture

You should be typing "create.php mysql", not "create.php database.mysql".

Darren Oh’s picture

You should be typing "create.php mysql", not "create.php database.mysql".

zxmon’s picture

Success!

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