How do you perform database migrations with Meteor? With Ruby on Rails there is ActiveRecord::Migration. Is there an equivalent mechanism in Meteor?
For example, I make an app with some user data. I'm storing the data in Mongo using a JSON format. The app changes, and the JSON database schema needs to change. I can write a migration method to change the schema, however, I only want this to run if the server database is out of date.
There's nothing built in for this. What I've done myself for now is similar to how Rails works, but as part of startup instead of a separate task. First create a Meteor.Collection called Migrations, and then for each discrete migration, create a function under the server subdirectory that runs on startup. It should only run the migration if it hasn't run before, and it should flag the migration in the Migrations collection once its done.
// database migrations
Migrations = new Meteor.Collection('migrations');
Meteor.startup(function () {
if (!Migrations.findOne({name: "addFullName"})) {
Users.find().forEach(function (user) {
Users.update(user._id, {$set: {fullname: users.firstname + ' ' + users.lastname}});
});
Migrations.insert({name: "addFullName"});
}
});
You could extend this technique to support down migrations (look for the existence of a given migration and reverse it), enforce a sort order on the migrations, and split each migration into a separate file if you wanted.
It'd be interesting to think about a smart package for automating this.
As Aram pointed already in the comment, percolate:migrations package gives you what you need. Sample
Migrations.add({
version: 1,
name: 'Adds pants to some people in the db.',
up: function() {//code to migrate up to version 1}
down: function() {//code to migrate down to version 0}
});
Migrations.add({
version: 2,
name: 'Adds a hat to all people in the db who are wearing pants.',
up: function() {//code to migrate up to version 2}
down: function() {//code to migrate down to version 1}
});
I created a smart package for this use case.
See https://atmosphere.meteor.com/package/migrations
Related
I have created an ASP.NET Core web app using the MVC template, added my model class, added a corresponding controller, and ran add-migration "initialsetup" followed by update-database at the package manager console.
However, I have ended up with 2 local databases:
Why is this?
I had some difficulty getting here; e.g. for the controller name, I went for ConsumerModelController (singular) despite its automatic suggestion for ConsumerModelsController (plural) which ended up failing, but when I then went for the default suggested plural name, it worked...
Try deleting both databases and execute Update-Database again, It should create one database? Also try renaming your database name into something easier to be compared.
I am trying to use SearchKit and I want to know how to set up an ElasticSearch instance indexed with sample movie dataset.
Tried using:
AWS ElasticSearch Service. But don't have an actual dataset to upload through bulk API. The one that I have doesn't have indexing.
Sample data:
[
{
"movie_title":"Avatar ",
"director_name":"James Cameron",
"actor_1_name":"CCH Pounder",
"actor_2_name":"Joel David Moore",
"genres":"Action|Adventure|Fantasy|Sci-Fi",
"language":"English",
"country":"USA",
"content_rating":"PG-13",
"budget":"237000000",
"title_year":"2009",
"plot_keywords":"avatar|future|marine|native|paraplegic",
"movie_imdb_link":"http://www.imdb.com/title/tt0499549/?ref_=fn_tt_tt_1"
},
...
]
Tried using appbase.io ready made movie-dataset. But I am not sure how to connect it with SearchKitManager().
Would really appreciate any help.
you may want to try ReactiveSearch - it's an alternative to SearchKit from appbase.io (I work there) that's actively maintained. It supports direct use of appbase.io apps.
Re: SearchKitManager(), I am not sure if that may work as we don't support cluster level routes.
I've got a CakePHP based web app that needs some initial configuration. I'd like to do the equivalent of the mysql source command to set up a bunch of tables / initial rows, then execute a $this->User->save() command to create the root account (I think this needs to be done via code since it'll use the salt value for the local install of CakePHP, which might/should be different than the one on my dev machine), etc, etc.
My hack-y solution is to expose a public method on a controller that does this, direct my browser to it, then set stuff up (via the Configure::load and Configure::dump) so that the route from that URL to the method is removed after the installation is complete.
Does CakePHP provide any support for 'installing' a web app?
Part of my problem is that my attempts at Googling for "CakePHP web app installation" are all overshadowed by the various tutorials (etc) about how to install CakePHP itself. My issue is not installing CakePHP, it's providing an easy and safe way to set up the stuff my web app needs (like SQL database tables, etc) for it's particular needs.
It's called Cake Schemas....
The simplest thing you can do in your development environment is run the following via command line from root:
./app/Console/cake schema dump --write filename.sql
Which gives you a dump of your SQL file then you can edit the sql file directly before using it.
You specifically ask for running $this->User->save(), while learning about Schemas might be a bit complicated, you can accomplish this by running
./app/Console/cake schema generate
Which creates your schema.php, then:
App::uses('User', 'Model');
public function after($event = array()) {
if (isset($event['create'])) {
switch ($event['create']) {
case 'users':
App::uses('ClassRegistry', 'Utility');
$user = ClassRegistry::init('User');
$user->create();
$user->save(
array('User' =>
array(
'username' => 'admin',
'role' => 'admin',
'password' => 'admin'
)));
break;
}
}
}
Which makes a definition as you wish, then when you run:
./app/Console/cake schema create
Your tables get dropped, but remade as per your schema definitions and your model files, and with your specific "after" function
http://book.cakephp.org/2.0/en/console-and-shells/schema-management-and-migrations.html
I've been previously using tools such as Google Website Optimizer to run multi-variation or A/B tests. However right now I am looking for a solution that works for a larger site (400-500 000 unique visitor per month) with a very locked down source code environment. Basically:
The site is balanced over several servers
All code that is to be released on any of those servers must go via version control, unit testing and acceptans testing. All releases must be signed by develop, sys-admin and test executive.
This means that I am not allowed/it's hard to add "new code" (even if it's tested and verified) via Google Website Optimizer or any other of the GUI-paste-your-new-variation-here type of solutions.
We can however on server side decide which users gets which variation. Basically we can push the new version on X of the servers making 10-30% of the users view it for their entire session. The question is: Which tools do we use to measure "success" (i.e improved conversion rate). My idea so far has been:
Tag the new version in Google Analytics using a session variable (and then make reports based on segment) (similar to what is described on http://searchengineland.com/how-to-analyze-ab-tests-using-google-analytics-67404 )
Use Optimizely which has API support:
window.optimizely = window.optimizely || [];
window.optimizely.push(['bucketUser', EXPERIMENT_ID, VARIATION_ID])
What solutions have you tried for locked-down environments? Am I missing some obvious solution?
The site is in .NET/Episerver on IIS.
Regards,
Niklas
You could use the AB-testing capability built into EPiServer CMO.
We ended up going with Google Analytics and adding a session variable such as "abtest" with value "variation-4" and publishing it on certain nodes. It worked fairly well, with some limitations, namely that google analytics funnels doesn't have segment support.
We did something similar and we have found Google Analytics documentation confusing. In the end the following code (made by server) got the work done for us:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxx-xxxx', {
'custom_map': {'dimension1': 'abTestDesign'}
});
gtag('event', 'abTestDesign_dimension', {'abTestDesign': 0, 'non_interaction': true});
</script>
This code is generated by the server, where the last JS line is either that one of
gtag('event', 'abTestDesign_dimension', {'abTestDesign': 1, 'non_interaction': true});
Seems to be working fairly well on Numbeo.com
Hy there,
I can't find enough beginner resources on the web about HTML5 database storage usage examples (CRUD)
I'm opening(creating) my DB like this:
var db;
$(document).ready(function()
{
try
{
if (!window.openDatabase) {
alert('Not Supported -> Please try with a WebKit Browser');
} else {
var shortName = 'mydatab';
var version = '1.0';
var displayName = 'User Settings Database';
var maxSize = 3072*1024; // = 3MB in bytes 65536
db = openDatabase(shortName, version, displayName, maxSize);
}
}
catch(e)
{
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}return;
}
});
QUESTION 1: How many databases can i create and use on one domain?
QUESTION 2. How to delete (drop) a database. -> i have not figured this out yet.
To create sql queries you use transaction:
function nullDataHandler(transaction, results) { }
function createTables(db)
{
db.transaction(function (transaction)
{
//first query causes the transaction to (intentionally) fail if the table exists.
transaction.executeSql('CREATE TABLE people(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL DEFAULT "John Doe", shirt TEXT NOT NULL DEFAULT "Purple");', [], nullDataHandler, errorHandler);
});
}
QUESTION 3: How so is the above transaciton failed if a table exists? Is the nullDataHandler involved to do this? Where on the web is there documentation explaining the executeSql API? Arguments?
thx
The spec you're looking for is Web SQL Database. A quick reading suggests:
There is no limit, although once your databases increase beyond a certain size (5MB seems to be the default), the browser will prompt the user to allow for more space.
There is no way, in the current spec, to delete databases.
The executeSql() function takes an optional error callback argument.
HTML5 Doctor also has a good introduction.
Going forward, though, I'd recommend looking at Indexed DB. Web SQL has essentially been abandoned since there is no standard for SQL / SQLite. Even Microsoft has endorsed Indexed DB. See Consensus emerges for key Web app standard.
CREATE TABLE IF NOT EXISTS table_name
will create a table table_name only if if does not exist.
I found the following WebSQL tutorials helpful for basic CRUD operations, as they contained examples, and explained what the code was doing:
A Simple TODO list using HTML5 WebDatabases
HTML5 Web SQL Database
And the following links for SequelSphere (an HTML5 JavaScript SQL Relational Database Alternative to WebSQL that works in all browsers, storing data in LocalStorage and IndexedDB):
SequelSphere basic Usage instructions
API Documentation
Full Index of Guides and Helper Documentation
Using PersistenceJS there is a persistence.reset API which will wipe the database clean.
PersistenceJS Site
For developing / testing purposes, you can view content and delete webSQL, IndexedDB, cookies, etc by searching for your domain name at this URL in Chrome:
chrome://settings/cookies
There, you can delete all the storage for a domain or just certain local storage entities. Yes, the URL implies just 'cookies', but the interface at this URL includes all types of offline storage.
It would be great I think if the Chrome developer tools interface had the ability to right-click and delete a data storage entity in the Resources tab along with inspecting the content. But for now, all I know of is the settings/cookies URL.
It is supported on iOS safari,chrome and some latest version of opera....it's not yet adopted by IE and Firefox that's it......what more one can ask than local db on browser which has relational db system...so u can query it easily and handle complex data....which is very tougher in key vale based systems..
I remember reading it even supports upto one gb.i am not sure....
Note:
1)I'd like to mention one point there is a IDE called Dashcode which let's u build web apps that looks like iOS native.even there also web SQL is used.
2)actually web SQL is a implementation of SQLite on browsers.
3)SQLite is most prefered in both iOS and android as db for native code..
The drawbacks of SQLite:
The Lack of concurrency support but which is not a problem in browser as it's gonna be used by single user at a time..this is a case also in mobile.
Conclusions:
Web Sql is abandoned by w3 that's a sad thing so we've to explore other options.