diff --git a/lib/db/mdb2schemareader.php b/lib/db/mdb2schemareader.php
index 3f6cadd3dc44fd6fa9b6bd3a7d2d243b0854cdc6..05b9bd21289629b48b09d2cf6e197040dc755447 100644
--- a/lib/db/mdb2schemareader.php
+++ b/lib/db/mdb2schemareader.php
@@ -131,7 +131,12 @@ class OC_DB_MDB2SchemaReader {
 			if (empty($options['default'])) {
 				if ($type == 'integer') {
 					if (empty($options['default'])) {
-						$options['default'] = 0;
+						if (empty($options['notnull'])) {
+							unset($options['default']);
+						}
+						else {
+							$options['default'] = 0;
+						}
 					}
 				}
 				if (!empty($options['autoincrement'])) {
diff --git a/lib/db/mdb2schemawriter.php b/lib/db/mdb2schemawriter.php
new file mode 100644
index 0000000000000000000000000000000000000000..a6367a0e3549e510b9e3ab728a094c6f500a07ad
--- /dev/null
+++ b/lib/db/mdb2schemawriter.php
@@ -0,0 +1,127 @@
+<?php
+/**
+ * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class OC_DB_MDB2SchemaWriter {
+	static public function saveSchemaToFile($file, $sm) {
+		$xml = new SimpleXMLElement('<database/>');
+		$xml->addChild('name', OC_Config::getValue( "dbname", "owncloud" ));
+		$xml->addChild('create', 'true');
+		$xml->addChild('overwrite', 'false');
+		$xml->addChild('charset', 'utf8');
+		foreach ($sm->listTables() as $table) {
+			self::saveTable($table, $xml->addChild('table'));
+		}
+		file_put_contents($file, $xml->asXML());
+		return true;
+	}
+
+	private static function saveTable($table, $xml) {
+		$xml->addChild('name', $table->getName());
+		$declaration = $xml->addChild('declaration');
+		foreach($table->getColumns() as $column) {
+			self::saveColumn($column, $declaration->addChild('field'));
+		}
+		foreach($table->getIndexes() as $index) {
+			if ($index->getName() == 'PRIMARY') {
+				$autoincrement = false;
+				foreach($index->getColumns() as $column) {
+					if ($table->getColumn($column)->getAutoincrement()) {
+						$autoincrement = true;
+					}
+				}
+				if ($autoincrement) {
+					continue;
+				}
+			}
+			self::saveIndex($index, $declaration->addChild('index'));
+		}
+	}
+
+	private static function saveColumn($column, $xml) {
+		$xml->addChild('name', $column->getName());
+		switch($column->getType()) {
+			case 'SmallInt':
+			case 'Integer':
+			case 'BigInt':
+				$xml->addChild('type', 'integer');
+				$default = $column->getDefault();
+				if (is_null($default) && $column->getAutoincrement()) {
+					$default = '0';
+				}
+				$xml->addChild('default', $default);
+				$xml->addChild('notnull', self::toBool($column->getNotnull()));
+				if ($column->getAutoincrement()) {
+					$xml->addChild('autoincrement', '1');
+				}
+				if ($column->getUnsigned()) {
+					$xml->addChild('unsigned', 'true');
+				}
+				$length = '4';
+				if ($column->getType() == 'SmallInt') {
+					$length = '2';
+				}
+				elseif ($column->getType() == 'BigInt') {
+					$length = '8';
+				}
+				$xml->addChild('length', $length);
+				break;
+			case 'String':
+				$xml->addChild('type', 'text');
+				$default = trim($column->getDefault());
+				if ($default === '') {
+					$default = false;
+				}
+				$xml->addChild('default', $default);
+				$xml->addChild('notnull', self::toBool($column->getNotnull()));
+				$xml->addChild('length', $column->getLength());
+				break;
+			case 'Text':
+				$xml->addChild('type', 'clob');
+				$xml->addChild('notnull', self::toBool($column->getNotnull()));
+				break;
+			case 'Decimal':
+				$xml->addChild('type', 'decimal');
+				$xml->addChild('default', $column->getDefault());
+				$xml->addChild('notnull', self::toBool($column->getNotnull()));
+				$xml->addChild('length', '15');
+				break;
+			case 'Boolean':
+				$xml->addChild('type', 'integer');
+				$xml->addChild('default', $column->getDefault());
+				$xml->addChild('notnull', self::toBool($column->getNotnull()));
+				$xml->addChild('length', '1');
+				break;
+			case 'DateTime':
+				$xml->addChild('type', 'timestamp');
+				$xml->addChild('default', $column->getDefault());
+				$xml->addChild('notnull', self::toBool($column->getNotnull()));
+				break;
+
+		}
+	}
+
+	private static function saveIndex($index, $xml) {
+		$xml->addChild('name', $index->getName());
+		if ($index->isPrimary()) {
+			$xml->addChild('primary', 'true');
+		}
+		elseif ($index->isUnique()) {
+			$xml->addChild('unique', 'true');
+		}
+		foreach($index->getColumns() as $column) {
+			$field = $xml->addChild('field');
+			$field->addChild('name', $column);
+			$field->addChild('sorting', 'ascending');
+			
+		}
+	}
+
+	private static function toBool($bool) {
+		return $bool ? 'true' : 'false';
+	}
+}
diff --git a/lib/db/schema.php b/lib/db/schema.php
index ca90e300e0d5520decb07fbc4804839f4c8ab9c9..231b8068af0b2f7da3da4d337e14d553a8ee6a07 100644
--- a/lib/db/schema.php
+++ b/lib/db/schema.php
@@ -18,11 +18,10 @@ class OC_DB_Schema {
 	 *
 	 * TODO: write more documentation
 	 */
-	public static function getDbStructure( $conn, $file ,$mode=MDB2_SCHEMA_DUMP_STRUCTURE) {
+	public static function getDbStructure( $conn, $file, $mode=MDB2_SCHEMA_DUMP_STRUCTURE) {
 		$sm = $conn->getSchemaManager();
-		$fromSchema = $sm->createSchema();
 
-		return OC_DB_MDB2SchemaWriter::saveSchemaToFile($file);
+		return OC_DB_MDB2SchemaWriter::saveSchemaToFile($file, $sm);
 	}
 
 	/**
diff --git a/tests/lib/dbschema.php b/tests/lib/dbschema.php
index becfe9edf4554a1109c6c25b786cff54a3643cc5..cd408160afb3770ddaeeb28363602ca51e3b7013 100644
--- a/tests/lib/dbschema.php
+++ b/tests/lib/dbschema.php
@@ -55,7 +55,6 @@ class Test_DBSchema extends UnitTestCase {
 	}
 
 	public function doTestSchemaDumping() {
-	return;
 		$outfile = 'static://db_out.xml';
 		OC_DB::getDbStructure($outfile);
 		$content = file_get_contents($outfile);