From a previous export of an Oracle DB, i have both xml and dtd files with lots of data but dbunit seems to ignore the dtdfiles when i try to import the data.
flatXMLBuilder.build(xmlFile);
I always get this errormessage when trying to reference one object of a table:
ERROR Table 'TABLE' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap
and the warning:
WARN session=global o.d.database.DatabaseConnection - The given schema 'SA' does not exist
I am not sure if the including of the dtd files would fix the schemaproblem (which is always set to PUBLIC)
But i would be happy if i at least could test it out.
The files are always in the same directory:
T1.xml
T1.dtd
Also the core problem seems to be the schema which is always set to PUBLIC on my target H2 in memory db.
I tried out some schema set methods like "set schema" or "init=create schema" in the connection url but the currentschema was always public.
So when i was logged in as the SA user for example i couldn't find any tables becaue in the SA schema are no tables only in PUBLIC.
I also tried the setSchema(String) method on the h2connection but it don't work (i get an uncatchable error on call)
Update:
Currently i use a FileInputStream to read the dtd file and add it to the builder:
builder.setMetaDataSetFromDtd(dtdStream);
But it didn't help with the problem.
If you want to create a schema x, and also set the default schema to x, in the database URL, then you need to use:
jdbc:h2:~/data/databaseName;init=create schema if not exists x\;set schema x
Please note the escaped semicolon. In Java, you need to escape the backslash as well:
String url = "jdbc:h2:~/data/databaseName;init=create schema if not exists x\\;set schema x";
This URL is a bit long. You might want to move all the statements to an init script, and just run that:
jdbc:h2:~/data/databaseName;init=runscript 'init.sql'
Related
I am using Strapi v3.0.0-beta.18.7
How to delete the ContentType and the related tables from DB ?
I tried the command below, but it is not deleting the db tables.
DELETE http://localhost:1337/content-type-builder/content-types/application::CONTENT_TYPE_NAME.CONTENT_TYPE_NAME
To delete the content-type and related db-tables in Strapi,
You can delete the folder inside /api folder having same name as your content-type
Suppose if you want to delete the "product" content type, you can delete the product folder inside of /api
The database's tables sync is not managed in the Content Type Builder plugin.
By default, Strapi doesn't delete anything from your database structure.
Strapi is customizable but you will not be able to update this.
Here is an issue that talks about this topic - https://github.com/strapi/strapi/issues/1114
The answers above are really helpful but don't explain, how you would actually go about deleting the table manually.
Suppose you run a local default installation with sqlite, you can find your database at .tmp/data.db. To connect to it, you will need a tool that you can get from sqlite directly:
https://sqlite.org/download.html
I guess you can add it to the PATH, but since I am a beginner and I just wanted it to work, i put the sqlite3.exe directly in the folder of the database and ran it.
To open the database, I used .open data.db and tested it with .tables which showed me all the tables that strapi created for me but didn't delete.
To ensure that I found the right table (recipe-cuisine) i looked at the content using .headers on followed by SELECT * FROM "recipe-cuisine";.
I finally deleted the whole table using DROP TABLE "recipe-cuisine";.
There is an awesome documentation on how to do other operations here: https://www.sqlitetutorial.net/
I hope that helps other beginners who struggle to delete the tables. If anybody has suggestions or helpful links with more information, that would be great!
Lets assume you need remove abc collection.
WARNING
Be sure you created backup and there are not other collections that contains abc substring.
Then you need execute commands:
DELETE FROM `users-permissions_permission` WHERE `controller` LIKE '%abc%';
DELETE FROM strapi_permission WHERE `subject` LIKE '%abc%';
DELETE FROM core_store WHERE `key` LIKE '%abc%';
DELETE FROM upload_file_morph WHERE related_type LIKE '%abc%';
DROP TABLE abc;
Then you need execute also:
rm -rf api/abc
Additional notes:
take care about plural names
be sure that there are no relations with other tables in other case you will see error
TypeError: Cannot read property 'globalId' of undefined
I want to get the schema name from my DataSource or Connection object so that it can be used in my SQL queries dynamically. I'm using DB2 and there is no implementation of connection.getSchema() in DB2 driver.
I'm using DataSource to get connection. Since connection.getSchema() is not working, I tried another approach as given below
connection.getMetaData().getURL()
But this is returning connection URL without schema information like below:
jdbc:db2://servername:1446/DBName
But i have given schema information in the URL while creating the datasource in embeddable Container.
jdbc:db2://servername:1446/DBName:currentSchema=mySchema
I need to get schema name to use it in query. Somebody knows how to get schema name.
Try the SQL statement
values current schema
The Db2BaseDataSource has a property currentSchema, along with a getter and a setter.
There's also a property called user .
setter:
db2ds.setCurrentSchema("fred");
getter:
String x = db2ds.getCurrentSchema() ;
Currently we are writing a page in mvc5, with an oracle sql database connected with entitiy framework 6.
We currently have two schemas in the oracle database, one for testing and the other for development. The model in entitiy framework is generated from the development database, and works perfectly with it.
The problem comes, when changing the connection string to the testing schema. When the connection string is changed the application is unable to locate the tables (as they still reference the development schemes).
Currently I can fix this, by deleting all the tables from the model, and recreating the model from the correct schema, or manually editing every file referencing the schema. Both solutions are kinda tiresome and error prone.
How is this scenario usually dealt with?
EDIT
It seems that changing the database and retaining the schema, does not produce any error. So this is only schema related.
I guess this is a perfect use case for using entity framework command interceptors. I just tried and it works perfectly, even for Entity Framework DB-First approach.
You can register a custom command interceptor like this:
DbInterception.Add(new ReplaceSchemaInterceptor(newSchema: "[my]"));
This line will replace [dbo] schema name with the [my] schema name, before the query reaches the database. Luckily, schema name is enclosed with square brackets when Entity Framework generates the command text, so it's easy to match and replace. BTW, I'm not an Oracle expert, so I'm assuming that Oracle queries also include schemas in the same format. If not, then maybe you will have to tweak the implementation a bit (to replace the schema from whatever format it is generated by EF).
ReplaceSchemaInterceptor is a class that implements IDbCommandInterceptor interface. Inside this class, you need to replace the schema with your own schema. Below is the implementation of this class:
class ReplaceSchemaInterceptor : IDbCommandInterceptor
{
private readonly string _newSchema;
public ReplaceSchemaInterceptor(string newSchema)
{
_newSchema = newSchema;
}
public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
command.CommandText = command.CommandText.Replace("[dbo]", _newSchema);
}
public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
}
public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
command.CommandText = command.CommandText.Replace("[dbo]", _newSchema);
}
public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
command.CommandText = command.CommandText.Replace("[dbo]", _newSchema);
}
}
And lastly, the code is not perfect. You need to add some null checks for the constructor parameters, and maybe get rid of the code duplication inside implementation methods when replacing command text (extract into reusable method?). Right now it just does what you had asked for.
With fluent mappings in Entity Framework code-first you can indicate the default schema at runtime. This is one statement in OnModelCreating in your DbContext subclass, for instance:
modelBuilder.HasDefaultSchema("dev");
You're used to regenerating the model from the database, from which I conclude that the model doesn't contain many (or any) customizations that would make model generation a painstaking operation. This also should make it relatively easy to move to code-first. So I'd recommend you do that.
In Visual Studio, you can generate a code-first model from an existing database by adding an "ADO.Net Entity Data Model" from the templates that come with Entity Framework tools for Visual Studio. (Probably pre-installed). Choose the option "Code First from database" and follow the guidelines.
If you do that, you'll find a connection string in the project containing the model. This connection string may serve as template for the connection string you will put in the config file of your executing assembly. You'll notice that it doesn't look like...
metadata=res://* ... provider=System.Data.SqlClient;provider connection string="...""
This is the connection string that belongs to a database-first edmx model. It contains a path to the metadata files that are generated as resources into the assembly. Instead, the connection string will be a simple ADO.Net connection string. With code-first, EF will generate the meta data at runtime.
If you have this, you can add an entry in your config file for the default database schema and use that to set the schema as I showed above.
It looks like something we are doing here at my workplace.
Use synonyms for your objects!
A possibility would be to create synonyms dynamically for your test tables - and remove references to schema in your files
Say the user that connects is CONNECT_USER - must be different user as the schemas you're using which are SCHEM_DEV and SCHEM_TEST.
Here is how I would do the switch (Oracle PL/SQL scripting - connected as CONNECT_USER):
begin
for x in (select * from all_tables where owner='SCHEM_DEV')
loop
--- drop synonyms on SCHEM_DEV objects
execute immediate 'drop synonym '||table_name ;
--- create synonyms on SCHEM_TEST objects
execute immediate ' create or replace synonym '||table_name||' for SCHEM_TEST.'||table_name ;
end loop;
end;
/
I have an existing MVC3 application and database that is on a SQL Server 2008 R2 and I had to add a bool item to an existing model.
After I added it to the model, I rebuilt and published the project. Then I opened up SQL Server Management Studio and went to the table and added the entry to the column as a bit, I had to make it nullable since the table already contains data.
I thought this would be all that I would need to do to get everything working. However I got this error:
The model backing the 'psllc_DB' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
I'm not sure what else to do, I went back to the code and changed the bool to bool? so it will be nullable but this didn't help either. I can't drop the entire database since it's full of data, however as a last ditch possibility I could drop this table and re-create it cause it only has a few entries, but I don't know if that will work. I'm not sure what else to do so any advice would be very appreciated.
Update
Since I'm not getting any responses, let me rephrase my question.
How should I update my database (a SQL Express mdf file) to add a bool Column to a Table that has data already? I need the database to match my updated MVC 3 Entity Code First model otherwise I get the above error.
Thanks
Since this is code-first, you should do this code-first: change the class and use EF-migrations to change the database. The way you do it, the model and the database may match, but the meta information in the database is not updated.
By the way, if you supply a default value, you can add a non-nullable column.
I have two tables that exist in the same Oracle database system but different schemas, which I've mapped like this:
ABC.Store:
component schema="ABC" table="Stores"
{
property name="Id" fieldtype="id" generator="sequence" sequence="store_id_seq";
property name="Products" fieldtype="one-to-many" cfc="Product";
}
DEF.Product:
component schema="DEF" table="Products"
{
property name="Id" fieldtype="id" generator="sequence" sequence="product_id_seq";
}
I set my application's default datasource as this.datasource = "ABC" in application.cfc.
The problem I'm running into here is whenever I try to save a Product. ColdFusion spits out an error that says the sequence cannot be found for the Id property on Product. This is because the product_id_seq sequence is in the DEF schema, but ColdFusion is trying to find it in the ABC schema, even though I set the schema on the Product as DEF.
If I set the datasource attribute on Product to DEF, I then get an error that says the Products property on Store is unmapped. This is because, as the ColdFusion documentation states:
"Since a Hibernate configuration uses a single data source, all related CFCs (using ORM relationships) must have the same data source."
My question then is, how do I map the two tables in two different schemas, using a sequence as an ID generator?
I've been able to get it to work if I specify the schema for the sequence:
property name="Id" fieldtype="id" generator="sequence" sequence="def.product_id_seq";
But this is hard-coded and I'd like it to be dynamic and pull the schema name from a configuration bean.
The only way I've been able to get this to work seamlessly was to:
Create a single user in database, in this case MySQL, that had access to the desired schemas.
Setup and configure a single datasource in CFIDE that utilizes the newly created user for authentication.
Set the datasource attribute in all desired persistent objects to the newly created datasource.
Set the schema attribute in all desired persistent objects to reference the correct schema, or database. (the two are synonymous in ColdFusion ORM)
Note: Be sure to use full component path when referencing CFCs in your COM.